Skip to content

Instantly share code, notes, and snippets.

@jmeyo
Forked from danharper/background.js
Last active March 2, 2016 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmeyo/ee54ebb27a415b1f7a04 to your computer and use it in GitHub Desktop.
Save jmeyo/ee54ebb27a415b1f7a04 to your computer and use it in GitHub Desktop.
Bare minimum Chrome extension to inject a JS file into the given page when you click on the browser action icon. The script then inserts a new div into the DOM.
// this is the background code...
// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function (tab) {
// for the current tab, inject the "inject.js" file & execute it
chrome.tabs.executeScript(tab.ib, {
file: 'inject.js'
});
});
// this is the code which will be injected into a given page...
(function() {
walk(document.body);
function walk(node)
{
// I stole this function from here:
// http://is.gd/mwZp7E
var child, next;
if (node.tagName.toLowerCase() == 'input' || node.tagName.toLowerCase() == 'textarea'
|| node.classList.indexOf('ace_editor') > -1) {
return;
}
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(/\bTrump\b/g, "Drumpf");
textNode.nodeValue = v;
}
})();
{
"name": "Victor",
"version": "0.0.1",
"manifest_version": 2,
"description": "Injecting stuff",
"homepage_url": "http://www.houseofagile.com",
"background": {
"scripts": [
"background.js"
],
"persistent": true
},
"browser_action": {
"default_title": "Inject!"
},
"permissions": [
"https://*/*",
"http://*/*",
"tabs"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment