refactor: Create and use generic portal traversal utility#1935
Merged
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
25a034f to
51c3253
Compare
AprilSylph
reviewed
Jan 20, 2026
AprilSylph
left a comment
Owner
There was a problem hiding this comment.
Yeah, separate event names would be better. As it is, the code diff for src/main_world/index.js just looks like it's trying to avoid setting { bubbles: true } when dispatching events to document.documentElement, which... there's nowhere for it to bubble anyway... so it doesn't make sense until you realise it's just being used as a signal for responseHandler to resolve to something different. Best to just be explicit instead.
AprilSylph
reviewed
Jan 20, 2026
Collaborator
Author
|
The remaining parts of the above diff: diff --git a/src/main_world/index.js b/src/main_world/index.js
index 08508f7e..0e0a5858 100644
--- a/src/main_world/index.js
+++ b/src/main_world/index.js
@@ -22,7 +22,7 @@ document.documentElement.addEventListener('xkit-injection-request', async event
new CustomEvent('xkit-injection-element-response', { detail: JSON.stringify({ id }), bubbles: true })
);
} else {
- document.documentElement.dispatchEvent(
+ target.dispatchEvent(
new CustomEvent('xkit-injection-response', { detail: JSON.stringify({ id, result }) })
);
}
diff --git a/src/utils/inject.js b/src/utils/inject.js
index b29e2953..1591935a 100644
--- a/src/utils/inject.js
+++ b/src/utils/inject.js
@@ -17,7 +17,7 @@ export const inject = (path, args = [], target = document.documentElement) =>
const { id, result, exception } = JSON.parse(detail);
if (id !== requestId) return;
- document.documentElement.removeEventListener('xkit-injection-response', responseHandler);
+ target.removeEventListener('xkit-injection-response', responseHandler);
document.documentElement.removeEventListener('xkit-injection-element-response', responseHandler);
if (exception) {
@@ -28,7 +28,7 @@ export const inject = (path, args = [], target = document.documentElement) =>
resolve(result);
}
};
- document.documentElement.addEventListener('xkit-injection-response', responseHandler);
+ target.addEventListener('xkit-injection-response', responseHandler);
document.documentElement.addEventListener('xkit-injection-element-response', responseHandler);
target.dispatchEvent( |
AprilSylph
reviewed
Jan 20, 2026
Co-authored-by: April Sylph <28949509+AprilSylph@users.noreply.github.com>
AprilSylph
approved these changes
Jan 29, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
A little messy, but it works? Maybe helpful for #1931? I dunno. Possibly not worth it otherwise?
This:
xkit-injection-requestandxkit-injection-responsehandlers ondocument.documentElement(yes, this could mean a few extra handlers firing once in a blue moon; one could makexkit-injection-element-responseand add the event listener twice to prevent this I guess but I don't think that would save any cycles edit: see comment).closest()that traverses portals.Testing steps
More tests could be run with #677/#1662, I guess.