1

I am trying to make a chrome extensions, that is started in one tab and continues to run while opening several new tabs. However, it only starts ONE new tab instead of the 5 specified because the script stops running.

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 :)

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.

Ranger
  • 75
  • 7
  • The script is restricted to the file it is created in, you cannot pass code to new tabs – Andrew Daly Aug 01 '19 at 15:11
  • Of course the script is restricted to the file it is created in, I don't want to pass code to new tabs, the only thing I want to have is that the extension continues to run if the tab is changing and the popup is closed... If I run the script in Console, it runs perfectly fine and finishes all it's duties – Ranger Aug 01 '19 at 15:29

1 Answers1

2

I found a solution for myself: 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 :)

Ranger
  • 75
  • 7
  • However, I don't quite know how to implement .addEventListener in this scenario... Could anyone help me out? – Ranger Aug 01 '19 at 16:32
  • Use chrome.tabs.onUpdated ([example](https://stackoverflow.com/a/44864966)) or chrome.webNavigation.onCompleted ([example](https://stackoverflow.com/a/57315200)). – wOxxOm Aug 01 '19 at 18:47
  • @wOxxOm Okay... I'm not quite sure if there's a better method than just posting messages to the popup script from background page – Ranger Aug 03 '19 at 11:41
  • Also, I don't know how to access the content script stored in the new opened Tabs... Tab Class from Chrome doesn't seem to have something like currentTarget where you can directly call functions – Ranger Aug 03 '19 at 11:42
  • The content script runs in a web page which is one process. The popup/background script runs in an extension page which is another process. You can use [messaging](https://developer.chrome.com/extensions/messaging) to communicate between different processes. You definitely can't invoke functions directly from another process. – wOxxOm Aug 03 '19 at 12:01