Skip to content

Instantly share code, notes, and snippets.

@kwilczynski
Created July 30, 2021 03:45
Show Gist options
  • Save kwilczynski/52aee7ae8e0d27bdc21e858fcb7ec78e to your computer and use it in GitHub Desktop.
Save kwilczynski/52aee7ae8e0d27bdc21e858fcb7ec78e to your computer and use it in GitHub Desktop.
// 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