Replacing the innerHTML
will also replace attributes and text within script
tags. To replace only text nodes inside non-script tags, use a TreeWalker
.
var treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT),
textNode;
while(textNode = treeWalker.nextNode()) {
if(textNode.parentElement.tagName !== 'SCRIPT') {
textNode.nodeValue = textNode.nodeValue.replace(/10:00/g, "11:00 AM");
}
}
Also, you don't need to append a script to the DOM to be able to access its contents. Content scripts can modify the DOM.
JSBin Demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…