I want to use Greasemonkey to highlight two words e.g. "Basel, Bern". If I use only Basel the version below works. Not very well but well enough. But when I use two words the highlighting doesn't work.
// ==UserScript==
// @name highlight-some-words
// @description highlight some words in html
// @grant none
// ==/UserScript==
document.body.innerHTML= document.body.innerHTML.replace(/Basel|Bern/g, function(m){
return '<span style="background-color:lightgreen">'+m+'</span>'
});
EDIT: Interesting, the script works on stackoverflow.com but not google.com. Why? And how to modify the script then?
As I said in the comment, works fine (through console) on google.com AFTER searching for Basel and Bern ... perhaps as a GM script it is running too early
@wOxxOm raises a very good point - changing innerHTML will mess up event handlers in the page, a better way to do it would be to change only text nodes. The following is probably not the most efficient way of doing this, but it's a derivative of a greasemonkey script i wrote many many years ago, back when greasemonkey was less than a year old!!