开发者

Chrome extension: callback function not getting called

开发者 https://www.devze.com 2023-02-13 08:13 出处:网络
I am developing a small extension( https://docs.google.com/leaf?id=0B5ZSnXcRXnSpMmM0NTFiNGEtMzEzZS00M2YzLWI4MzItMmVmNmM3OGE1MDRh&hl=en&authkey=CLzGpOMN ) that saves all tabs in a particular wi

I am developing a small extension( https://docs.google.com/leaf?id=0B5ZSnXcRXnSpMmM0NTFiNGEtMzEzZS00M2YzLWI4MzItMmVmNmM3OGE1MDRh&hl=en&authkey=CLzGpOMN ) that saves all tabs in a particular window, while closing that session. In this, when I am trying to restore the session, 开发者_如何转开发I am not getting callback function getting called, though the new window is successfully opened.

The funny thing is, when in developer mode, using developer tools, the callback function gets called and restored all tabs. Please help me.

Here is the code:

function restoreTabs( saveTabName ) 
{

var tabVals = window.localStorage.getItem(saveTabName);

if (tabVals == null)
    return;

var callbackFunc = function (window, tabValList) {
    //alert('created window');
    for (var i = 0; i < tabValList.length; i++) {
        var tab = eval('(' + tabValList[i] + ')');
        var newTabObj = {
            windowId: window.id,
            index: tab.index,
            url: tab.url,
            selected: tab.selected,
            pinned: tab.pinned
        };
        chrome.tabs.create(newTabObj);
    }
};

var tabValList = tabVals.split('|');
chrome.windows.create(null, function (win) { callbackFunc(win, tabValList); });
}


Interesting problem. Popup is getting automatically closed when you create a new window (and as a result popup code execution is terminated), that's why it works in developer mode only because it forces the popup to stay open. You need to move restoreTabs() function to a background page, you can still easily call it from your popup:

linka.onclick = function () { 
    chrome.extension.getBackgroundPage().restoreTabs('saveTabs'+savetabName); 
};
0

精彩评论

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