I would like to create Google Chrome extension. Its job is to replace a word with another on all websites.
I have the following manifest.json file:
{
"name": "My extension",
"version": "1.0",
"background_page": "background.html",
"permissions": [
"tabs", "http://*/*"
],
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["myscript.js"],
"run_at": "document_end"
}
]
}
and the javascript in myscript.js is:
< script type="text/javascript" >
document.body.innerHTML = document.body.innerHTML.replace("uno", "dos");
< /script >
However this does not function.. and I cannot find a way to debug the content script, only the background.html
Use the DOM and modify the
data
of the appropriateText
nodes. E.g.I have actually written this in jQuery: (Making sure you have the correct include tag)
Replacing/changing text within the DOM on this scale should not be done with blunt HTML-regex replacement, which is very unsafe. You risk mutilating the HTML in the process.
What you need to do is loop over every TextNode (
Node
) within the document, modifying the text within them.Your code will end up looking something like this:
I took the example from JavaNut13 and Matt Curtis to create an alias hider extension for Reddit, and updated it for the new manifest 2. It looks for user on Reddit named "user1" and replaces it with "nobody". Modify as you need.
manifest.json
myscript.js