Results 1 to 3 of 3

Thread: GM help?

  1. #1

    Joined
    Jun 2012
    Posts
    2,236
    Pronouns
    He / Him
    Userbars
    39
    Thanks
    1,472
    Thanked
    2,167/810
    DL/UL
    16/0
    Mentioned
    228 times
    Time Online
    63d 23h 28m
    Avg. Time Online
    22m

    GM help?

    Code:
    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.previousSibling.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
    Code:
    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.
    Last edited by Shawn; 06-09-2012 at 09:00 AM.

  2. #2
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m
    Quote Originally Posted by Shawn View Post
    Code:
    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.previousSibling.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
    Code:
    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.
    Code:
    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

    Code:
    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".

    Code:
    if (item.snapshotlength > 0){item = item.snapshotItem(0);selectedlink=item.previousSibling.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.

  3. The Following User Says Thank You to Zachafer For This Useful Post:

    Shawn (06-10-2012)

  4. #3

    Joined
    Jun 2012
    Posts
    2,236
    Pronouns
    He / Him
    Userbars
    39
    Thanks
    1,472
    Thanked
    2,167/810
    DL/UL
    16/0
    Mentioned
    228 times
    Time Online
    63d 23h 28m
    Avg. Time Online
    22m
    Yes I understand this now. Yes, it is from a GM ABer that I'm trying to adapt.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •