Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Based on https://github.com/ericwbailey/millennials-to-snake-people.
function walkTextNodes(rootNode) {
let walker = document.createTreeWalker(rootNode, NodeFilter.SHOW_TEXT,
function(node) {
if (node.textContent.length === 0) {
return NodeFilter.FILTER_SKIP;
}
return NodeFilter.FILTER_ACCEPT
},
false);
let node;
while (node = walker.nextNode()) {
handleText(node);
}
}
function handleText(textNode) {
textNode.nodeValue = replaceText(textNode.nodeValue);
}
function replaceText(value) {
return value.replace(/\b(y|Y)our\b/g, "$1ou're");
}
function isForbiddenNode(node) {
return node.isContentEditable ||
(node.parentNode && node.parentNode.isContentEditable) ||
(node.tagName && (node.tagName.toLowerCase() == "textarea" ||
node.tagName.toLowerCase() == "input"));
}
function observerCallback(mutations) {
let i, node;
mutations.forEach(function(mutation) {
for (i = 0; i < mutation.addedNodes.length; i++) {
node = mutation.addedNodes[i];
if (isForbiddenNode(node)) {
continue;
}
if (node.nodeType === 3) {
handleText(node);
} else {
walkTextNodes(node);
}
}
});
}
(function (content) {
let observerConfig = {
characterData: true,
childList: true,
subtree: true,
};
walkTextNodes(content.body);
content.title = replaceText(content.title);
let bodyObserver = new MutationObserver(observerCallback);
bodyObserver.observe(content.body, observerConfig);
let title = content.getElementsByTagName('title')[0];
if (title) {
let titleObserver = new MutationObserver(observerCallback);
titleObserver.observe(title, observerConfig);
}
})(document);
{
"manifest_version": 2,
"name": "You're welcome!",
"description": "You need it. Period.",
"version": "0.1.0",
"content_scripts": [
{
"matches": [
"*://*/*"
],
"all_frames": true,
"js": [
"content.js"
],
"run_at": "document_end"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment