Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript - Greasemonkey problem

    Hi everyone,

    i was hoping one of you could help me with this.
    I found a greasemonkey-script which basicly does exactly what i want - it highlights a word when a web-page loads.

    however, i'm trying to modify this script to highlight several words, preferably from a text-file "database" located locally (or externally).

    Any help would be greatly appreciated, however i realize it's probably not done in a couple of minutes.

    Code:
    TEXT = "someword";
    COLOR = "red";
    
    
    var allText = document.evaluate( "//text()[contains(., '" + TEXT + "' )]", document, null, XPathResult. ORDERED_NODE_SNAPSHOT_TYPE , null);
    
    for(var i = 0; i < allText.snapshotLength; i++)
    {
    var cur = allText.snapshotItem(i);
    var par = cur.parentNode;
    var textInd;
    var curName = cur.nodeName;
    do
    {
    var curText = cur.nodeValue;
    GM_log("curText: " + curText);
    textInd = curText.indexOf(TEXT);
    if(textInd != -1)
    {
    var before = document.createTextNode( curText.substring(0, textInd ) );
    var highlight = document. createElement("span");
    highlight.class = "highlight";
    highlight.textContent = TEXT;
    highlight.style.color = COLOR;
    var after = document.createTextNode( curText.substring(textInd + TEXT.length) );
    par.insertBefore(before, cur);
    par.insertBefore(highlight, cur);
    par.insertBefore(after, cur);
    par.removeChild(cur);
    cur = after;
    }
    } while(textInd != -1)
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,141
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Do you mean several consecutive words (a phrase) or a number of different words spread throughout the document?

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Do you mean several consecutive words (a phrase) or a number of different words spread throughout the document?
    im thinking both actually;

    "word"
    "another word"
    "and a complete phrase"


  •  

    Posting Permissions

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