I'm trying to write a bookmarklet that calls the function doSomething(textNode)
on all instances of visible text on the document.
doSomething()
, just for fun, replaces every word with "derp" by replacing the textContent of the textNode passed into it. However, this makes some textNodes that are empty to have words in them, so it breaks the web page.
Is there a way to call doSomething()
on only every textNode that has words in it?
function recurse(element)
{
if (element.childNodes.length > 0)
for (var i = 0; i < element.childNodes.length; i++)
recurse(element.childNodes[i]);
if (element.nodeType == Node.TEXT_NODE && element.nodeValue != '')
doSomething(element);
}
var html = document.getElementsByTagName('html')[0];
recurse(html);