violentmonkey icon indicating copy to clipboard operation
violentmonkey copied to clipboard

[Feature] default to top-level-await

Open scarf005 opened this issue 5 months ago • 2 comments

Motivation

while it's possible via // @top-level-await (#833), i think this behavior could be default.

Proposed Solution

make this flag the default.

Use Cases

  • it's convenient to await web APIs.
const isIncognito = await navigator.storage.getDirectory().then(() => false).catch(() => true)

(code from https://github.com/Joe12387/detectIncognito/issues/51)

  • less surprises to people new to userscript that expects valid JS syntax to work

scarf005 avatar Jul 19 '25 08:07 scarf005

I disagree because it's not enabled by default, this is not "valid JS syntax" out of the box. One must explicitly declare a module in order to enable it, so declaring @top-level-await serves the same purpose.

tophf avatar Jul 19 '25 09:07 tophf

Another problem is that the browser.userScripts API doesn't allow registering a module, meaning that top-level await won't work for userscripts registered in their own isolated JS world that can be done without the intermediate wrapper added by the extension, the way it's been done forever in Firefox and recently implemented in Chrome's MV3.

tophf avatar Jul 19 '25 09:07 tophf