Highlight specific words on any page...

Subscribe to Highlight specific words on any page... 20 posts, 6 voices

 
husamn User

is there a script that highlights specific words that I want on any site/page?

 
husamn User

please.

 
husamn User

please.

 
dob Scriptwright

var b = document.body;
b.innerHTML = b.innerHTML.replace(new RegExp("("+"your word"+")"), "<span style='color:#f00;font-weight:bold;'>$1</span>");

 
Mikado Scriptwright

Crappy way, dob.

var RE = /(script)/;

for (var tx = document.evaluate('//text()[normalize-space(.)!=""]', document, null, 6, null), t, i = 0; t = tx.snapshotItem(i); i++) {
	var before =  t.textContent, st, matched = false;
	if (t.parentNode.tagName == 'STYLE' || t.parentNode.tagName == 'SCRIPT') continue;
	while ((st = before.search(RE)) != -1) {
		t.parentNode.insertBefore(document.createTextNode(before.substr(0, st)), t);
		t.parentNode.insertBefore(document.createElement('b'), t).textContent = RegExp.$1;
		matched = true;
		before = before.substr(st + RegExp.$1.length);
	}
	if (matched) t.textContent = before;
}

 
dob Scriptwright

That's why I'm still learning :)

 
husamn User

thanks mikado, worked...but is there a way I can add more words? and also color them instead just bold?

also when I add a word like (My Home) it only highlights exactly as it's typed, like it doesn't highlight (my home) just (My Home)
with capital letters.

thanks.

 
husamn User

please.

 
Mikado Scriptwright

husamn wrote:

but is there a way I can add more words? <...> also when I add a word like (My Home) it only highlights exactly as it's typed, like it doesn't highlight (my home) just (My Home)

var RE = /(phrase one|phrase two|etc)/i; - i at the end stands for case insensivity.

husamn wrote:

and also color them instead just bold?

Replace t.parentNode.insertBefore(document.createElement('b'), t).textContent = RegExp.$1; with:

with (t.parentNode.insertBefore(document.createElement('span'), t))
	textContent = RegExp.$1,
	style.cssText = 'color: #f00; size: 14pt;';

Add any styles you like to the cssText string.

 
husamn User

multi word/case insensivity worked.
without replacing the color code.

but when I replace, the script doesnt work at all.

 
husamn User

please.

 
Mikado Scriptwright

I've mismatched quotes in that line, try again.

 
husamn User

k.

 
husamn User

nope, still not working.

 
Mikado Scriptwright

Working for me.

 
husamn User

not working for me.
can't you just repost the full (working) script?

 
Mikado Scriptwright

var RE = /(e)/;

for (var tx = document.evaluate('//text()[normalize-space(.)!=""]', document, null, 6, null), t, i = 0; t = tx.snapshotItem(i); i++) {
	var before =  t.textContent, st, matched = false;
	if (t.parentNode.tagName == 'STYLE' || t.parentNode.tagName == 'SCRIPT') continue;
	while ((st = before.search(RE)) != -1) {
		t.parentNode.insertBefore(document.createTextNode(before.substr(0, st)), t);
		with (t.parentNode.insertBefore(document.createElement('span'), t))
			textContent = RegExp.$1,
			style.cssText = 'color: #f00; font-size: 14pt;';
		matched = true;
		before = before.substr(st + RegExp.$1.length);
	}
	if (matched) t.textContent = before;
}

 
^tyrant^ User

is there another way to do the var? i added the words i want to highlight and its a pretty large list and it causes the script to stall

 
itsjareds Scriptwright

How could I use this script to replace text smilies with images (data: uris)?

I cant figure it out

This is what I need to incorporate it in:


var happy = "data:image/gif;base64,R0lGODlhEQARALMAABy+ASTTAiXWAiHLAhWtARawAR7EARq ... ";

function replaceSmilie(smilie, surl)
{
  var reptext = new RegExp(smilie, "gi");
  document.body.innerHTML = document.body.innerHTML.replace(reptext, "<img src='" + surl + "' height='17' width='17' align='center' style='border:none;' />");
}

replaceSmilie(":\\)", happy);

Someone told me that using innerHTML replace was much slower, but I dont know how to use your way.

 
norz User

Note: this script deletes words inside textareas instead of highlighting them. (Tested with Firefox 3 and Safari 3.)
Workaround: exclude pages with textareas in greasemonkey or greasekit.