PDA

View Full Version : GM help?



Shawn
06-09-2012, 08:51 AM
if(document.body.innerHTML.indexOf('Red Apple') > -1) {
var item= document.evaluate('//b[. = "What What What Stick"]',document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);if (item.snapshotlength > 0){item = item.snapshotItem(0);selectedlink=item.previousSib ling.previousSibling;window.location = selectedlink}return;}


Could someone explain to me what is happening here, step by step please?
I'm trying to learn by adapting available code :$


The only thing I partially understand is

if(document.body.innerHTML.indexOf('Red Apple') > -1)
Where the character string "Red Apple" is searched for in the page. And an if event is triggered if it is > -1? I don't under stand the > -1 part.

Zachafer
06-09-2012, 12:11 PM
if(document.body.innerHTML.indexOf('Red Apple') > -1) {
var item= document.evaluate('//b[. = "What What What Stick"]',document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);if (item.snapshotlength > 0){item = item.snapshotItem(0);selectedlink=item.previousSib ling.previousSibling;window.location = selectedlink}return;}


Could someone explain to me what is happening here, step by step please?
I'm trying to learn by adapting available code :$


The only thing I partially understand is

if(document.body.innerHTML.indexOf('Red Apple') > -1)
Where the character string "Red Apple" is searched for in the page. And an if event is triggered if it is > -1? I don't under stand the > -1 part.


if(document.body.innerHTML.indexOf('Red Apple') > -1)indexOf returns the index of the parameter in the instance string. If the parameter does not exist in the instance string, indexOf returns -1. The parameter in this case is 'Red Apple' and the instance string is document.body.innerHTML. So that if statement is checking if 'Red Apple' is found in document.body.innerHTML


var item= document.evaluate('//b[. = "What What What Stick"]',document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);This is fairly advanced code. This uses XPath (XML Path language) to find a certain element in the document. I believe '//b[. = "What What What Stick"]' finds a bold element (<b>) that contains the text "What What What Stick".


if (item.snapshotlength > 0){item = item.snapshotItem(0);selectedlink=item.previousSib ling.previousSibling;window.location = selectedlink}return;}

This code looks to see if the previous XPath expression found the element (snapshotlength > 0). If so, it sets the first resultant element to item (for convenience purposes). It then sets a variable (selectedlink) to the item's previousSibling's previousSibling (I can't really explain this without seeing the HTML). It then points the browser to that URL.

This looks like code for a GM ABer or something like that. Hope I helped.

Shawn
06-10-2012, 12:58 AM
Yes I understand this now. Yes, it is from a GM ABer that I'm trying to adapt.