1

So I'm using iMacros and I will use a JS file to play my macro based on a few if statements.

Here's a small sample pseudocode:

if (TextContent == "Hello World")
{
    iim.Play("myMacro.iim);
}

I'm trying to check if the webpage contains the word "Hello World!".

I heard about the TextContent method but I don't know how to use. Is it the proper way to use it? If not then how do you use it properly?

UPDATE

I've tried the ones but it's giving me a reference error.

puretppc
  • 3,232
  • 8
  • 38
  • 65

2 Answers2

2

This code is totally wrong if you want to run it in iMacros

if (TextContent == "Hello World")
{
    iim.Play("myMacro.iim);
}

This code is CORRECT.

var macro;

macro ="CODE:";
macro +="SET !ERRORIGNORE YES"+"\n";
macro +="TAG POS=1 TYPE=HTML ATTR=TXT:* EXTRACT=TXT"+"\n";

iimPlay(macro)

var text=iimGetLastExtract();

if(text.search("Hello World")!=-1)
{
    alert("Found the text");
}

This will extract all text from the page and if there is Hello World in it, it will alert "Found the text". Also there are tons of examples on how to make iMacros JavaScript script.

Here is one on the link

Also you can't use JavaScript code in iMacros just like that. If you use

document.getElelementById("some_id").click();

just like this in the script it will not work.

But if you use it as a part of URL GOTO command it might work in IE and earlier versions of Firefox.

URL GOTO=javascript:document.getElelementById("some_id").click();

The rule is

  1. First extract with iMacros TAG command
  2. Store the text in variable with iimGetLastExtract()
  3. Use search(), match(), indexOf() method to find what you are looking for.
Community
  • 1
  • 1
edinvnode
  • 3,497
  • 7
  • 30
  • 53
  • Thanks a lot it's working :). Also, what does `macro ="CODE:";` do? Doesn't it work without using that? – puretppc Feb 08 '14 at 02:21
  • The script doesn't work if you remove that part. And the code I placed is a model of iMacros JavaScript code. – edinvnode Feb 08 '14 at 13:20
0

textContent is property of documentElement so you need to:

if ((document.documentElement.textContent || document.documentElement.innerText).indexOf('Hello World') > -1)
{
    iim.Play("myMacro.iim);
}

Or as suggested in comment use jQuery's text() method which is cross-browser compatible

Wojciech Bednarski
  • 6,033
  • 9
  • 49
  • 73
  • I doubt imacros has jQuery or anything like that. So wait uh do I have to define an object variable for `document` and `documentElement` or is it a universal way for me to call? I hardly know JS so yeah just trying to learn – puretppc Feb 06 '14 at 23:09
  • @puretppc `document` is the "web page" let say, try it in you extension to see if that works. You can include jQuery in your extension as well. https://developer.mozilla.org/en-US/docs/Web/API/document – Wojciech Bednarski Feb 06 '14 at 23:11
  • So basically the name of `document` is the name of whatever the title of the webpage is? – puretppc Feb 06 '14 at 23:15
  • @puretppc No. Look at the link above. – Wojciech Bednarski Feb 06 '14 at 23:41
  • I'm getting this error when I tested the code: `ReferenceError: document is not defined, line: 1 (Error code: 991)` – puretppc Feb 07 '14 at 05:11