Skip to content

Instantly share code, notes, and snippets.

@roger-mo-gusto
Created June 19, 2020 04:40
Show Gist options
  • Save roger-mo-gusto/9e239fd062a670a60b9b126fbdd142c3 to your computer and use it in GitHub Desktop.
Save roger-mo-gusto/9e239fd062a670a60b9b126fbdd142c3 to your computer and use it in GitHub Desktop.
(function () {
const ELEMENT_NODE = 1;
const TEXT_NODE = 3;
// https://stackoverflow.com/a/34559316
function walkText(node) {
if (node.nodeType == TEXT_NODE) {
node.data = node.data.replace(/(?<=[ \t]|(?:^|\"|\-|\=|(?:[.!?]\s)))(Sean|sean)(?=([ \t]|\.|\'||\-|\=|\"|\?|\/|\,))/g, "Shane");
} else if (node.nodeType == ELEMENT_NODE && node.nodeName != "SCRIPT") {
for (var i = 0; i < node.childNodes.length; i++) {
walkText(node.childNodes[i]);
}
}
}
walkText(document.body);
document.title = document.title.replace(/(?<=[ \t]|(?:^|\"|\-|\=|(?:[.!?]\s)))(Sean|sean)(?=([ \t]|\.|\'||\-|\=|\"|\?|\/|\,))/g, "Shane");
})();
This is a super simple example of a chrome extension that runs on every page.
It converts all instance of the word Sean to Shane.
I made it because our nickname for our roommate Sean is Shane.
If you want it to run on only specific pages, modify the "matches" part of the manifest.json to the URL.
Ex:
// Only run on the GitHub pull request page
// https://developer.chrome.com/extensions/match_patterns
"matches":[
"*://github.com/org_name/repo_name/pull/*"
],
To install:
- Save the manifest.json and background.json to a folder
- open chrome://extensions
- enable Developer mode at the top right
- click on "Load unpacked"
- select the folder
{
"name": "2Shanez",
"version": "1.0",
"description": "Sean 2 Shane",
"manifest_version": 2,
"persistent": false,
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"background.js"
],
"run_at": "document_end"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment