Search code examples
loopsimacroskeyword-search

Trying to do a looped search for keywords with imacros


I have a csv file with 128 different keywords (1 column, 128 rows) that I am trying to search for on my current page, and if it locates one of them to continue with a very simple task, else prompt me saying none of the keywords were found.

The goal is to have something like the following:

SET !DATASOURCE ...                            'already have this part working
SET !DATASOURCE_COLUMNS 1
SET !DATASOURCE_LINE {{!LOOP}}
{{!LOOP}} = 1
if TAG POS=1 TYPE=* ATTR=TXT:{{!COL1}} = true   'not actual code from here down 
move to next step
elseif {{!LOOP}} +1 = 129
PROMPT "No keyword was found"
END                                             'killing the script
else 
loop++                                          'loop and search for next keyword

If it matters, the keywords are actually IP addresses with the final block missing IE. "192.168.0." Not sure if it would affect formatting or not. I'm sure there's a better way to do it with search. I'm just looking for a 0 or 1 (yes or no response) if a keyword is present and then continue following links if found.


Solution

  • If you want to do conditionals such as if-then you will need to use a scripting language[1]. I have provided a Javascript example below that follows your pseudocode.

    var datasource, macro, retcode, numberOfLinesInDatasource, aTagWasFound;
    
    datasource = "somedatasource";
    numberOfLinesInDatasource = 128;
    aTagWasFound = false;
    
    macro = "CODE:";
    // change link to a different website
    macro += "URL GOTO=http://stackoverflow.com/questions/25467549/trying-to-do-a-looped-search-for-keywords-with-imacros\n";
    retcode = iimPlay(macro);
    
    // loop through all lines in datasource
    for (var i = 0; i < numberOfLinesInDatasource; i++)
    {
      // get the datasource value at this line
        macro = "CODE:";
        macro += "SET DATASOURCE " + datasource + "\n";
        macro += "SET DATASOURCE_COLUMNS 1\n";
        macro += "SET DATASOURCE_LINE " + i + "\n";
        macro += "ADD !EXTRACT {{!COL1}}\n";
        retcode = iimPlay(macro);
        keyword = iimGetLastExtract();
    
        // search for this keyword
        macro = "CODE:";
        macro += "TAG POS=1 TYPE=* ATTR=TXT:" + keyword + "\n";
        retcode = iimPlay(macro);
    
        // if retcode is 1 then the tag was found move on to next step
        if (retcode === 1){
            // move on to next step
            aTagWasFound = true;
            break;
        }
        // tag not found try the next value
    }
    
    if (!aTagWasFound)
    {
        alert("No keyword found");
    } else 
    {
        alert("Tag Found");
    }