You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/user-scripts-api.md
+55-1Lines changed: 55 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -52,9 +52,12 @@ User scripting related features will be exposed in a new API namespace, tentativ
52
52
#### Types
53
53
54
54
```
55
+
// See RegisteredUserScript validation section, below.
55
56
dictionary RegisteredUserScript {
56
57
boolean? allFrames;
57
-
ScriptSource[] js;
58
+
// js is required in userScripts.register(), optional in userScripts.update().
59
+
// When specified, must be a non-empty array.
60
+
ScriptSource[]? js;
58
61
string[]? excludeMatches;
59
62
string id;
60
63
string[]? matches;
@@ -133,6 +136,8 @@ where
133
136
134
137
In the future, if we allow multiple user script worlds (see section in Future Work below), this method can be expanded to allow for a user script world identifier to customize a single user script world.
135
138
139
+
The proposal at [multiple_user_script_worlds.md](multiple_user_script_worlds.md) expands the behavior of `userScripts.configureWorld`.
140
+
136
141
##### Messaging
137
142
138
143
User scripts can send messages to the extension using extension messaging APIs: `browser.runtime.sendMessage()` and `browser.runtime.connect()`. We leverage the runtime API (instead of introducing new userScripts.onMessage- and userScripts.sendMessage-style values) in order to keep extension messaging in the same API. There is precedent in this (using the same API namespace to send messages from a different (and less trusted) context, as `chrome.runtime` is also the API used to send messages from web pages.
@@ -157,11 +162,56 @@ As mentioned in requirement A, the user script world can communicate with differ
157
162
- Scripts registered via [`scripting.registerContentScripts()`](https://developer.chrome.com/docs/extensions/reference/scripting/#method-registerContentScripts), following the order they were registered in. Updating a content script doesn't change its registration order.
158
163
- Scripts registered via `userScripts.register()`, following the order they were registered in. Updating a user script doesn’t change its registration order.
159
164
- User scripts are always persisted across sessions, since the opposite behavior would be uncommon. (We may explore providing an option to customize this in the future.)
165
+
- Unlike regular content scripts, `matches` is allowed to be optional when `includeGlobs` is specified. A user script matches a document when its URL matches either `matches` or `includeGlobs`.
166
+
167
+
### RegisteredUserScript validation
168
+
169
+
The `RegisteredUserScript` type is shared by `userScripts.register()` and
170
+
`userScripts.update()`. All fields except `id` are declared as optional, to
171
+
allow `userScripts.update()` to update individual properties.
172
+
173
+
#### Requirements per method
174
+
175
+
`userScripts.register()`:
176
+
177
+
-`js` must be present and a non-empty array.
178
+
- At least one of `matches` or `includeGlobs` must be a non-empty array.
179
+
180
+
`userScripts.update()`:
181
+
182
+
- Individual properties may be `null` or omitted to leave the value unchanged.
183
+
- To clear an array, an empty array can be passed.
184
+
- The resulting script must be validated to make sure that the updated
185
+
script remains a valid script before it replaces a previous script.
186
+
187
+
#### Example
188
+
189
+
```javascript
190
+
// Valid registration:
191
+
awaitbrowser.userScripts.register([
192
+
{
193
+
worldId:"myScriptId",
194
+
js: [{ code:"console.log('Hello world!');" }],
195
+
matches: ["*://example.com/*"],
196
+
},
197
+
]);
198
+
199
+
// Invalid! Would result in script without matches or includeGlobs!
From here, each browser vendor should be able to implement their own restrictions. Chrome is exploring limiting the access to this API when the user has enabled developer mode (bug), but permission grants are outside of the scope of this API proposal.
164
212
213
+
Firefox restricts the permission to `optional_permissions` only, which means that the permission is not granted at install time, and has to be requested separately through browser UI or the `permissions.request()` API ([Firefox bug 1917000](https://bugzilla.mozilla.org/show_bug.cgi?id=1917000)).
214
+
165
215
## (Potential) Future Enhancements
166
216
167
217
### `USER_SCRIPT`/ `ISOLATED` World Communication
@@ -172,10 +222,14 @@ In the future, we may want to provide a more straightforward path for communicat
172
222
173
223
In addition to specifying the execution world of `USER_SCRIPT`, we could allow extensions to inject in unique worlds by providing an identifier. Scripts injected with the same identifier would inject in the same world, while scripts with different world identifiers inject in different worlds. This would allow for greater isolation between user scripts (if, for instance, the user had multiple unrelated user scripts injecting on the same page).
174
224
225
+
This proposal is at [multiple_user_script_worlds.md](multiple_user_script_worlds.md).
226
+
175
227
### Execute user scripts one time
176
228
177
229
Currently, user scripts are registered and executed every time it matches the origin in a persistent way. We may explore a way to execute a user script only one time to provide a new capability to user scripts (e.g `browser.userScripts.execute()`).
178
230
231
+
This proposal is at [user-scripts-execute-api.md](user-scripts-execute-api.md).
232
+
179
233
### Establish common behaviors for the CSP of scripts injected into the main world by an extension
180
234
181
235
Create certain HTML elements even if their src, href or contents violates CSP of the page so that the users don't have to nuke the site's CSP header altogether.
0 commit comments