javascript - Chrome 扩展在打开新标签后退出运行 javascript
问题描述
我正在尝试制作一个 chrome 扩展,它在一个选项卡中启动,并在打开几个新选项卡时继续运行。但是,它只启动一个新选项卡而不是指定的 5 个,因为脚本停止运行。
重要的部分:
function launch (url, name, callOnLoad) {
window.open(url, name)
.addEventListener(
"load",
callOnLoad,
{ once: true, passive: true }
);
}
async function launchProcess (taburl) {
const sleep3 = { then(resolve) { setTimeout(resolve, 250); } };
for (let i = 2; i < 7; ++i) {
launch(taburl, "tab" + i, proceed);
await sleep3;
}
}
launchProcess(taburl) //Taburl is acquired by running a chrome query to find the last active site.
整个脚本:
function proceed (evt) {
console.log("A window has been loaded");
console.log(evt);
//End of execution
}
async function launchProcess (taburl) {
const sleep3 = { then(resolve) { setTimeout(resolve, 250); } };
for (let i = 2; i < 7; ++i) {
launch(taburl, "tab" + i, proceed);
await sleep3;
}
}
async function timer(time) { //timer() is only used if the user activates it, so you can ignore it atm
const sleep = { then(resolve) { setTimeout(resolve, 15000); } };
var date = new Date();
var timestamp = date.getTime();
timestamp = timestamp + 60000
while (time > timestamp) {
date = new Date();
timestamp = date.getTime();
timestamp = timestamp + 60000
await sleep;
}
const sleep2 = { then(resolve) { setTimeout(resolve, 250); } };
var excttimestamp = new Date();
while (time > excttimestamp) {
excttimestamp = new Date();
await sleep2;
}
launchProcess().then(console.log, console.error);
}
PS:如果有人对如何使 timer() 更有效率有一些线索,非常欢迎帮助:)
我希望该功能能够“启动”多个选项卡并休眠,然后打开另一个选项卡并运行其他未提及的功能。相反,它只打开一个并退出。
解决方案
我为自己找到了一个解决方案:
chrome.tabs.create({url: 'http://www.google.com', active: false});
在 chrome 上打开一个新选项卡,但不关注它,所以弹出窗口保持不变,代码继续运行 :)
推荐阅读
- python - 在python中将二维列表制作成一维列表
- python - 使用 boto 的 SSLError
- rust - 包含期货的 Vec 的完整类型签名是什么?
- swift - 收到警告 xpression 类型“@lvalue 字符串?” 没有更多上下文是模棱两可的
- elasticsearch - 添加elasticsearch节点多机
- javascript - 坚持如何在函数中创建数组
- c# - 从 C# 调用 MySql 存储过程 - 异常“数据为 Null。无法对 Null 值调用此方法或属性。”
- python - 在 Python3 中比较后,我有没有办法从列表中访问帐户类对象
- php - 验证设置
- jqgrid - 如何在 jqGrid 搜索表单上添加 jqTree