metamorpov/content_script.js

63 lines
1.0 KiB
JavaScript

var person;
loadReplace();
function loadReplace()
{
chrome.storage.local.get("person", function(value)
{
person = value.person;
if(person != null){walk(document.body);}
}
)
}
function walk(node)
{
// I stole this function from here:
// http://is.gd/mwZp7E
var child, next;
switch ( node.nodeType )
{
case 1: // Element
case 9: // Document
case 11: // Document fragment
child = node.firstChild;
while ( child )
{
next = child.nextSibling;
walk(child);
child = next;
}
break;
case 3: // Text node
handleText(node);
break;
}
}
function handleText(textNode)
{
var v = textNode.nodeValue;
v = v.replace(/\by\/n\b/ig, person);
textNode.nodeValue = v;
}
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutations, observer) {
loadReplace();
});
// define what element should be observed by the observer
// and what types of mutations trigger the callback
observer.observe(document, {
subtree: true,
childList: true
//...
});