-
-
Notifications
You must be signed in to change notification settings - Fork 417
Content script context event #1778
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
Draft
sleekslush
wants to merge
6
commits into
wxt-dev:main
Choose a base branch
from
sleekslush:content-script-context-event
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ed88d95
Unregisters listeners when invalid
sleekslush 2d30f75
Explicit invalidate handler
sleekslush 3101e9a
Removes options .. unnecessary in this context
sleekslush 47580a7
uses the formatted event name
sleekslush 011342e
Simpler event handler
sleekslush f24fb95
fix comment
sleekslush File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,27 +39,14 @@ import { createLocationWatcher } from './internal/location-watcher'; | |
| * ``` | ||
| */ | ||
| export class ContentScriptContext implements AbortController { | ||
| private static SCRIPT_STARTED_MESSAGE_TYPE = getUniqueEventName( | ||
| 'wxt:content-script-started', | ||
| ); | ||
|
|
||
| private isTopFrame = window.self === window.top; | ||
| private abortController: AbortController; | ||
| private locationWatcher = createLocationWatcher(this); | ||
| private receivedMessageIds = new Set<string>(); | ||
|
|
||
| constructor( | ||
| private readonly contentScriptName: string, | ||
| public readonly options?: Omit<ContentScriptDefinition, 'main'>, | ||
| ) { | ||
| this.abortController = new AbortController(); | ||
|
|
||
| if (this.isTopFrame) { | ||
| this.listenForNewerScripts({ ignoreFirstEvent: true }); | ||
| this.stopOldScripts(); | ||
| } else { | ||
| this.listenForNewerScripts(); | ||
| } | ||
| } | ||
|
|
||
| get signal() { | ||
|
|
@@ -221,8 +208,19 @@ export class ContentScriptContext implements AbortController { | |
| if (this.isValid) this.locationWatcher.run(); | ||
| } | ||
|
|
||
| const eventType = type.startsWith('wxt:') ? getUniqueEventName(type) : type | ||
|
|
||
| target.addEventListener?.( | ||
| eventType, | ||
| () => this.isInvalid, // signals the abort controller when invalid | ||
| { | ||
| capture: true, | ||
| signal: this.signal, | ||
| }, | ||
| ); | ||
|
|
||
| target.addEventListener?.( | ||
| type.startsWith('wxt:') ? getUniqueEventName(type) : type, | ||
| eventType, | ||
| handler, | ||
| { | ||
| ...options, | ||
|
Comment on lines
+211
to
226
|
||
|
|
@@ -241,46 +239,6 @@ export class ContentScriptContext implements AbortController { | |
| `Content script "${this.contentScriptName}" context invalidated`, | ||
| ); | ||
| } | ||
|
|
||
| stopOldScripts() { | ||
| // Use postMessage so it get's sent to all the frames of the page. | ||
| window.postMessage( | ||
| { | ||
| type: ContentScriptContext.SCRIPT_STARTED_MESSAGE_TYPE, | ||
| contentScriptName: this.contentScriptName, | ||
| messageId: Math.random().toString(36).slice(2), | ||
| }, | ||
| '*', | ||
| ); | ||
| } | ||
|
|
||
| verifyScriptStartedEvent(event: MessageEvent) { | ||
| const isScriptStartedEvent = | ||
| event.data?.type === ContentScriptContext.SCRIPT_STARTED_MESSAGE_TYPE; | ||
| const isSameContentScript = | ||
| event.data?.contentScriptName === this.contentScriptName; | ||
| const isNotDuplicate = !this.receivedMessageIds.has(event.data?.messageId); | ||
| return isScriptStartedEvent && isSameContentScript && isNotDuplicate; | ||
| } | ||
|
|
||
| listenForNewerScripts(options?: { ignoreFirstEvent?: boolean }) { | ||
| let isFirst = true; | ||
|
|
||
| const cb = (event: MessageEvent) => { | ||
| if (this.verifyScriptStartedEvent(event)) { | ||
| this.receivedMessageIds.add(event.data.messageId); | ||
|
|
||
| const wasFirst = isFirst; | ||
| isFirst = false; | ||
| if (wasFirst && options?.ignoreFirstEvent) return; | ||
|
|
||
| this.notifyInvalidated(); | ||
| } | ||
| }; | ||
|
|
||
| addEventListener('message', cb); | ||
| this.onInvalidated(() => removeEventListener('message', cb)); | ||
| } | ||
| } | ||
|
|
||
| export interface WxtWindowEventMap extends WindowEventMap { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first addEventListener call is registering a function that returns a boolean as an event handler, which is incorrect. Event handlers should be functions that handle events, not functions that return boolean values.