What I want to do is replace all instances of 'foo' in a webpage with 'bar' in a JS bookmarklet/greasemonkey script. How can I do this? I suppose jQuery works, as there're hacks to include those in both bookmarklets and greasemonkey scripts.
This script iterates through each element in the document and replaces every instance of foo
with bar
.
The gi
modifiers on the regex make it do a global, case-insensitive search.
var els = document.getElementsByTagName("*");
for(var i = 0, l = els.length; i < l; i++) {
var el = els[i];
el.innerHTML = el.innerHTML.replace(/foo/gi, 'bar');
}
You can target specific tag names by changing the "*"
to the tag name of your choice (e.g. "p"
, "td"
).