Javascript 后台页面JS代码在弹出式控制台中以不同方式记录

Javascript 后台页面JS代码在弹出式控制台中以不同方式记录,javascript,google-chrome-extension,google-chrome-devtools,Javascript,Google Chrome Extension,Google Chrome Devtools,我遇到了一个非常奇怪的错误 在我的background.js文件中,我有一个函数,可以像这样关闭选项卡 closeTabs = function(tabIds,category){ for(var i=0; i<tabIds.length;i++){ var url = findTabById(tabIds[i]).url; console.log(url); reservedUrls.push(url); } con

我遇到了一个非常奇怪的错误

在我的background.js文件中,我有一个函数,可以像这样关闭选项卡

closeTabs = function(tabIds,category){
    for(var i=0; i<tabIds.length;i++){
        var url = findTabById(tabIds[i]).url;
        console.log(url);
        reservedUrls.push(url);
    }
    console.log(arrToString(reservedUrls));
    if(tabIds.length>0){
        chrome.tabs.remove(tabIds,function(){
        });
    }
}
closeTabs=函数(tabid,类别){
对于(var i=0;i0){
chrome.tabs.remove(tabid,function()){
});
}
}
此函数是通过我的浏览器操作调用的

然后,我有一个用于remove选项卡事件的事件侦听器

chrome.tabs.onRemoved.addListener(function(tabId){
    console.log('removed');
    var removedUrl = findTabById(tabId).url;
    console.log(removedUrl);
    console.log(arrToString(reservedUrls));
    var index = reservedUrls.indexOf(removedUrl);
    if(index>-1){
        console.log('in here');
        reservedUrls.splice(index,1);
    } else {
        var category = findCategoryById(tabId);
        if(category){
            for(var i=0;i<currentTabs[category];i++){
                if(currentTabs[category][i].id==tabId){
                    currentTabs[category].splice(i,1);
                }
            }
        } else {
            for(var i=0;i<ungrouped.length;i++){
                if(ungrouped[i].id==tabId){
                    ungrouped.splice(i,1);
                }
            }
        }
        console.log('sending message');
        chrome.runtime.sendMessage({type:'removed',tabId:tabId});
    }
    console.log(currentTabs);
});
chrome.tabs.onRemoved.addListener(函数(tabId)){
console.log('removed');
var removedUrl=findTabById(tabId).url;
日志(removedUrl);
控制台日志(arrToString(reservedUrls));
var指数=reservedUrls.indexOf(removedUrl);
如果(索引>-1){
console.log('in here');
保留剪接(索引1);
}否则{
var类别=findCategoryById(tabId);
如果(类别){

对于(var i=0;i您删除选项卡脚本是在自己的上下文中执行的,这并不意味着它会同步删除选项卡。因此,一旦从系统中真正删除了选项卡,您的侦听器就会获得事件。您可以使用一个页面来检查这一点,该页面阻止选项卡关闭表单卸载事件