IMacro Scripting - How to read a local .txt file u

2019-05-22 19:59发布

There is this IMacro scripting tool, if you want to automate some web page visits by using javascript.

I would like to have my javascript to read from a local .txt file (not a .cvs file and not well formatted.. I would like to search in it probably with a regular expression..) and based on that reading, the script will do some job in IMacros.. (e.g call some web site url etc..)

Do you guys have any idea how this can be done ? I am doing everything local and that is my local browser reading from my local hard drive.. it should be somehow possible.. but how ?

4条回答
Deceive 欺骗
2楼-- · 2019-05-22 20:32

in Firefox you can read the file directly.

more info at https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O#Line_by_line

to read file line by line use the following

var FileUtils = Components.utils.import("resource://gre/modules/FileUtils.jsm").FileUtils;

FileLocation = "C:\\myFile.txt"

var file   = new FileUtils.File( FileLocation );

// open an input stream from file
var istream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
istream.init(file, 0x01, 0444, 0);
istream.QueryInterface(Components.interfaces.nsILineInputStream);

// read lines into array
var line = {}, lines = [], hasmore;
do {
  hasmore = istream.readLine(line);
  lines.push(line.value); 
} while(hasmore);

istream.close();

// do something with read data
alert(lines);
查看更多
三岁会撩人
3楼-- · 2019-05-22 20:33

Yes you can do it with imacros, but you need to call it from javascript.js file. load your content as one block, then you can use javascript indexOf method to find the string in the text and perform if statement. Text example (inside your txt file): "hello world!"

var load;
load =  "CODE:";
load +=  "set !extract null" + "\n"; 
load +=  "SET !DATASOURCE text.txt" + "\n"; 
load +=  "SET !DATASOURCE_COLUMNS 1" + "\n"; 
load +=  "SET !DATASOURCE_LINE 1" + "\n"; 
load +=  "SET !extract {{!col1}}" + "\n";
iimPlay(load);
var s=iimGetLastExtract(0);
var index=s.indexOf("w");
if (index>0){
do your code;
}
查看更多
爷的心禁止访问
4楼-- · 2019-05-22 20:39

I solved it in the old fashioned way - reading line by line:

function read_file(path) {
    var content = '', l = 1, f, res = '';

    do {
        content += res && (res + "\n");
        f = "CODE: "+"\n";
        f += "SET !EXTRACT null" + "\n"; 
        f += "SET !DATASOURCE \""+path+"\" "+"\n";
        f += "SET !DATASOURCE_COLUMNS 1" + "\n"; 
        f += "SET !DATASOURCE_LINE " + l + "\n"; 
        f += "SET !EXTRACT {{!col1}}" + "\n";
        iimPlay(f);
        res = iimGetLastExtract();
        l++;
    } while (res && res != '#EANF#');

    return content;
}

var file_conten = read_file('/home/user/iMacros/templates/some_file.txt');

Hope it'll help future readers ^_^

查看更多
闹够了就滚
5楼-- · 2019-05-22 20:42

You have to use xml http request as Activex object of file is not supported by any other browser than IE.

This code works perfectly fine while reading local txt or any other file too.

f();
function f()
{
    var allText =[];
    var allTextLines = [];
    var Lines = [];
    var txtFile = new XMLHttpRequest();

    txtFile.open("GET", "file://D:/test.csv", true);
    allText = txtFile.responseText;
    //allTextLines = allText.split(/\r\n|\n/);//splits ur file line by line.

    //alert(allTextLines);
    txtFile.onreadystatechange = function()
    {
        if (txtFile.readyState == 4)
        {
            // Makes sure it's found the file.
            allText = txtFile.responseText;
            allTextLines = allText.split(/\r\n|\n/);

            alert(allText);
        } else { //alert("Didn't work"); 
        }
    }
    txtFile.send(null)
}
查看更多
登录 后发表回答