javascript - 控制台执行的脚本运行但 tabs.executescript 没有
问题描述
我正在尝试编写一个 Chrome 扩展程序,它调用通过内容脚本注入某些站点的函数。该脚本在通过浏览器控制台调用时运行良好,但不能通过 tabs.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 调用运行?
解决方案
推荐阅读
- javascript - 如何将来自 html 输入字段的文本存储在 node.js 变量中?
- centos - Salt pkg.installed 未正确检查安装
- javascript - 如何检查字符串是否包含字符JavaScript之间的任何空格?
- windows - 我想开始使用 kinect 进行编程。我应该从 XBOX 360 版本一还是新版本 kinect 开始?
- javascript - 如何实现条带幂等键
- java - 在sqllite数据库上多次插入相同的数据
- php - PHP 不接收来自 html 表单的数据
- java - 无法从eclipse中删除tomcat服务器
- ios - 互联网连接状态快速可达
- swift - 尽管提供了 Twitter Api,但回调 url 未获批准