简体   繁体   English

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

[英]Chrome extension quits running javascript after opening new tab

I am trying to make a chrome extensions, that is started in one tab and continues to run while opening several new tabs. 我正在尝试创建一个Chrome扩展程序,该扩展程序在一个标签中启动,并在打开多个新标签时继续运行。 However, it only starts ONE new tab instead of the 5 specified because the script stops running. 但是,由于脚本停止运行,因此它仅启动一个新标签,而不是指定的5个新标签。

The important part: 重要部分:

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.

The whole script: 整个脚本:



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: If someone has some clues on how to make timer() more efficient, help is very welcomed :) PS:如果有人对如何提高timer()的效率有一些了解,欢迎您提供帮助:)

I expect the Function to "launch" multiple tabs and sleep, then opening another tab and running other not mentioned functions. 我希望该功能“启动”多个选项卡并进入睡眠状态,然后打开另一个选项卡并运行其他未提及的功能。 Instead it only opens one and quits. 相反,它仅打开一个并退出。

I found a solution for myself: chrome.tabs.create({url: 'http://www.google.com', active: false}); 我为自己找到了一个解决方案: chrome.tabs.create({url: 'http://www.google.com', active: false}); opens a new tab over chrome but doesn't focus on it, so the popup stays and the code keeps running :) 在chrome上打开一个新标签,但不关注该标签,因此弹出框保持不变,代码继续运行:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM