0

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?

user2651403
  • 478
  • 5
  • 17
  • As a comment, telling those who are answering your questions that their "answer is junk" isn't particularly polite or constructive. If there is something wrong with an answer, please try to give a little more detail as to what's wrong with it. – Brad Larson Sep 08 '13 at 21:30

1 Answers1

1

You will want to use Content Scripts for this, here is the documentation, http://developer.chrome.com/extensions/content_scripts.html. You are also using the "replace()" method incorrectly. That method only alters the value of whatever variable you are applying it to, in your case "str". If you want to change the value of "javascriptstring" you can just place

javascriptstring = 'javascriptstring';

in the js file wish to run when the specific domain loads.

  • No, I can't use a content script. Because my content script runs on a different domain. Unless you can tell me how to run two separate content scripts on two separate domains (one content script ONLY runs on the one domain and NOT the other; and vice-versa) within a single Chrome extension, which I don't think is possible. Thus I need to do it with a pure javascript file. – user2651403 Aug 05 '13 at 00:47
  • 1
    Try adding another object to the Content scripts array. For example: `"content_scripts": [ { "matches": ["http://domain1.com/*"], "js": ["js1.js"] }, { "matches": ["http://domain2.com/*"], "js": ["js2.js"] } ]` –  Aug 05 '13 at 01:23
  • No, it's not working. I said thank you as a courtesy but it did not solve the problem. I am no further ahead than when I started. – user2651403 Aug 05 '13 at 02:05
  • @user2651403 This answer is correct, you have to use content scripts. If you want to edit variables within the page, a script has to be [injected](http://stackoverflow.com/a/9517879) though. If you want to modify some visible text inside the document, see [this example](http://stackoverflow.com/a/12212700/938089). – Rob W Aug 05 '13 at 09:15
  • No - I'm using a content script but this is not doing anything on the page. Hello World works which means the script is running on the page, but the code is Not Working to replace anything. I Do Not want to modify visible text. I want to modify Javascript Code in the page. – user2651403 Aug 05 '13 at 15:27