2

如果你想通过使用 javascript 来自动化一些网页访问,有这个IMacro脚本工具。

我想让我的 javascript 从本地 .txt 文件中读取(不是 .cvs 文件并且格式不正确..我想用正则表达式在其中搜索..)并基于该阅读,脚本将在 IMacros 中做一些工作..(例如调用一些网站 url 等..)

你们知道如何做到这一点吗?我在本地做所有事情,那是我的本地浏览器从我的本地硬盘读取..它应该以某种方式可能..但是如何?

4

4 回答 4

3

是的,你可以用 imacros 来做,但你需要从 javascript.js 文件中调用它。将您的内容作为一个块加载,然后您可以使用 javascript indexOf 方法在文本中查找字符串并执行 if 语句。文本示例(在您的 txt 文件中):“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;
}
于 2013-02-10T20:19:19.893 回答
1

您必须使用 xml http 请求,因为除 IE 之外的任何其他浏览器都不支持文件的 Activex 对象。

此代码在读取本地 txt 或任何其他文件时也可以正常工作。

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)
}
于 2011-02-03T10:13:10.073 回答
1

我以老式的方式解决了它 - 逐行阅读:

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');

希望对以后的读者有所帮助^_^

于 2015-09-03T10:25:59.833 回答
1

在 Firefox 中,您可以直接读取文件。

更多信息请访问https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O#Line_by_line

逐行读取文件使用以下

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);
于 2017-02-22T01:34:57.970 回答