0

In my extension, I send some injection code when the current tab url is in the list of my target urls. here is the code

chrome.tabs.onUpdated.addListener(function(tabId, info) { if(info.status == "complete") { var tabUrl = ""; var run = false; chrome.tabs.get(tabId, function(tab) { tabUrl = tab.url; }); var storedList = localStorage["GAR_ExcList"]; if(!storedList) storedList = ""; var storedListArray = storedList.split("\n"); for(var i = 0; i < storedListArray.length; i++) { var ind = tabUrl.indexOf(storedListArray[i]); alert("for " + i + " index is " + ind); if(ind != -1) { alert("Running"); run = true; break; } } if(run) { chrome.tabs.executeScript(tabId, { file: "js/jquery-1.6.1.min.js" }, function() { chrome.tabs.executeScript(tabId, { file: "js/inject.js"}); }); } else { alert("excluding"); } }}); 

Even though this code perfectly well. For some reason, when I comment out the alert within the for loop, I get run = false and I receive the last alert, which I should not.

Has anyone ever seen something like this before? Any help is very much appreciated.

Best,

1
  • console.log is better than alert. Use that and try printing tabUrl and storedListArray. Commented May 14, 2011 at 2:03

1 Answer 1

1

Instead of:

chrome.tabs.onUpdated.addListener(function(tabId, info) { ... var tabUrl = ""; chrome.tabs.get(tabId, function(tab) { tabUrl = tab.url; }); 

Try:

chrome.tabs.onUpdated.addListener(function(tabId, info, tab) { ... var tabUrl = tab.url; 

chrome.tabs.get() is asynchronous, so you need to put the rest of the code inside its callback if you use this approach.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.