Search code examples
javascripthtmldomgreasemonkey

How do I color a string of text RED in Greasemonkey?


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.


Solution

  • 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]+\)/