...

View Full Version : Javascript - Greasemonkey problem



apatisk
07-24-2009, 11:01 AM
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.



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)
}

Philip M
07-24-2009, 04:23 PM
Do you mean several consecutive words (a phrase) or a number of different words spread throughout the document?

apatisk
07-25-2009, 06:10 AM
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"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum