I'm writing a Chrome Extension that changes some words in a webpage to others. This is what I have and I'm not getting any words to change and no console errors.
myscript.js
var text = $(this).text();
var mapObj = {
is:"tests",
a:"AAAAAAAA",
the:"testing"
};
var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
$(this).text(text.replace(re, function(matched){
return mapObj[matched.toLowerCase()];
}));
manifest.json
{
"manifest_version": 2,
"name": "example",
"description": "bla",
"version": "1.0",
/* "browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},*/
"content_scripts": [
{
"matches": ["<all_urls>"],
"js":["js/jquery-3.1.1.js", "myscript.js"],
"run_at": "document_end"
}
],
"permissions": [
"activeTab",
"<all_urls>"
]
}
I followed this guys answer but it's not changing the words for me. What am I doing wrong?
UPDATE I have tried this:
$(this).text(text.replace(new RegExp("the", "g"), "test"));
and it works. and this:
document.body.innerHTML = document.body.innerHTML.replace(new RegExp("the", "g"), "hunter");
and it works. So it obviously has something to do with this (and not any other file or permission. Correct?):
var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
$(this).text(text.replace(re, function(matched){
return mapObj[matched.toLowerCase()];
}));
In the end I want to loop through each word and test if it needs to be changed, and if so change it. Please let me know if there is a better way.