I am currently working on a web add-on that does certain things depending on what site you are currently on. The add-on works fine when I go to a webpage from the URL bar like for instance yahoo.com, but when I try to make the javascript run from any other page on yahoo it doesn’t run. The code I am using is:
if(document.URL.search("yahoo.com")>=0){
var scriptYahoo = document.createElement('script');
scriptYahoo.type='text/javascript';
scriptYahoo.src='http://localhost:8001/yahooChrome.js';
body.appendChild(scriptYahoo);
}
I am under the impression that the URL.search will look throughout the entire URL and come back true if it finds it anywhere in the URL. If someone could just try to let me know why this only works when you are going to yahoo.com and not any other site navigated to inside of yahoo.com.
I have narrowed it down a bit and realized that the file that they are located in does not even get called whenever the pages are loaded. It does get called though when I just go to some random other webpage.
—–EDIT—–
I apologize for my lack of complete knowledge of what is going on, I am taking over this code from another developer and don’t really know exactly what is going on yet. What I have discovered is that if this code were just simply run it would return every website with yahoo.com in the URL. This is never getting called at any point though. I have this function here to see if a tab has been modified at all:
chrome.tabs.onUpdated.addListener(check);
//chrome.tabs.onClicked.addListener(test);
function check(tab_id,data,tab){
console.log("updated");
if (data.status == "complete")
{
chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
console.log(response.farewell); });
}
};
If I put an alert in here anywhere in the function it will always hit no matter what page is being loaded. When I get over to my onRequest function though it does not hit if it is some variation of yahoo.com
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?"from a content script:" + sender.tab.url :"from the extension");
trackInfo();
setTimeout("init();", 2000); //wait till the page got loaded
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
else
sendResponse({});
});
So at this point I am not sure why it would stop catching all the variations inside of the function. Thanks again for the help it’s much appreciated.
I figured it out, I simply needed to do:
instead of:
in my manifest file and it fixed all of my issues.