Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-24-2009, 11:01 AM   PM User | #1
apatisk
New to the CF scene

 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
apatisk is an unknown quantity at this point
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)
}
apatisk is offline   Reply With Quote
Old 07-24-2009, 04:23 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Do you mean several consecutive words (a phrase) or a number of different words spread throughout the document?
Philip M is offline   Reply With Quote
Old 07-25-2009, 06:10 AM   PM User | #3
apatisk
New to the CF scene

 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
apatisk is an unknown quantity at this point
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"
apatisk is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:34 AM.


Advertisement
Log in to turn off these ads.