chrome扩展:不调用回调函数 [英] chrome extension: callback function not getting called

查看:238
本文介绍了chrome扩展:不调用回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小型扩展程序( https:// docs .google.com / leaf?id = 0B5ZSnXcRXnSpMmM0NTFiNGEtMzEzZS00M2YzLWI4MzItMmVmNmM3OGE1MDRh& hl = en& authkey = CLzGpOMN )在关闭该会话时保存特定窗口中的所有选项卡。
在此,当我尝试恢复会话时,虽然新窗口已成功打开,但我没有调用回调函数。

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.

这里是代码:

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); });
}


推荐答案

有趣的问题。当您创建一个新窗口时弹出窗口会自动关闭(因此弹出代码执行被终止),这就是它在开发人员模式下工作的原因,因为它会强制弹出窗口保持打开状态。您需要将 restoreTabs()功能移动到后台页面,您仍然可以通过弹出窗口轻松调用它:

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); 
};

这篇关于chrome扩展:不调用回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆