Javascript 从扩展名调用chrome.tabs.update时将停止工作
我尝试了以下代码。它基本上从当前窗口中打开的所有选项卡中获取屏幕截图:Javascript 从扩展名调用chrome.tabs.update时将停止工作,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我尝试了以下代码。它基本上从当前窗口中打开的所有选项卡中获取屏幕截图: function captureWindowTabs(windowId, callbackWithDataUrlArray) { var dataUrlArray = []; // get all tabs in the window chrome.windows.get(windowId, { populate: true }, function(windowObj) { var tabArray =
function captureWindowTabs(windowId, callbackWithDataUrlArray) {
var dataUrlArray = [];
// get all tabs in the window
chrome.windows.get(windowId, { populate: true }, function(windowObj) {
var tabArray = windowObj.tabs;
// find the tab selected at first
for(var i = 0; i < tabArray.length; ++i) {
if(tabArray[i].active) {
var currentTab = tabArray[i];
break;
}
}
// recursive function that captures the tab and switches to the next
var photoTab = function(i) {
chrome.tabs.update(tabArray[i].id, { active: true }, function() {
chrome.tabs.captureVisibleTab(windowId, { format: "png" }, function(dataUrl) {
// add data URL to array
dataUrlArray.push({ tabId:tabArray[i].id, dataUrl: dataUrl });
// switch to the next tab if there is one
if(tabArray[i+1]) {
photoTab(i+1);
}
else {
// if no more tabs, return to the original tab and fire callback
chrome.tabs.update(currentTab.id, { active: true }, function() {
callbackWithDataUrlArray(dataUrlArray);
});
}
});
});
};
photoTab(0);
});
}
函数captureWindowTabs(windowId,callbackWithDataUrlArray){
var dataUrlArray=[];
//获取窗口中的所有选项卡
get(windowId,{populate:true},函数(windowObj){
var tabArray=windowObj.tabs;
//首先查找选定的选项卡
对于(变量i=0;i
当我从作为网页打开的popup.html调用此代码时,它会按预期工作(我通过在popup.html中单击按钮触发此操作)。当我从浏览器扩展调用它时,它只是从它选择的第一个选项卡中断。知道为什么吗?我无法共享错误,因为从扩展调用调试器时调试器会关闭
补充,有没有一种方法可以在不需要切换可视选项卡的情况下达到预期的效果 将下一个选项卡更新为活动选项卡时。通过执行以下操作,确保当前选项卡不再是活动选项卡
chrome.tabs.update(tabArray[i-1].id, { active: false }, ()=>{});
将扩展移到后台脚本修复了该问题
原因是一旦选项卡切换,弹出窗口将关闭。因此,它需要在后台运行,在弹出窗口关闭时不会中断 它不起作用了。从弹出窗口激活第一个选项卡的那一刻起,调试器就停止了,其余的代码就不会被执行。我想这是因为我必须将它移到后台才能让它继续运行。谢谢,是的<代码>弹出窗口关闭后停止执行