open text file with imacros

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the Google search box (at the top of each forum page) to see if a similar problem or question has already been addressed. This will search the entire contents of the forums as well as the iMacros Wiki.
3. We can respond much faster to your posts if you include the following information:

CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST

Answering your own posts (e.g. attempting to "bump" your topic) drops your topic from the list of unanswered threads, so it may actually receive less views.
Post Reply
upl4you
Posts: 12
Joined: Fri Nov 23, 2012 11:22 am

open text file with imacros

Post by upl4you » Wed Nov 02, 2016 10:41 pm

My configuration details
OS Windows 8.1 64 bit
Firefox : 49
Imacro version 8.9.7

Hello
I'm using XMLHttpRequest from years ago on firefox up to 39 to read local text file but the firefox disable XHR in all the new version of firefox from 40 until now I want to update my source code to get it working with the new version of firefox

Here is my old code :

Code: Select all

function readTextFile(file)
{const XMLHttpRequest = Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1");
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {         allText = rawFile.responseText;
                  }
        }
    }
    rawFile.send(null);
    rawFile = null;
    }
readTextFile("file:///D:/textfile.txt");
Any help would be appreciated
chivracq
Posts: 7971
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: open text file with imacros

Post by chivracq » Thu Nov 03, 2016 3:18 am

upl4you wrote:My configuration details

Code: Select all

OS Windows 8.1 64 bit
Firefox : 49
Imacro version 8.9.7
Hello
I'm using XMLHttpRequest from years ago on firefox up to 39 to read local text file but the firefox disable XHR in all the new version of firefox from 40 until now I want to update my source code to get it working with the new version of firefox

Here is my old code :

Code: Select all

function readTextFile(file)
{const XMLHttpRequest = Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1");
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {         allText = rawFile.responseText;
                  }
        }
    }
    rawFile.send(null);
    rawFile = null;
    }
readTextFile("file:///D:/textfile.txt");
Any help would be appreciated
Hum, I don't understand what your 'readTextFile()' Function is doing exactly (I'm no JavaScript Guru, and especially with "complicated" 'XMLHttpRequest' Functions and "status=0 or 4") but if you just want to read a '.TXT' File, you can simply open it (from iMacros) in a second TAB in FF and treat it and access it from iMacros like any Web-Page loaded in a TAB... (and use 'EXTRACT' to fetch its Content...)

If your '.TXT' File contains some "complex" Data that you need to (re)use bit by bit, you will need 'EVAL()' (+ 'split()' for example) to separate the Data in the parts that you want, and you may want to format your '.TXT' File in a specific way to facilitate this Process.

If your Data is "very complex", you can even save your '.TXT' File, opened from Excel or OO, as HTML where all Cells will be seen by iMacros as separate HTML Elements, and adding an extra Column with a Row-Counter (+ Relative Positioning) and using Colours for Columns that you can use as Attributes for 'TAG' can make it very easy to specify/locate a specific Cell...

This is actually a very powerful Method to check the whole Content of a DataSource with just one Statement without having to loop the DataSource Row by Row which I have described and explained a few times on the Forum...
Last edited by chivracq on Thu Nov 03, 2016 12:27 pm, edited 2 times in total.
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
upl4you
Posts: 12
Joined: Fri Nov 23, 2012 11:22 am

Re: open text file with imacros

Post by upl4you » Thu Nov 03, 2016 8:32 am

Thank you chivracq for your reply
I must open text file with javascript because the file is big and very complex to extract data from it with imacros it's long time and high cpu usage

The firefox have replaces the xhr with fetcher api but I don't know how to use it for opening local text file
chivracq
Posts: 7971
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: open text file with imacros

Post by chivracq » Thu Nov 03, 2016 1:07 pm

upl4you wrote:Thank you chivracq for your reply
I must open text file with javascript because the file is big and very complex to extract data from it with imacros it's long time and high cpu usage

The firefox have replaces the xhr with fetcher api but I don't know how to use it for opening local text file
It's not iMacros which opens the File, it's the Browser, I open a '.TXT' File with up to 4000 Rows in less than a Second in one of my Macros, it can even already be ("manually") pre-loaded in TAB_2 (+ 'REFRESH' eventually if the Content changes), provided it always has the same name, so Size won't be the Pb, if JavaScript can open it, your Browser can open it as well...!

Well "very complex" is always "relative", ah-ah...! 'EVAL()' is as clever as you want to be to locate and isolate the specific Data that you need, and 'match()', 'SEARCH' and 'split()' are very powerful Functions/Commands. (I prefer using 'split()' myself as I'm a bit "afraid" or using REGEX, ah-ah...! :oops: )
And I gave you otherwise (the) 2 ways to deal with ("very") "complex" Data...
You can upload an Example of your '.TXT' File to your Thread (zipped, Max 256Kb) and mention what Data exactly you want to isolate and I can have a look...
"My" own '.TXT' File is about 40Kb per 1000 Rows => 400Kb for 10,000 Rows => 85Kb once zipped. I'd be surprised if you hit the 256Kb Limit...!
Last edited by chivracq on Thu Nov 03, 2016 2:17 pm, edited 1 time in total.
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
upl4you
Posts: 12
Joined: Fri Nov 23, 2012 11:22 am

Re: open text file with imacros

Post by upl4you » Thu Nov 03, 2016 1:22 pm

as I do many search on the file in different location in every loop and I don't want to loop on the file each time
1)is it possible to read all text file and put it in variable ?

2) in some of my case I have text file about 100k line and I search for the line that contain "threadID=71537" and for let me confirm it's really available on my text or I extract what's come after 71537 in the same line or more line below that id to javascript variable
and the search changed in every loop
chivracq
Posts: 7971
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: open text file with imacros

Post by chivracq » Thu Nov 03, 2016 2:39 pm

upl4you wrote:as I do many search on the file in different location in every loop and I don't want to loop on the file each time
1)is it possible to read all text file and put it in variable ?
This is exactly what I meant with:
This is actually a very powerful Method to check the whole Content of a DataSource with just one Statement without having to loop the DataSource Row by Row which I have described and explained a few times on the Forum...
You don't seem to really understand my Replies which surprises me as "my Method" is much more simple and easier to use than your technically high Level Method with your complex 'readTextFile()' Function that I in return have difficulties to follow...
upl4you wrote:2) in some of my case I have text file about 100k line and I search for the line that contain "threadID=71537" and for let me confirm it's really available on my text or I extract what's come after 71537 in the same line or more line below that id to javascript variable
and the search changed in every loop
Yep, you simply use one of the 3 Functions/Commands I mentioned or the very same Mechanism you are already using which should keep working, and even easier (again...!, as you don't seem to understand my Answers...), if your Data is already formatted in "similar" Rows, you save it as HTML from Excel or OO.
The only "bottleneck" might be that if your File contains 100,000 Lines (your "100k line" is not clear to me, especially because of "line" without an "s", and if "Line" = "Row") and those 100,000 Lines = 100,000 Rows, there used to be a 64k Limitation for the Number of Rows Excel could handle, but I'm talking about years ago, I don't know if there is still this Limit, but then you can always split your File into 50k Rows (<64k) and you open them in TAB_2 + TAB_3.
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
upl4you
Posts: 12
Joined: Fri Nov 23, 2012 11:22 am

Re: open text file with imacros

Post by upl4you » Thu Nov 03, 2016 4:22 pm

I checked your profile posts and there is too many posts you have and I can't find relevant answer of eval for opening text file
please show me sample code to read all text file and put it in variable using eval
chivracq
Posts: 7971
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: open text file with imacros

Post by chivracq » Thu Nov 03, 2016 5:48 pm

upl4you wrote:I checked your profile posts and there is too many posts you have and I can't find relevant answer of eval for opening text file
please show me sample code to read all text file and put it in variable using eval
But pfff, you don't need any Example(s), it is very basic use of iMacros that anybody should understand and master in less than 1 hour after just discovering iMacros for the first time and from your Script and your Date of Registration on the Forum you must be way above that Level... :roll:

Step by Step Instructions:
**********************************
1- You open your '.TXT' File from FF in some "free/new" TAB using FF Menu: File / Open File.
2- Using the Standard 'TAG' Mode you record clicking anywhere in the middle of your File opened in that TAB.
iMacros will record stg like:

Code: Select all

TAG POS=1 TYPE=PRE ATTR=TXT:Beginning<SP>of<SP>your<File...*
3- You simply add "EXTRACT=TXT" like for any Extract to that Statement:

Code: Select all

TAG POS=1 TYPE=PRE ATTR=TXT:Beginning<SP>of<SP>your<File...* EXTRACT=TXT
4- You run your Macro, and... Bingo!!, the whole Content of your '.TXT' gets extracted and stored in the '!EXTRACT' Built-in Var that you now can treat with 'EVAL()' like any "Standard" Extract...!

In Step 3-, you can even simplify the Statement to make it "universal" to work with any '.TXT' File opened and extracted this way with:

Code: Select all

TAG POS=1 TYPE=PRE ATTR=TXT:* EXTRACT=TXT
Instead of 'TYPE=PRE', you can even use 'TYPE=BODY' or 'TYPE=HTML', they all 3 work the same.
(Tested (again) using iMacros for FF v8.8.2, Pale Moon v26.3.3 (=FF47), Win10-x64.)

With a large amount of Text, the Extract Test Popup has a Limitation of a few Kb with what it can display and it will "explode" your Screen anyway (you'll need to close it using the 'Close' Button, the 'OK' and 'Cancel' Buttons are usually not reachable), but the EXTRACT Mechanism is still able to extract the whole Content.

Bottleneck might be that FF might take quite some time and might appear to freeze when extracting such a large amount of Text, if this is the case, you'll have to use the HTML Solution that I already mentioned twice where this won't happen as the whole Content of your File will get "divided" into Rows and Cells = 'TD' HTML Elements that you can extract separately..., removing by the same way the need to use 'EVAL()' to isolate/locate the Data that you really want.
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
upl4you
Posts: 12
Joined: Fri Nov 23, 2012 11:22 am

Re: open text file with imacros

Post by upl4you » Fri Nov 04, 2016 2:03 pm

Thank you chivracq for your explanation
as my javascript file is very big and do a lot of treatment that cannot be used with eval but I take the idea of opening new tab and open text file with imacros and extract it and go back to my JSscript automatically I think I can go with it

Thank you so much Mr chivracq i really appreciate it
truongfiu-vn
Posts: 1
Joined: Mon Jan 14, 2019 9:51 am

Re: open text file with imacros

Post by truongfiu-vn » Mon Jan 14, 2019 9:57 am

This code using XMLHttpRequest to read local text file working for me

Code: Select all

let url = "file:///D:/list.txt" ;
          let request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
                    .createInstance(Components.interfaces.nsIXMLHttpRequest);
      request.onload = function(aEvent) {
        text = aEvent.target.responseText
      }
      request.open("GET", url, false);
      request.send();
      iimDisplay(text);
Tom, Tech Support
Posts: 3413
Joined: Mon May 31, 2010 4:59 pm

Re: open text file with imacros

Post by Tom, Tech Support » Thu Jan 17, 2019 2:09 pm

chivracq wrote:
Thu Nov 03, 2016 5:48 pm
With a large amount of Text, the Extract Test Popup has a Limitation of a few Kb with what it can display and it will "explode" your Screen anyway (you'll need to close it using the 'Close' Button, the 'OK' and 'Cancel' Buttons are usually not reachable), but the EXTRACT Mechanism is still able to extract the whole Content.
This pop-up can always be suppressed by adding SET !EXTRACT_TEST_POPUP NO to the macro.
Regards,

Tom, iMacros Support
Post Reply