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