Skip to content Skip to sidebar Skip to footer

Javascript For Replacing Text In The Body Tag Of Pages Loaded Into An Open Source Browser For Android

I'm writing a JavaScript for an open source browser available for Android to replace the text in the body tag of the pages loaded into the browser with some different text. This s

Solution 1:

Sorry, you don't get DOM Level 3 XPath in the Android browser.

Whilst you can use a JavaScript XPath implementation (eg), that's going to be a slow and bulky solution compared to writing specific DOM traversal code.

functionreplaceInTextNodes(parent, needle, replacement) {
    for (var child= parent.firstChild; child!==null; child= child.nextSibling) {
        if (child.nodeType===1) { # Node.ELEMENT_NODEvar tag= child.tagName.toLowerCase();
            if (tag!=='script' && tag!=='style' && tag!=='textarea')
                replaceInTextNodes(child, needle, replacement);
        }
        elseif (child.nodeType===3)
            child.data= child.data.replace(needle, replacement);
    }
}
replaceInTextNodes(document.body, /'/g, '\u2665');

Post a Comment for "Javascript For Replacing Text In The Body Tag Of Pages Loaded Into An Open Source Browser For Android"