Google chrome extension chrome.tabs.executeScript仅处理之前的警报
我相信这与chrome扩展的异步性有关 我的代码的这一部分:Google chrome extension chrome.tabs.executeScript仅处理之前的警报,google-chrome-extension,Google Chrome Extension,我相信这与chrome扩展的异步性有关 我的代码的这一部分: alert(tab.title); chrome.tabs.executeScript(tab.id, {code:"document.title = 'test'"}); 工作正常,但一旦我删除警报,它就会停止工作。我能做些什么来移除警报,但仍然注入js 编辑:更多代码 选项卡是选项卡对象的全局数组 chrome.tabs.onSelectionChanged.addListener(function (tabId
alert(tab.title);
chrome.tabs.executeScript(tab.id, {code:"document.title = 'test'"});
工作正常,但一旦我删除警报,它就会停止工作。我能做些什么来移除警报,但仍然注入js
编辑:更多代码
选项卡是选项卡对象的全局数组
chrome.tabs.onSelectionChanged.addListener(function (tabId) {
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].id == tabId) {
var tab = tabs[i];
while (i < tabs.length) {//length-1
tabs[i] = tabs[i+1];
i++;
}
tabs.pop();
alert(tab.title);//WHY IS THIS NEEDED
chrome.tabs.executeScript(tab.id, {code:"document.title = document.title.substring(1)"});
return;
}
}
});
但是,只要我将延迟更改为50,脚本就不会再次执行。我宁愿不必耽搁。有人知道发生了什么吗?听起来你有比赛条件。我的最佳猜测是将注入的代码更改为在onLoad上执行。听起来您有一个竞争条件。我的最佳猜测是将注入的代码更改为在onLoad上执行。我知道这是一个老问题,但如果有人在看,请在超时后包装chrome.tabs.executeScript方法
setTimeout(function(){chrome.tabs.executeScript(tab.id, {code:"setTimeout('document.title = document.title.substring(1)',100)"});},500);
这当然不理想,但它完成了任务。我知道这是一个老问题,但如果有人在看,请在超时后包装chrome.tabs.executeScript方法
setTimeout(function(){chrome.tabs.executeScript(tab.id, {code:"setTimeout('document.title = document.title.substring(1)',100)"});},500);
这当然不理想,但它完成了任务。听起来不太对。请发布更多代码。用
break
替换return
。如何构建选项卡
阵列?已替换。选项卡基本上是用“chrome.tabs.getAllInWindow”构建的。同样,代码在没有警报的情况下工作。奇怪!有人知道发生了什么吗?你需要提供更多的代码-你展示的代码似乎不包含错误,问题出在其他地方。这听起来不对。请发布更多代码。用break
替换return
。如何构建选项卡
阵列?已替换。选项卡基本上是用“chrome.tabs.getAllInWindow”构建的。同样,代码在没有警报的情况下工作。奇怪!有人知道发生了什么吗?您需要提供更多的代码-您显示的代码似乎不包含错误,问题出在其他地方。'chrome.tabs.executeScript(tab.id,{code:'window.onload=function(){document.title=document.title.substring(1);};“});'无法工作…'chrome.tabs.executeScript(tab.id,{code:'window.onload=function(){document.title=document.title.substring(1);};“});'不起作用。。。