Trying to replace a javascript string on a specific domain page using a chrome extension.
Here is what I have so far:
manifest.json file (not the whole file of course)
"permissions": [ "tabs", "cookies", "http://*/*" ],
"background": {
"page": "background.html"
},
background.html file
<script type="text/javascript" src="js/replace.js"></script><br>
js/replace.js file
if (window.location.href.indexOf('http://somedomain.com/page') != -1) {
var str="javascriptstring = javascriptstring('javascriptstring');";
var n=str.replace("aaaaaaaaaaaaa;");
}
Nothing happens on the page when I open the page. I view Page Source but nothing is replaced.
I tried replacing the js/replace.js file with the following code to test it:
if (window.location.href.indexOf('http://somedomain.com/page') != -1) {
alert("Hello World!");
}
But yet, no alert is popping up saying hello world when the page is loaded. Which means that probably the script is not even running on the page, unless I wrote the Hello World javascript wrong.
What am I doing wrong?
p.s. the rest of my extension (which is unrelated to this) works just fine.
UPDATE:
I tried another method, which also is not working:
loc = window.location.href;
match = loc.match(/somedomain\.com\/page);
if(match)
window.location = loc.replace(bsa, "aaaaa");
UPDATE TWO:
I tried this code instead, and used a content script instead defining in the manifest to only run this file on this URL.
loc = window.location.href;
alert("Hello World!");
Sucess on getting Hello World popup on page load; let's now try to do the replace.
UPDATE THREE:
I'm getting Hello World popup which means the script is running, but the code is not being replaced.
TRY 1:
loc = window.location.href;
alert("Hello World!");
window.location = loc.replace(texttext, "aaaaa");
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
--
TRY 2:
loc = window.location.href;
alert("Hello World!");
loc.replace(texttext, "aaaaa");
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
--
TRY 3:
loc = window.location.href;
alert("Hello World!");
texttext = "aaaaa";
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
--
TRY 4:
loc = window.location.href;
alert("Hello World!");
var str="texttext";
var n=str.replace("texttext","aaaaa");
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
So still no good. What am I doing wrong?