Define order of execution for registered userScripts; currently inconsistent
Categories
(WebExtensions :: General, enhancement, P3)
Tracking
(Not tracked)
People
(Reporter: robwu, Unassigned)
References
(Blocks 1 open bug)
Details
(Whiteboard: [wecg])
Bug 1792685 improved the guaranteed ordering of statically defined content scripts as defined in manifest.json. The bug did not specifically cover dynamic content script registration, but in practice the order is static content scripts, followed by dynamic content scripts and user scripts. And what bug 1792685 did guarantee, even for these, is that the order of internal registration is now also the order of execution.
The defined order of execution in the user scripts is specified in the WECG as follows:
When multiple scripts match and have the same runAt schedule, the execution order is:
- Scripts registered via the content_scripts key in the manifest file
- Scripts registered via scripting.registerContentScripts(), following the order they were registered in. Updating a content script doesn't change its registration order.
- Scripts registered via userScripts.register(), following the order they were registered in. Updating a user script doesn’t change its registration order.
In practice, Firefox behavior is as follows:
- When registered at runtime, the order is whatever was registered last (registerNewScripts in parent and "Extension:RegisterContentScripts" in child).
- When updated at runtime, the order depends:
- In existing processes, it is whatever was registered/updated last (updateScripts in parent and "Extension:UpdateContentScripts" in child).
- In new processes, the order of registration (independent of updates), because the execution order is the order in the internal
registeredContentScripts
map, whose order does not change by updating.
- When the browser restarts and updates, the order is in lexicographical order (
initializeFromDatabase
).- This does not match the letter of the spec text above, but some extension devs prefer that: https://github.com/w3c/webextensions/issues/606
What Chrome does is:
- execution order is in lexicographical order; write-up of their implementation is at https://bugzilla.mozilla.org/show_bug.cgi?id=1792685#c16; for dynamic user scripts / content scripts
AddPrefixToDynamicScriptId
is also relevant (withscript_id
being the extension-defined ID). - https://issues.chromium.org/issues/337078958 is "User scripts appear to be injected based on ID, rather than registration order", where an extension developer expressed concern on the inability to reorder scripts if the order were to be based on registration order.
Our current order is inconsistent. We should align across browsers what the order really should be, and implement that.
Reporter | ||
Updated•4 months ago
|
Description
•