IMacro 脚本 - 如何使用 javascript 读取本地 .txt 文件

发布于 2024-08-01 23:57:20 字数 377 浏览 8 评论 0原文

如果你想自动化一些网页,可以使用这个 IMacro 脚本工具 使用 javascript 进行页面访问。

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

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唯憾梦倾城 2024-08-08 23:57:20

是的,您可以使用 imacros 来完成此操作,但您需要从 javascript.js 文件中调用它。 将您的内容作为一个块加载,然后您可以使用 javascript indexOf 方法查找文本中的字符串并执行 if 语句。
文本示例(在您的 txt 文件中):
“你好世界!”

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;
}

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;
}
骷髅 2024-08-08 23:57:20

您必须使用 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)
}

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)
}
泛滥成性 2024-08-08 23:57:20

我用老式的方式解决了这个问题 - 逐行阅读:

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

希望它能帮助未来的读者^_^

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 ^_^

东风软 2024-08-08 23:57:20

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文