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

Implement "to content script" via background page #29

Merged
merged 3 commits into from
Sep 27, 2021
Merged

Conversation

fregante
Copy link
Collaborator

@fregante fregante commented Sep 26, 2021

Test:

  1. Open https://rawtext.club
  2. See it call the entire CS test suite for other tabs, through the background page

Next:

  • Cleanup
  • Double-check

Notes:

  • The API isn't elegant, but as always I'm focusing on tests and getting it working and fully typed before perfecting the API, which can change at any time between iterations

@fregante fregante added the enhancement New feature or request label Sep 26, 2021
Comment on lines +115 to +122
if (message.target) {
const publicMethod = getContentScriptMethod(message.type);
return handleCall(
message,
sender,
publicMethod(message.target, ...message.args)
);
}
Copy link
Collaborator Author

@fregante fregante Sep 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the core of the forwarder at this point. Information about the original sender is not preserved yet.

I think I will turn MessengerMeta into an array where each step just prepends its own "sender" information, sort of like a stack trace.

Comment on lines +240 to +248
const sendMessage = !browser.tabs
? async () => browser.runtime.sendMessage(makeMessage(type, args, target))
: async () =>
browser.tabs.sendMessage(
target.tabId,
makeMessage(type, args),
// `frameId` must be specified. If missing, the message is sent to every frame
{ frameId: target.frameId ?? 0 }
);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the current "routing" implementation.

Since getContentScriptMethod is specific to content scripts, it's clear what the direction is: If we can't message the CS directly, we must contact the background page.

@@ -0,0 +1,33 @@
export async function ensureScripts(tabId: number): Promise<void> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey this looks familiar!

target?: Target;

/** If the message is being sent to an intermediary receiver, also set the options */
options?: Target;
Copy link
Collaborator Author

@fregante fregante Sep 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notification forwarding hasn't been implemented yet, so this is currently unused. I'll implement them separately

@fregante fregante marked this pull request as ready for review September 27, 2021 14:40
@fregante
Copy link
Collaborator Author

This is what the logging looks like.

On the left, the sender. On the right, the background page logging the messages it receives (no mentions that they're actually just forwarded, at the moment)

Screen Shot 9

@fregante fregante merged commit 57eac8e into main Sep 27, 2021
@fregante fregante deleted the forward-to-cs branch September 27, 2021 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

Implement "to content script" via background page
1 participant