Skip to content
New issue

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

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

Already on GitHub? # to your account

Bug 1943050 - new userScripts API content #38073

Open
wants to merge 17 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 (12)

Note! 17 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/Add-ons/WebExtensions/API/userScripts/UserScriptFilter
Title: userScripts.UserScriptFilter
Flaw count: 1

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

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 (3)

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


URL: /en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions
Title: permissions


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

(comment last updated: 2025-02-19 00:51:21)

@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).

Fired when a connection is made with a user script from one of the extension's [`USER_SCRIPT` worlds](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/ExecutionWorld).

A user script can only connect and then sent messages from a `USER_SCRIPT` world that is configured by {{WebExtAPIRef('userScripts.configureWorld()')}} with `messaging` set to `true`.

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 add a note that this event requires the "userScripts" permission (in Firefox). In Chrome, the onUserScriptConnect and onUserScriptMessage events are always present, even in extensions that don't declare the userScripts permission, even if there is no way for that event to fire.

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

Use this event to listen for messages sent from one of the extension's [`USER_SCRIPT` worlds](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/ExecutionWorld).

A user script can only send messages using {{WebExtAPIRef('runtime.sendMessage')}} from a `USER_SCRIPT` world that is configured by {{WebExtAPIRef('userScripts.configureWorld()')}} with `messaging` set to `true`.

Copy link
Member

Choose a reason for hiding this comment

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

(already mentioned before; adding for completeness)

Let's add a note that this event requires the "userScripts" permission (in Firefox). In Chrome, the onUserScriptConnect and onUserScriptMessage events are always present, even in extensions that don't declare the userScripts permission, even if there is no way for that event to fire.

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


- `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

- `world` {{optional_inline}}
- : {{WebExtAPIRef("userScripts.ExecutionWorld")}}. The execution environment to use to run the scripts. Defaults to "USER_SCRIPT".
- `worldId` {{optional_inline}}
- : `string`. ID of a user script world execute the script in. Only valid if `world` is `USER_SCRIPT` or omitted. If `worldId` is omitted, the script is execute in the default `USER_SCRIPT` world (""). Values with leading underscores (`_`) are reserved. The maximum length is 256 characters.
Copy link
Member

Choose a reason for hiding this comment

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

Refer to configureWorld for ease of discovery of what world configurations are.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Rob--W don't follow this feedback. Isn't the main reference for worlds on the API's root page. Or did you mean a reference to 'getWorldConfigurations'?

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
Co-authored-by: Rob Wu <rob@robwu.nl>
- `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
Contributor

Choose a reason for hiding this comment

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

[mdn-linter] reported by reviewdog 🐶

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 standard content scripts CSP, which prohibits dynamic code execution, such as`eval()`.

@rebloor rebloor requested a review from Rob--W February 19, 2025 01:42
# for free to join this conversation on GitHub. Already have an account? # 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