javascript - 打开新标签页后 Chrome 扩展程序退出运行 javascript

标签 javascript google-chrome google-chrome-extension

我正在尝试制作一个 Chrome 扩展,它在一个选项卡中启动,并在打开几个新选项卡时继续运行。但是,它仅启动 1 个新选项卡,而不是指定的 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 上打开一个新选项卡,但不关注它,因此弹出窗口保持不变,代码继续运行:)

关于javascript - 打开新标签页后 Chrome 扩展程序退出运行 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57312000/

相关文章:

javascript - gulp -如果。如何检查js文件是否包含特定属性?

javascript - Chrome 扩展 -executeScript 不在事件选项卡中

google-chrome-extension - 如何打开 chrome ://extensions/

javascript - 收到响应后使用 Chrome webRequest 进行 URL 转发

javascript - Chrome 扩展 : Replace HTML before loading the page

javascript - html5-canvas 在线移动对象

javascript - wordpress:如何使用 JS 更改动态生成的 div 的 innerHTML?错误: 'cannot set property innerHTMl of null'

html - 在使用 Chrome 时发现 HTML 中是否缺少关闭标记?

javascript - Jquery val 长度不起作用

css - 为什么 chrome 会从边框宽度中减去小于 .1px 的宽度?