开发者

strange chrome extension javascript problem

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.addListene开发者_Go百科r(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,


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜