我正在尝试编写一个Chrome扩展程序,该扩展程序调用通过内容脚本注入某些网站的函数。通过浏览器控制台调用时,脚本运行良好,但无法通过tabs.executescript起作用,因为它似乎比内容脚本的注入(因此调用空函数)要快
ExecuteScript调用:
chrome.tabs.executeScript(tab.id, {
code: 'var nummer = ' + nummer + '; var tabURL = "' + taburl + '";'
}, function() {
chrome.tabs.executeScript(tab.id, {
file: "launch-script-manually.js"
}, function() {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
}
});
});
function launch (url, name, callOnLoad) {
window.open(url, name)
.addEventListener(
"load",
callOnLoad,
{ once: true, passive: true }
);
}
function proceed (evt) {
console.log("A window has been loaded");
console.log(evt);
if(evt.currentTarget.checkbtn(nummer) == 1) {
console.log("It worked!" + evt);
}
else {
document.body.innerHTML = document.body.innerHTML + "Window" + evt + "Failed<br>"
}
}
async function launchProcess (taburl) {
document.body.innerHTML = "";
const sleep3 = { then(resolve) { setTimeout(resolve, 250); } };
for (let i = 2; i < 7; ++i) {
launch(taburl, "tab" + i, proceed);
await sleep3;
}
}
launchProcess(tabURL);
这会导致控制台错误:
Uncaught TypeError: evt.currentTarget.checkbtn is not a function at proceed (launch-script-manually.js:24)
但是,如果我通过控制台手动运行它,效果很好!
如何解决此问题,以便它可以通过executeScript调用运行?