How to use browserify with fs to download a page's html and use elements of it in a script?

371 Views Asked by At

I am using Node.js to write a script that would cycle through a downloaded html to find some elements and then build a macro by adding them to the commands. I call that script from another macro from the browser (the calling macro also downloads the html and places it in a file), I will put that macro on loop. The problem is that I can't get the script to be executed. I tried using browserify, and it did the trick, but it says that fs.readFileSync() is not a function. I know of brfs, but it creates a static entry into the code, and the Source.htm file is dynamic and will reload with different information in it. So I'm in a ditch here. I read all about browserify-fs as well, but it doesn't do anything. The following code is basically the small script I wrote, I didn't include the part that browserify generates. I also skipped the part with the macro creation, because that's irrelevant. I must say that I'm a noob and I did a lot of reading to get even this far, but currently I have no idea how to proceed, and any help will be greatly appreciated!

    var fs = require('fs');
    var pageSource = fs.readFileSync('Source.htm', 'utf8').toString().split("\n");
    var idStrings = [];
    var userIDs = [];

    //getting string lines from page source array containing ids
    for(i in pageSource){
        if(pageSource[i].includes("id=\\")){
            idStrings[i] = pageSource[i];
            }
    }

    //getting numbers from string lines using regex
    for(i in tradeStrings){
        userIDs[i] = idStrings[i].replace(/^\D+|\D+$/g, "");
    }

    //sorting just in case
    userIDs.sort(function(a,b){return a - b}); 

    //building macro and executing it...
1

There are 1 best solutions below

3
On

Fundamentally, a web browser will not permit a script to open an arbitrary file on the hard drive. This is for user security - imagine what a malicious web site could do with full file system access!

Rather than save to a file, have the first script download the resource into a javascript variable and parse it from there.