0

How can I write a simple Greasemonkey script that allows it to search for a string of text on one website and then simply color it red?

For example, say a site had the words, "Normal Healthy (R11-0902 Gr 9)" ... could we program this script to make all instances of this turn red? I would edit the script often to add additional strings to turn red.

halfer
  • 19,824
  • 17
  • 99
  • 186
WebMW
  • 514
  • 2
  • 13
  • 26

1 Answers1

2

Following code will highlight all the Greasmonkey in this page.

document.body.innerHTML= document.body.innerHTML.replace(/Greasemonkey/g, function(m){
    return '<span style="background-color:yellow">'+m+'</span>'
});

In your case the pattern would be something like /\w+ \w+ \(\w\d\d-\d{4} \w\w \d\)/. This will only work if your format of the word (Normal Healthy (R11-0902 Gr 9)) is consistent.

Other formats can be,

  1. /\w+ \w+\([^\)]+\)/
    
  2. /\w+ \w+\([^\s\w\d]+\)/
    
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Thank you, this has helped a lot! I am attempting to expand your script to make it more powerful here: http://stackoverflow.com/questions/8982891/small-greasemonkey-script-that-highlights-h2-elements - Please feel free to take a look, I would really appreciate it! – WebMW Jan 24 '12 at 06:58
  • 1
    This is a really bad idea. Changing the whole page's `innerHTML` (!!!) will trash event handlers. Also, when "Greasemonkey" inevitably changes to something real, you will get [the nightmare of mixing regex and HTML/JS](http://stackoverflow.com/a/1732454/331508). – Brock Adams Jan 24 '12 at 07:14
  • 1
    Well if you or anyone has any suggestions for the link I posted in my last comment, I'm all ears and do appreciate your advice. – WebMW Jan 24 '12 at 17:07