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

Specifying css origin as USER in declarative/registered content scripts #414

Open
tophf opened this issue Jul 2, 2023 · 4 comments
Open
Labels
enhancement Enhancement or change to an existing feature implemented: safari Implemented in Safari proposal Proposal for a change or new feature supportive: chrome Supportive from Chrome supportive: firefox Supportive from Firefox

Comments

@tophf
Copy link

tophf commented Jul 2, 2023

There should be a way to specify origin: 'USER' of CSS content scripts that are 1) declared in manifest.json, 2) registered via chrome.scripting.registerContentScripts.

It would align the API with the existing world parameter for js content scripts.

Why is it important? Because the USER origin is the only way to override !important in inline style attributes of a web page. Currently we can only use chrome.scripting.insertCSS for that, but it's not declarative so it requires observing navigation, which is inefficient as it wakes up the service worker frequently, it may also easily cause a flash of unstyled content if the page loaded before the SW started and the browser processed the API call.

@xeenon xeenon added enhancement Enhancement or change to an existing feature proposal Proposal for a change or new feature and removed needs-triage labels Jul 6, 2023
@xeenon
Copy link
Collaborator

xeenon commented Jul 6, 2023

Extensions styles are already user level in Safari. I am supportive of having a way to change between user and author.

@xeenon xeenon added the supportive: safari Supportive from Safari label Jul 6, 2023
@Rob--W Rob--W added supportive: chrome Supportive from Chrome supportive: firefox Supportive from Firefox labels Jul 6, 2023
@Rob--W
Copy link
Member

Rob--W commented Jul 6, 2023

Firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1679997

While there is consensus on the capability, I believe that we haven't talked about the exact shape of the API yet.

The tabs.insertCSS API, which is the origin of the feature, takes a cssOrigin property with the values "author" or "user": https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/insertCSS#cssorigin

The scripting.insertCSS API has renamed the name to just origin with the values "AUTHOR" or "USER" (upper case): https://developer.chrome.com/docs/extensions/reference/scripting/#property-CSSInjection-origin

Because this feature makes sense in manifest.json and scripting.registerContentScripts, just "origin" would be confusing, so I propose to stick with the cssOrigin name.

Since the scripting namespace is already using the upper case variants of the value, I would continue that practice in the improved API.

@xeenon
Copy link
Collaborator

xeenon commented May 2, 2024

I plan to add support for css_origin with author and user values from the manifest.json in Safari / WebKit. I went with snake case for the key and lowercase for the values for consistency. And cssOrigin to scripting.registerContentScripts() with author and user values (case-insensitive since scripting already has AUTHOR and USER values for the origin key of insertCSS().) See #563 for discussion on case.

@xeenon
Copy link
Collaborator

xeenon commented May 3, 2024

WebKit issue tracking this: https://bugs.webkit.org/show_bug.cgi?id=273122

@xeenon xeenon added implemented: safari Implemented in Safari and removed supportive: safari Supportive from Safari labels May 3, 2024
webkit-commit-queue pushed a commit to xeenon/WebKit that referenced this issue May 5, 2024
https://webkit.org/b/273122
rdar://problem/126916972

Reviewed by Antti Koivisto.

Change web extension content styles to be author level instead of user level by default.

To allow extensions to still use user level, added support for `cssOrigin` in the
`tabs.insertCSS()` options, as well as `origin` in the `scripting.insertCSS` options.
Accepted values are `user` and `author`.

In addition, this introduces `cssOrigin` in `scripting.registerContentScripts()` and
`css_origin` for the `content_scripts` entry in the manifest. These are new options
and are being discussed in the WECG at w3c/webextensions#414.

* Source/WebCore/en.lproj/Localizable.strings: Updated.
* Source/WebKit/Shared/Extensions/WebExtensionDynamicScripts.serialization.in:
* Source/WebKit/Shared/Extensions/WebExtensionRegisteredScriptParameters.h:
* Source/WebKit/Shared/Extensions/WebExtensionScriptInjectionParameters.h:
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIScriptingCocoa.mm:
(WebKit::WebExtensionContext::scriptingInsertCSS):
(WebKit::WebExtensionContext::createInjectedContentForScripts):
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPITabsCocoa.mm:
(WebKit::WebExtensionContext::tabsInsertCSS):
* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionCocoa.mm:
(WebKit::WebExtension::populateContentScriptPropertiesIfNeeded):
* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionContextCocoa.mm:
(WebKit::WebExtensionContext::addInjectedContent):
* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionDynamicScriptsCocoa.mm:
(WebKit::WebExtensionDynamicScripts::injectStyleSheets):
* Source/WebKit/UIProcess/Extensions/WebExtension.h:
* Source/WebKit/UIProcess/Extensions/WebExtensionDynamicScripts.h:
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIScriptingCocoa.mm:
(WebKit::toWebAPI): Added origin.
(WebKit::WebExtensionAPIScripting::executeScript): Return early if outExceptionString is set.
(WebKit::WebExtensionAPIScripting::insertCSS): Ditto.
(WebKit::WebExtensionAPIScripting::removeCSS): Ditto.
(WebKit::WebExtensionAPIScripting::validateCSS): Added origin.
(WebKit::WebExtensionAPIScripting::validateRegisteredScripts): Ditto.
(WebKit::WebExtensionAPIScripting::parseCSSInjectionOptions): Ditto.
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPITabsCocoa.mm:
(WebKit::WebExtensionAPITabs::parseScriptOptions):
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtension.mm:
(TestWebKitAPI::TEST(WKWebExtension, ContentScriptsParsing)): Added new cases.
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionAPIScripting.mm:
(TestWebKitAPI::TEST(WKWebExtensionAPIScripting, ErrorsCSS)): Added new error case.
(TestWebKitAPI::TEST(WKWebExtensionAPIScripting, ErrorsRegisteredContentScript)): Ditto.
(TestWebKitAPI::TEST(WKWebExtensionAPIScripting, CSSUserOrigin)): Added.
(TestWebKitAPI::TEST(WKWebExtensionAPIScripting, CSSAuthorOrigin)): Added.
(TestWebKitAPI::TEST(WKWebExtensionAPIScripting, World)): Fixed test that was always passing.
(TestWebKitAPI::TEST(WKWebExtensionAPIScripting, RegisterContentScriptsWithCSSUserOrigin)): Added.
(TestWebKitAPI::TEST(WKWebExtensionAPIScripting, RegisterContentScriptsWithCSSAuthorOrigin)): Added.
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionAPITabs.mm:
(TestWebKitAPI::TEST(WKWebExtensionAPITabs, Errors)): Added new error case.
(TestWebKitAPI::TEST(WKWebExtensionAPITabs, CSSUserOrigin)): Added.
(TestWebKitAPI::TEST(WKWebExtensionAPITabs, CSSAuthorOrigin)): Added.
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionContext.mm:
(TestWebKitAPI::TEST(WKWebExtensionContext, ContentScriptsParsing)): Added new cases.
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionController.mm:
(TestWebKitAPI::TEST(WKWebExtensionController, CSSUserOrigin)): Added.
(TestWebKitAPI::TEST(WKWebExtensionController, CSSAuthorOrigin)): Added.

Canonical link: https://commits.webkit.org/278387@main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement or change to an existing feature implemented: safari Implemented in Safari proposal Proposal for a change or new feature supportive: chrome Supportive from Chrome supportive: firefox Supportive from Firefox
Projects
None yet
Development

No branches or pull requests

3 participants