简体   繁体   English

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

[英]IMacro Scripting - How to read a local .txt file using javascript

There is this IMacro scripting tool, if you want to automate some web page visits by using javascript. 如果您想使用javascript自动进行某些网页访问,则有此IMacro脚本工具。

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.. (eg call some web site url etc..) 我想让我的JavaScript从本地.txt文件(不是.cvs文件,并且格式不正确。.我想用正则表达式进行搜索..),并根据该阅读结果编写脚本将在IMacros中做一些工作。(例如,调用某些网站的网址等。)

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 ? 我正在做本地的所有事情,那是我的本地浏览器从本地硬盘读取的信息..应该是可以的..但是如何?

Yes you can do it with imacros, but you need to call it from javascript.js file. 是的,您可以使用imacros来做到这一点,但是您需要从javascript.js文件中调用它。 load your content as one block, then you can use javascript indexOf method to find the string in the text and perform if statement. 将您的内容加载为一个块,然后可以使用javascript indexOf方法在文本中查找字符串并执行if语句。 Text example (inside your txt file): "hello world!" 文本示例(在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;
}

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 ^_^ 希望对以后的读者有所帮助^ _ ^

in Firefox you can read the file directly. 在Firefox中,您可以直接读取文件。

more info at https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O#Line_by_line 有关更多信息, 访问https://developer.mozilla.org/zh-CN/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);

You have to use xml http request as Activex object of file is not supported by any other browser than IE. 您必须使用xml http请求,因为IE之外的任何其他浏览器均不支持文件的Activex对象。

This code works perfectly fine while reading local txt or any other file too. 读取本地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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM