Is code passed to sequential chrome.tabs.executeScript calls guaranteed to run in order?

StackOverflow https://stackoverflow.com/questions/23641873

  •  22-07-2023
  •  | 
  •  

Pergunta

In my tests, code always seems to execute in a target tab in the order of the chrome.tabs.executeScript calls. For example, I see the numbers 0..99 logged in order in the target tab’s console when I run this:

for (var i = 0; i < 100; i++) {
  chrome.tabs.executeScript(150, {code: 'console.log(' + i + ')'}, function () {});
}

I’m wondering if I can rely on that ordering behavior. If so, I can request 10 script executions all at once by synchronously calling chrome.tabs.executeScript 10 times — even if some of the later scripts depend on some of the earlier scripts.

The alternative is slower: requesting that the next script be executed only after the current script is done (i.e. from its callback). A second drawback of this approach is that other event handling in my extension (e.g. handling messages from content scripts just injected into the tab) can be interleaved with the content script execution, slowing it down even further.

Foi útil?

Solução

The relative execution order of chrome.tabs.executeScript is not documented, but it extremely likely that you can rely on its order.

Behind the scenes, executeScript does the following:

  1. The JavaScript code invokes native code on the UI thread (source code).
  2. If the extension is allowed to execute code in the tab, the UI thread sends the message with execution details to the renderer process over IPC (source code, source code).
  3. When the renderer receives the message (source code), it executes the code in the web page (source code). When the script has finished executing in all pages, the renderer sends a message back to the extension process (source code).
  4. The message is received by the extension process, and the callback (JavaScript) is invoked (source code).

Note that everything in this flow is synchronized: JavaScript is single-threaded, so the code is synchronously processed. The native code that handles the call runs synchronously as well, and the messages in IPC are also synchronized.

chrome.tabs.executeScript is asynchronous in the sense that it does not block the extension process until the code in the tab has finished (i.e. after calling chrome.tabs.executeScript, it immediately returns, ready to execute the next statement in your code). That is nice, because you don't want that your extension process gets frozen because of an alert dialog in the executed content script.

This answer refers to code at trunk (Chrome 36), but I believe that it will remain accurate in the future, because there is no reason to alter the implementation that intentionally mixes the relative order of the messages. One of the most realistic ways of mixing up messages is to make native code asynchronous and have different execution paths (I reckon that this does not happen).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top