[英]Background Page JS code logs differently in popup console
我遇到了一个非常奇怪的错误。
在我的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(){
});
}
}
从我的浏览器操作中调用此函数。
然后,我有一个用于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);
});
当选项卡被第一个功能关闭时,我将它们放置在reservedUrls中,以便逻辑上的行为有所不同。 当我查看后台页面的控制台时,它会像应输入的那样输入if语句。 但是,在弹出窗口的控制台中,它将记录它导航到else语句中的情况。
每个人都有这种差异。 如果是这样,典型的原因是什么?
您删除选项卡脚本是在自己的上下文中执行的,这并不意味着它将同步删除选项卡。 因此,一旦从系统中真正删除了选项卡,您的监听器就会获得事件。 您可以使用阻止选项卡关闭表单卸载事件的页面进行检查
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.