PDA

View Full Version : Loop in EVAL in Imacros for Firefox



young_coder
11-30-2011, 09:20 PM
Dear all,
I'm new with JavaScript, so can please help me with this macro?
I need to reload URL (in this case refresh same page) until I don't have word beginning with capitalized A, B or C.
I can't find the way to create loop inside EVAL.
Can somebody help me please?
Thank you!


VERSION BUILD=7401110 RECORDER=FX
TAB T=1
URL GOTO=http://watchout4snakes.com/CreativityTools/RandomWord/RandomWord.aspx
SET !EXTRACT_TEST_POPUP NO
TAG POS=1 TYPE=SPAN ATTR=ID:tmpl_main_lblWord EXTRACT=TXT
SET !VAR1 EVAL("var re = /^[A || B || C]/gi; var str = \"{{!EXTRACT}}\"; while(str.search(re) == -1) {URL GOTO=http://watchout4snakes.com/CreativityTools/RandomWord/RandomWord.aspx}")
PROMPT {{!EXTRACT}}

jscheuer1
11-30-2011, 11:31 PM
If you're trying to use this outside the iMacros plugin, that will never work because that's not really javascript. It is partly. But it's also the iMacros proprietary code. For support on that for use with iMacros you should probably go here:

http://forum.iopus.com/viewforum.php?f=11

young_coder
12-01-2011, 05:43 PM
This is JavaScript issue and I thought somebody can help me here.

jscheuer1
12-01-2011, 07:15 PM
That's not javascript. None of that code is valid javascript, for one it's all caps. Javascript is almost always lowercase, with occasional camelBack notation. and even if the case were correct, the syntax is not javascript.

I will have another look at it to see if there's enough similarity to javascript to have a go at it. But I'd still recommend you go to the iMacros forum.

young_coder
12-01-2011, 08:31 PM
With this part of code I need help, where {{!EXTRACT}} is extracted word (variable) from page. So, while {{!EXTRACT}} is not word that starts with uppercase letter A, B or C, I need to go again and again on the same URL (in this case to refresh same page):



var re = /^[A || B || C]/gi;
var str = \"{{!EXTRACT}}\";
while(str.search(re) == -1)
{
go to this URL
}


John, thank you for your time and effort.

jscheuer1
12-02-2011, 02:37 AM
The code is way messed up javascript. Even if no eval where involved neither the re nor the search will do what's expected/desired.

This re:


/^[A || B || C]/

Means the beginning of the string followed by a space or a letter A, B, or C, or by a | symbol. The gi switch means that the search is global (g) -I'm not sure if that means the beginning of each line or only the very beginning of the string. Experiment is warranted. Or does it matter? You said word, it doesn't check the beginning of each word. The case insensitive (i) means just that - I thought you were only interested in upper case letters.

A better regular expression would be:


/\b[A-C]/g

That would check for a word boundary followed by a capital letter A, B, or C. The g switch would now check the entire string.

But if you use eval, \b means the lower case letter b, so you would have to double escape it:


/\\b[A-C]/g

The search expression:


str.search(re) == -1

will return true if the re is not found in the string, the opposite of what you say you want. I would scrap it altogether and use instead the test method:


re.test(str)

which returns true if the re is in the string, false if it's not.

So, I don't know about reloading of the page or about the EXTRACT or (upper case) EVAL, how any of that works in iMacros. But in javascript this works:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
onload = function(){eval("var re = /\\b[A-C]/g; var str = document.body.innerHTML; alert(re.test(str));");};
</script>
</head>
<body>
Will Alert True Because of the A's and B's at the beginning of words
</body>
</html>

And this:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
onload = function(){eval("var re = /\\b[A-C]/g; var str = document.body.innerHTML; alert(re.test(str));");};
</script>
</head>
<body>
Will alert false because of no upper cAse a's or b's at the beginning of words
</body>
</html>

If you want to play with it, be aware that comments in the body count as text for the function.

Or put another way:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
onload = function(){eval("var re = /\\b[A-C]/g; var str = document.body.innerHTML; if(re.test(str)){location.reload();}");};
var texts = [
"Will be fAlse beCAuse there's no upper cAse a's, b's, or c's at the beginning of words",
"Will Be True Because of the A's and B's at the beginning of words",
"Will be true even though the only 'keyword' is in a span after a linebreak.\n<br><span style='color: red;'>B</span>",
"another one that will be false"
];
texts.sort(function(){return 0.5 - Math.random();});
</script>
</head>
<body>
<script type="text/javascript">
document.write(texts[0]);
</script>
</body>
</html>