Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1943050 - new userScripts API content #38073

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

rebloor
Copy link
Contributor

@rebloor rebloor commented Feb 10, 2025

Description

Addresses the documentation needs of Bug 1943050 Enable userScripts API by default by adding the documentation for the user script, API and related APIs.

  • add stubs for types and methods
  • update introductory page
    • include information about differences in enabling methods between Firefox (optional-only permission) and Chrome (activating developer mode)
  • add detail details for types
  • add detail details for methods
  • add "userScripts" to permissions with notes regarding differences between Firefox (optional only) and Chrome (install time permission)
  • add explicit cross-references to the new example extension (Add example using MV3 userScripts API webextensions-examples#576)
  • add the runtime.onUserScriptMessage and runtime.onUserScriptConnect
  • add release notes
  • add related BCD PR
  • add related Extension Workshop PR (userScripts and permission warning to request the right permissions and add cross-reference to optional-only permission section on MDN)

Related issues and pull requests

Moving old API to legacy: #37975 (comment)
Addition of related browser compatibility data:

@rebloor rebloor added the Content:WebExt WebExtensions docs label Feb 10, 2025
@rebloor rebloor requested review from dotproto and Rob--W February 10, 2025 02:00
@rebloor rebloor self-assigned this Feb 10, 2025
@github-actions github-actions bot added the size/l [PR only] 501-1000 LoC changed label Feb 10, 2025
Copy link
Contributor

github-actions bot commented Feb 10, 2025

Preview URLs (21 pages)
Flaws (11)

Note! 18 documents with no flaws that don't need to be listed. 🎉

URL: /en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime
Title: runtime
Flaw count: 1

  • macros:
    • Can't resolve /en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onserScriptConnect

URL: /en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/MessageSender
Title: runtime.MessageSender
Flaw count: 1

  • macros:
    • Can't resolve /en-US/docs/Mozilla/Add-ons/WebExtensions/API/port/sender

URL: /en-US/docs/Mozilla/Firefox/Releases/136
Title: Firefox 136 for developers
Flaw count: 9

  • broken_links:
    • /en-US/docs/Web/API/document/referrer is ill cased
    • /en-US/docs/Glossary/asynchronous is ill cased
    • /en-US/docs/Web/API/document/cookie is ill cased
    • /en-US/docs/Web/URI/Schemes/data is a redirect
  • macros:
    • Macro produces link /en-US/docs/Web/API/document/referrer which is a redirect
    • Macro produces link /en-US/docs/Glossary/asynchronous which is a redirect
    • Macro produces link /en-US/docs/Web/API/document/cookie which is a redirect
    • Macro produces link /en-US/docs/Mozilla/Add-ons/WebExtensions/API/contextMenus/update which is a redirect
    • Macro produces link /en-US/docs/Mozilla/Add-ons/WebExtensions/API/contextMenus/remove which is a redirect
External URLs (2)

URL: /en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts
Title: userScripts


URL: /en-US/docs/Mozilla/Firefox/Releases/136
Title: Firefox 136 for developers

(comment last updated: 2025-02-22 16:52:59)

@github-actions github-actions bot added size/xl [PR only] >1000 LoC changed and removed size/l [PR only] 501-1000 LoC changed labels Feb 12, 2025
@github-actions github-actions bot added the Content:Firefox Content in the Mozilla/Firefox subtree label Feb 13, 2025
@rebloor rebloor marked this pull request as ready for review February 17, 2025 16:05
@rebloor rebloor requested review from a team as code owners February 17, 2025 16:05
@rebloor rebloor requested review from pepelsbey and removed request for a team February 17, 2025 16:05
@sideshowbarker sideshowbarker removed the request for review from a team February 18, 2025 00:36
Copy link
Member

@Rob--W Rob--W left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rebase your PR on the (presumably final) state of the legacy documentation move? Then I can more easily compare the difference with this link: https://github.com/rebloor/content/compare/Bug-1943050-userScripts-API-move-legacy-content..Bug-1943050-userScripts-API-new-content


{{Compat}}

> [!NOTE]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's delete all of this copyright etc stuff (in every article related to userScripts).

The userScripts API is designed together with me in the WECG; we don't need to call out Chromium's source and license header here.

- `port`
- : {{WebExtAPIRef('runtime.Port')}}. An object connecting the current script to the other context.

## Examples
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's delete this full example. It is inaccurate and incomplete, and I'd like to get the primary doc completed without being blocked on the example.

  • Missing userScripts.configureWorld() call.
  • Uses browserAction (which is MV2-only) instead of action API (which is MV3 only).
  • Example shows bad practice of storing the port in a global variable with the expectation of the variable sticking around; this is not guaranteed in event pages/service workers (because these may suspend).


- `allFrames` {{optional_inline}}
- : `boolean`. If `allFrames` is `true`, the script is injected into all of a page's frames. By default, it's `false` and the script is only injected into the top frame.
- `id` {{optional_inline}} for {{WebExtAPIRef("userScripts.update()")}} calls, required for {{WebExtAPIRef("userScripts.register()")}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ID is always required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Copy link
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot added the merge conflicts 🚧 [PR only] label Feb 18, 2025
@github-actions github-actions bot added size/l [PR only] 501-1000 LoC changed and removed merge conflicts 🚧 [PR only] size/xl [PR only] >1000 LoC changed labels Feb 18, 2025
@rebloor rebloor requested a review from Rob--W February 19, 2025 01:42

Resets the configuration of a `USER_SCRIPT` world set by {{WebExtAPIRef("userScripts.configureWorld")}} to the defaults specified in {{WebExtAPIRef("userScripts.WorldProperties")}}. When the default world is reset, all worlds without an explicit configuration are also reset.

Any scripts injected into a reset world are moved to the default `USER_SCRIPT` world.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Any scripts injected into a reset world are moved to the default `USER_SCRIPT` world.

This is factually incorrect. The closest reasonable interpretation of this is that the default world's configuration is used upon reset, which is already stated above.


```js-nolint
const resettingWorldConfiguration = await browser.userScripts.resetWorldConfiguration(
worldId // string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
worldId // string
worldId // optional string


{{AddonSidebar}}

The configuration of a `USER_SCRIPT` execution environment.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please list all methods that uses this type: configureWorld, getWorldConfigurations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

- `worldId` {{optional_inline}}
- : `string`. The identifier for the world. Values with leading underscores (`_`) are reserved. The maximum length is 256. Defaults to the default `USER_SCRIPT` world ("").
- `csp` {{optional_inline}}
- : string`. The world's Content Security Policy (CSP). Defaults to the standard content scripts CSP, which prohibits dynamic code execution, such as` eval()`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- : string`. The world's Content Security Policy (CSP). Defaults to the standard content scripts CSP, which prohibits dynamic code execution, such as` eval()`.
- : string`. The world's Content Security Policy (CSP). Defaults to the [default CSP for content scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_Security_Policy#csp_for_content_scripts), which prohibits dynamic code execution, such as` eval()`.

Added link that explains the default CSP.

@@ -132,6 +132,7 @@ These permissions are available in Manifest V2 and above unless otherwise noted:
- `theme`
- `topSites`
- `unlimitedStorage`
- `userScripts` (This permission is [optional-only](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/optional_permissions#optional-only_permissions) in Firefox. In Chrome, the extension user must also activate [developer mode](https://developer.chrome.com/docs/extensions/reference/api/userScripts#developer_mode_for_extension_users).)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `userScripts` (This permission is [optional-only](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/optional_permissions#optional-only_permissions) in Firefox. In Chrome, the extension user must also activate [developer mode](https://developer.chrome.com/docs/extensions/reference/api/userScripts#developer_mode_for_extension_users).)
- 'userScripts' (see [userScripts permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts#permissions))

Just link to the full documentation for brevity. Those who care are better served by the full documentation.

> No optional-only permissions were generally available at the time of writing, December 2024.
The optional-only API permissions are:

- 'userScripts' (Firefox only)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to also list userScripts in the list of optional permissions above.

Optional-only is a subset of optional_permissions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@rebloor rebloor requested a review from Rob--W February 20, 2025 17:03
Copy link
Member

@Rob--W Rob--W left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am approving because the PR can be merged once my suggestions have all been applied. If in doubt, please ping me and I will offer an expedited review.


To use the API, call `{{WebExtAPIRef("userScripts.register","register()")}}` passing in an object defining the scripts to register. The method returns a Promise that is resolved with a `{{WebExtAPIRef("userScripts.RegisteredUserScript","RegisteredUserScript")}}` object.
- In Firefox, `userScripts` is an [optional-only permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/optional_permissions#optional-only_permissions) declared in the `optional_permissions` manifest key. Your extension must check that the permission has been granted by checking the availability of the `userScripts` API namespace or using {{WebExtAPIRef("permissions.contains()")}} and, if not, request it using {{WebExtAPIRef("permissions.request()")}}.
- in Chrome, `userScripts` is an install time requested permission declared in the [`permissions` manifest key](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions). However, to enable use of the API, users must [turn on the developer environment in Chrome](https://developer.chrome.com/docs/extensions/reference/api/userScripts#developer_mode_for_extension_users).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Chrome is working on a per-extension toggle independently of the Developer Mode checkbox. That implementation is tracked at https://issues.chromium.org/issues/390138269

Co-authored-by: Rob Wu <rob@robwu.nl>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:Firefox Content in the Mozilla/Firefox subtree Content:WebExt WebExtensions docs size/l [PR only] 501-1000 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants