Repository navigation
Conversation
✅ Deploy Preview for creative-fairy-df92c4 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
@aklinker1 Right now
The issue mentions deduping as a nice to have. Since WXT already uses I can either:
Happy to do whichever you prefer. |
@wxt-dev/analytics
@wxt-dev/auto-icons
@wxt-dev/browser
@wxt-dev/i18n
@wxt-dev/is-background
@wxt-dev/module-react
@wxt-dev/module-solid
@wxt-dev/module-svelte
@wxt-dev/module-vue
@wxt-dev/runner
@wxt-dev/storage
@wxt-dev/unocss
@wxt-dev/webextension-polyfill
wxt
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2288 +/- ##
==========================================
+ Coverage 79.52% 79.80% +0.27%
==========================================
Files 135 135
Lines 4054 4104 +50
Branches 944 968 +24
==========================================
+ Hits 3224 3275 +51
+ Misses 734 732 -2
- Partials 96 97 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
- Clarified error message for content scripts not registered at runtime.
- Clarify the error message for invalid `optional` content scripts without `matches`.
- Use proper type instead of const
|
The only other improvement here I would recommend is to dedupe example optional_host_permissions: [
"https://*.internal-server.com"
]This way one host gives you access to anything internal. But in our content scripts were more specific like It would be sweet if:
No big deal if not we can easily change our custom module to remove the dups if not 😃 Although I think overall this behavior would be sweet for not only optional content scripts but also regular matches too |
|
@eupthere planning on taking this on still 👀 |
|
@marcellino-ornelas Thanks for the reminder! I will update the PR on this weekend |
Per review, `registration` only controls how `matches` is added to the manifest. The new `optionalMatches` option is always added to `optional_host_permissions` and never to `content_scripts`, so a single content script can have both required and optional hosts.
`optionalMatches` are appended to any user defined `optional_host_permissions`, skipping patterns already covered by another entry. For example, `https://app.example.com/path/*` is not added when `https://*.example.com/*` exists.
…hes` hosts Without this, a shadow root UI registered on an optional host can't load its CSS file.
Adds a Registration section to the content scripts guide covering `manifest`, `runtime` and `optionalMatches`, and removes the previous `registration: 'optional'` notes.
# Conflicts: # packages/wxt/src/core/utils/manifest.ts
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
| matches: ['*://required.com/*'], | ||
| optionalMatches: ['*://optional.com/*'], |
There was a problem hiding this comment.
What if somebody put duplicated values in matches, like:
Obvious:
| matches: ['*://required.com/*'], | |
| optionalMatches: ['*://optional.com/*'], | |
| matches: ['*://required.com/*'], | |
| optionalMatches: ['*://required.com/*'], |
or
Like the way, you have in code below:
| matches: ['*://required.com/*'], | |
| optionalMatches: ['*://optional.com/*'], | |
| matches: ['https://*.other.com/*'], | |
| optionalMatches: ['https://other.com/*'], |
In both cases, optionalMatches should be omitted. because it's covered already.
Maybe it's edge case, and dev should care about that.
But that's the same conversation like before, about duplicated values in optionalMatches or matches, and i think if we've decided to dedupe it before, we should dedupe it there as well.
There was a problem hiding this comment.
Thanks for the review! Added the edge case handling at 020a3a9
| const two = optionalCs( | ||
| { | ||
| registration: 'runtime', | ||
| optionalMatches: ['https://other.com/*', 'https://*.other.com/*'], |
There was a problem hiding this comment.
Add deduping like there, also for matches.
I'm wondering if this PR is a good place, because it's related with optional, but i think yeah, let's do it there.
There was a problem hiding this comment.
Implemented at → 39c5b88 . I left out deduping the user defined manifest host_permissions for now, as it might be confusing for users to notice how their config doesn't seem to match the output. But I am open to dedupe them too if that seems more consistent.
There was a problem hiding this comment.
Implemented at → 39c5b88 . I left out deduping the user defined manifest
host_permissionsfor now, as it might be confusing for users to notice how their config doesn't seem to match the output. But I am open to dedupe them too if that seems more consistent.
Maybe i'm not understand, can you provide me some examples?
| let added: string[] = []; | ||
|
|
||
| for (const pattern of matchPatterns) { | ||
| const isCovered = [...existing, ...added].some((other) => | ||
| matchPatternCovers(other, pattern), | ||
| ); | ||
| if (isCovered) continue; | ||
|
|
||
| // Drop previously added patterns that this broader pattern covers. User | ||
| // defined entries in `existing` are never removed. | ||
| added = added.filter((other) => !matchPatternCovers(pattern, other)); | ||
| added.push(pattern); | ||
| } |
There was a problem hiding this comment.
It isn't easier to do it like that?:
| let added: string[] = []; | |
| for (const pattern of matchPatterns) { | |
| const isCovered = [...existing, ...added].some((other) => | |
| matchPatternCovers(other, pattern), | |
| ); | |
| if (isCovered) continue; | |
| // Drop previously added patterns that this broader pattern covers. User | |
| // defined entries in `existing` are never removed. | |
| added = added.filter((other) => !matchPatternCovers(pattern, other)); | |
| added.push(pattern); | |
| } | |
| const added = matchPatterns.reduce<string[]>((added, pattern) => { | |
| const allPatterns = [...existing, ...added]; | |
| const isCovered = allPatterns.some((other) => | |
| matchPatternCovers(other, pattern), | |
| ); | |
| if (isCovered) { | |
| return added; | |
| } | |
| return [ | |
| ...added.filter((other) => !matchPatternCovers(pattern, other)), | |
| pattern, | |
| ]; | |
| }, []); |
There was a problem hiding this comment.
Changed the code style as suggested at 7c7be23
`optionalMatches` covered by any content script's `matches` or by `host_permissions` are not added to `optional_host_permissions`, since access to those hosts is already declared. Also avoids writing an empty `optional_host_permissions` array when every pattern is skipped.
Content script `matches` added to `host_permissions` now use the same dedupe as `optionalMatches`: patterns covered by an existing entry or by another added pattern are skipped. User defined `host_permissions` are never removed.
| ['<all_urls>', 'https://example.com/*', true], | ||
| ['https://example.com/*', 'https://example.com/*', true], | ||
| ['*://example.com/*', 'https://example.com/*', true], | ||
| ['*://example.com/*', 'http://example.com/*', true], | ||
| ['https://example.com/*', '*://example.com/*', false], | ||
| ['https://example.com/*', 'http://example.com/*', false], | ||
| ['*://example.com/*', 'file:///example/*', false], | ||
| ['https://*/*', 'https://example.com/*', true], | ||
| ['https://*.example.com/*', 'https://example.com/*', true], | ||
| ['https://*.example.com/*', 'https://a.b.example.com/*', true], | ||
| ['https://*.example.com/*', 'https://*.a.example.com/*', true], | ||
| ['https://*.example.com/*', 'https://notexample.com/*', false], | ||
| ['https://a.example.com/*', 'https://*.example.com/*', false], | ||
| ['https://example.com/*', 'https://example.com/path/*', true], | ||
| ['https://example.com/path/*', 'https://example.com/*', false], | ||
| ['https://example.com/a*', 'https://example.com/a/b*c', true], | ||
| ['https://example.com/a.b', 'https://example.com/aXb', false], | ||
| ['https://example.com/*', '<all_urls>', false], |
There was a problem hiding this comment.
| ['<all_urls>', 'https://example.com/*', true], | |
| ['https://example.com/*', 'https://example.com/*', true], | |
| ['*://example.com/*', 'https://example.com/*', true], | |
| ['*://example.com/*', 'http://example.com/*', true], | |
| ['https://example.com/*', '*://example.com/*', false], | |
| ['https://example.com/*', 'http://example.com/*', false], | |
| ['*://example.com/*', 'file:///example/*', false], | |
| ['https://*/*', 'https://example.com/*', true], | |
| ['https://*.example.com/*', 'https://example.com/*', true], | |
| ['https://*.example.com/*', 'https://a.b.example.com/*', true], | |
| ['https://*.example.com/*', 'https://*.a.example.com/*', true], | |
| ['https://*.example.com/*', 'https://notexample.com/*', false], | |
| ['https://a.example.com/*', 'https://*.example.com/*', false], | |
| ['https://example.com/*', 'https://example.com/path/*', true], | |
| ['https://example.com/path/*', 'https://example.com/*', false], | |
| ['https://example.com/a*', 'https://example.com/a/b*c', true], | |
| ['https://example.com/a.b', 'https://example.com/aXb', false], | |
| ['https://example.com/*', '<all_urls>', false], | |
| ['*://*/*', 'https://example.com/*', true], | |
| ['<all_urls>', 'https://example.com/*', true], | |
| ['https://example.com/*', 'https://example.com/*', true], | |
| ['*://example.com/*', 'https://example.com/*', true], | |
| ['*://example.com/*', 'http://example.com/*', true], | |
| ['https://example.com/*', '*://example.com/*', false], | |
| ['https://example.com/*', 'http://example.com/*', false], | |
| ['*://example.com/*', 'file:///example/*', false], | |
| ['https://*/*', 'https://example.com/*', true], | |
| ['https://*.example.com/*', 'https://example.com/*', true], | |
| ['https://*.example.com/*', 'https://a.b.example.com/*', true], | |
| ['https://*.example.com/*', 'https://*.a.example.com/*', true], | |
| ['https://*.example.com/*', 'https://notexample.com/*', false], | |
| ['https://a.example.com/*', 'https://*.example.com/*', false], | |
| ['https://example.com/*', 'https://example.com/path/*', true], | |
| ['https://example.com/path/*', 'https://example.com/*', false], | |
| ['https://example.com/a*', 'https://example.com/a/b*c', true], | |
| ['https://example.com/a.b', 'https://example.com/aXb', false], | |
| ['https://example.com/*', '*://*/*', false], | |
| ['https://example.com/*', '<all_urls>', false], |
| const two = optionalCs( | ||
| { | ||
| registration: 'runtime', | ||
| optionalMatches: ['https://other.com/*', 'https://*.other.com/*'], |
There was a problem hiding this comment.
Implemented at → 39c5b88 . I left out deduping the user defined manifest
host_permissionsfor now, as it might be confusing for users to notice how their config doesn't seem to match the output. But I am open to dedupe them too if that seems more consistent.
Maybe i'm not understand, can you provide me some examples?
| // Drop previously kept patterns that this broader pattern covers. Patterns | ||
| // in `coveredBy` are never removed. |
There was a problem hiding this comment.
| // Drop previously kept patterns that this broader pattern covers. Patterns | |
| // in `coveredBy` are never removed. | |
| // Drop previously kept patterns that this broader pattern covers. | |
| // Patterns in `coveredBy` are never removed. |
|
@eupthere Very good work, a couple more small things 😄 |
|
@eupthere Are you want to go on with that? |
Overview
Adds support for
registration: "optional"for content scripts so host match patterns are treated as optional origins instead of required host permissions.Related Docs:
MDN optional_host_permissions
Chrome Extensions Docs MV2 - Optional Permissions
What changed:
"optional".registration: "optional"scripts fromcontent_scripts(same dynamic-registration model as runtime scripts).matchesintooptional_host_permissionsinstead ofhost_permissions.optional_host_permissionsare merged intooptional_permissionswhen targeting MV2."optional"follows runtime-style content script reload flow.matches"optional"registration mode and describe intended optional-host runtime usage.Please review this:
I intentionally kept
registration: 'runtime'as the only mode that can omit matches, to preserve manual injection flows (for examplebrowser.scripting.executeScript({ target: { tabId } ... })) where URL match patterns are not needed in entrypoint options.registration: "optional"requires matches because this mode derives origin scope from matches to populateoptional_host_permissions(and MV2 conversion tooptional_permissions).Could you confirm whether this assumption about the original runtime exception is correct, and whether keeping optional stricter is the intended design?
Manual Testing
wxt core test pass
bun run --filter wxt test runGenerated Manifest
cd packages/wxt-demo npm run build:chrome-mv2 npm run build:chrome-mv3MV2
{ "manifest_version": 2, "name": "wxt-demo", "version": "1.0.0", "icons": { "16": "icons/16.png", "32": "icons/32.png", "48": "icons/48.png", "128": "icons/128.png" }, "permissions": ["storage"], "default_locale": "en", "web_accessible_resources": [ "iframe-src.html", "unlisted.js", "content-scripts/ui.css" ], "background": { "scripts": ["background.js"] }, "browser_action": { "default_title": "Popup", "default_popup": "popup.html" }, "options_ui": { "open_in_tab": false, "page": "options.html" }, "sandbox": { "pages": ["example.html", "sandbox.html"] }, "content_scripts": [ { "matches": ["https://*.duckduckgo.com/*"], "css": ["content-scripts/automount.css"], "js": ["content-scripts/automount.js", "content-scripts/ui.js"] }, { "matches": ["<all_urls>"], "js": ["content-scripts/example-tsx.js"] }, { "matches": ["*://*.google.com/*"], "js": ["content-scripts/iframe.js"] }, { "matches": ["*://*.crunchyroll.com/*"], "js": ["content-scripts/location-change.js"] }, { "matches": ["*://*/*"], "js": ["content-scripts/main-world.js"], "world": "MAIN" } ], "optional_permissions": ["https://example.com/*"] }MV3
{ "manifest_version": 3, "name": "wxt-demo", "version": "1.0.0", "icons": { "16": "icons/16.png", "32": "icons/32.png", "48": "icons/48.png", "128": "icons/128.png" }, "permissions": ["storage", "sidePanel"], "default_locale": "en", "web_accessible_resources": [ { "resources": ["iframe-src.html", "unlisted.js"], "matches": ["*://*.google.com/*", "*://*.example.com/*"] }, { "resources": ["content-scripts/ui.css"], "use_dynamic_url": true, "matches": ["https://*.duckduckgo.com/*"] } ], "background": { "service_worker": "background.js" }, "action": { "default_title": "Popup", "default_popup": "popup.html" }, "options_ui": { "open_in_tab": false, "page": "options.html" }, "sandbox": { "pages": ["example.html", "sandbox.html"] }, "side_panel": { "default_path": "sidepanel.html" }, "content_scripts": [ { "matches": ["https://*.duckduckgo.com/*"], "css": ["content-scripts/automount.css"], "js": ["content-scripts/automount.js", "content-scripts/ui.js"] }, { "matches": ["<all_urls>"], "js": ["content-scripts/example-tsx.js"] }, { "matches": ["*://*.google.com/*"], "js": ["content-scripts/iframe.js"] }, { "matches": ["*://*.crunchyroll.com/*"], "js": ["content-scripts/location-change.js"] }, { "matches": ["*://*/*"], "js": ["content-scripts/main-world.js"], "world": "MAIN" } ], "optional_host_permissions": ["https://example.com/*"] }Related Issue
This PR closes #2239