Skip to content

Commit d2f488f

Browse files
chrstinalinRob--W
authored andcommitted
Bug 1679997 - Add cssOrigin to declarative contentScripts API. r=robwu.
Differential Revision: https://phabricator.services.mozilla.com/D251173
1 parent f94bd7e commit d2f488f

20 files changed

+432
-4
lines changed

dom/chrome-webidl/WebExtensionContentScript.webidl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ enum ContentScriptExecutionWorld {
182182
"USER_SCRIPT",
183183
};
184184

185+
enum ContentScriptCssOrigin {
186+
"author",
187+
"user",
188+
};
189+
185190
[ChromeOnly, Exposed=Window]
186191
interface WebExtensionContentScript : MozDocumentMatcher {
187192
[Throws]
@@ -212,6 +217,12 @@ interface WebExtensionContentScript : MozDocumentMatcher {
212217
[Constant]
213218
readonly attribute DOMString? worldId;
214219

220+
/**
221+
* The css origin of the stylesheet to inject.
222+
*/
223+
[Constant]
224+
readonly attribute ContentScriptCssOrigin cssOrigin;
225+
215226
/**
216227
* A set of paths, relative to the extension root, of CSS sheets to inject
217228
* into matching pages.
@@ -234,6 +245,8 @@ dictionary WebExtensionContentScriptInit : MozDocumentMatcherInit {
234245

235246
DOMString? worldId = null;
236247

248+
ContentScriptCssOrigin cssOrigin = "author";
249+
237250
sequence<DOMString> cssPaths = [];
238251

239252
sequence<DOMString> jsPaths = [];

toolkit/components/extensions/Extension.sys.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,7 @@ export class ExtensionData {
21632163

21642164
jsPaths: options.js || [],
21652165
cssPaths: options.css || [],
2166+
cssOrigin: options.css_origin,
21662167
});
21672168
}
21682169

toolkit/components/extensions/ExtensionContent.sys.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ class Script {
360360
* execution details. This is usually a plain WebExtensionContentScript
361361
* except when the script is run via `tabs.executeScript` or
362362
* `scripting.executeScript`. In this case, the object may have some
363-
* extra properties: wantReturnValue, removeCSS, cssOrigin
363+
* extra properties: wantReturnValue, removeCSS
364364
*/
365365
constructor(extension, matcher) {
366366
this.scriptType = "content_script";
@@ -1455,7 +1455,6 @@ export var ExtensionContent = {
14551455
Object.assign(matcher, {
14561456
wantReturnValue: options.wantReturnValue,
14571457
removeCSS: options.removeCSS,
1458-
cssOrigin: options.cssOrigin,
14591458
});
14601459
let script = contentScripts.get(matcher);
14611460

toolkit/components/extensions/ExtensionScriptingStore.sys.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export const makeInternalContentScript = (
179179
// upfront.
180180
checkPermissions: true,
181181
cssPaths,
182+
cssOrigin: options.cssOrigin || "author",
182183
excludeMatches: options.excludeMatches,
183184
jsPaths,
184185
matches: options.matches,
@@ -214,6 +215,7 @@ export const makePublicContentScript = (extension, internalScript) => {
214215
runAt: internalScript.runAt,
215216
world: internalScript.world,
216217
persistAcrossSessions: internalScript.persistAcrossSessions,
218+
cssOrigin: internalScript.cssOrigin,
217219
};
218220

219221
if (internalScript.cssPaths.length) {

toolkit/components/extensions/Schemas.sys.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ class Context {
445445
localize(value) {
446446
return value;
447447
},
448+
stringToLowerCase(value) {
449+
return value.toLowerCase();
450+
},
448451
...params.preprocessors,
449452
};
450453

toolkit/components/extensions/WebExtensionContentScript.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,15 @@ class WebExtensionContentScript final : public MozDocumentMatcher {
193193
public:
194194
using RunAtEnum = dom::ContentScriptRunAt;
195195
using ExecutionWorld = dom::ContentScriptExecutionWorld;
196+
using CSSOrigin = dom::ContentScriptCssOrigin;
196197

197198
static already_AddRefed<WebExtensionContentScript> Constructor(
198199
dom::GlobalObject& aGlobal, WebExtensionPolicy& aExtension,
199200
const ContentScriptInit& aInit, ErrorResult& aRv);
200201

201202
RunAtEnum RunAt() const { return mRunAt; }
202203
ExecutionWorld World() const { return mWorld; }
204+
CSSOrigin CssOrigin() const { return mCssOrigin; }
203205
void GetWorldId(nsAString& aWorldId) const;
204206

205207
void GetCssPaths(nsTArray<nsString>& aPaths) const {
@@ -228,6 +230,7 @@ class WebExtensionContentScript final : public MozDocumentMatcher {
228230
RunAtEnum mRunAt;
229231
ExecutionWorld mWorld;
230232
Nullable<nsString> mWorldId;
233+
CSSOrigin mCssOrigin;
231234
};
232235

233236
} // namespace extensions

toolkit/components/extensions/WebExtensionPolicy.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,8 @@ WebExtensionContentScript::WebExtensionContentScript(
765765
aRv),
766766
mRunAt(aInit.mRunAt),
767767
mWorld(aInit.mWorld),
768-
mWorldId(aInit.mWorldId) {
768+
mWorldId(aInit.mWorldId),
769+
mCssOrigin(aInit.mCssOrigin) {
769770
mCssPaths.Assign(aInit.mCssPaths);
770771
mJsPaths.Assign(aInit.mJsPaths);
771772
mExtension = &aExtension;

toolkit/components/extensions/parent/ext-contentScripts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class ContentScriptParent {
9898
jsPaths: [],
9999
cssPaths: [],
100100
originAttributesPatterns: null,
101+
cssOrigin: details.cssOrigin || "author",
101102
};
102103

103104
if (details.cookieStoreId != null) {

toolkit/components/extensions/schemas/content_scripts.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
"optional": true,
6868
"description": "The JavaScript world for a script to execute within. Defaults to \"ISOLATED\"."
6969
},
70+
"cssOrigin": {
71+
"$ref": "extensionTypes.CSSOrigin",
72+
"optional": true,
73+
"description": "The css origin of the stylesheet to inject. Defaults to \"author\"."
74+
},
7075
"cookieStoreId": {
7176
"choices": [
7277
{

toolkit/components/extensions/schemas/extension_types.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
"id": "CSSOrigin",
6666
"type": "string",
6767
"enum": ["user", "author"],
68-
"description": "The origin of the CSS to inject, this affects the cascading order (priority) of the stylesheet."
68+
"description": "The origin of the CSS to inject, this affects the cascading order (priority) of the stylesheet.",
69+
"preprocess": "stringToLowerCase"
6970
},
7071
{
7172
"id": "InjectDetails",
@@ -107,6 +108,7 @@
107108
"cssOrigin": {
108109
"$ref": "CSSOrigin",
109110
"optional": true,
111+
"default": "author",
110112
"description": "The css origin of the stylesheet to inject. Defaults to \"author\"."
111113
}
112114
}

0 commit comments

Comments
 (0)