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

#579: Deduplicate injection and make it more straightforward #654

Closed
wants to merge 2 commits into from

Conversation

fregante
Copy link
Collaborator

Related to #579

@twschiller the code is WIP but I wanted to ask a question. Review coming in a minute

@@ -76,7 +76,7 @@ function backgroundMessageListener(
const handlerPromise = new Promise((resolve) => {
resolve(
handler(
{ tabId: meta.tabId, frameId: meta.frameId ?? 0 },
{ tabId: meta.tabId, frameId: meta.frameId ?? 0, url: meta.url },
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wanted to pass the URL around, when available, but I'm having trouble following the flow of this lifted function so I thought it's better to ask you.

Is it possible to get the URL of the tab here? It's used to exclude pages that have permanent permissions, see the new isContentScriptRegistered function.

Currently this is undefined

Copy link
Contributor

Choose a reason for hiding this comment

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

For the "lift" functions, there's always two sides to it, the calling function and the listening function. For this one, the calling side is here: http://github.com/pixiebrix/pixiebrix-extension/blob/8302a92f9ed90a4e48624a62e119e193fe2b278c/src/background/devtools/internal.ts#L184-L184

With that method setting the "meta" properties of the request here: http://github.com/pixiebrix/pixiebrix-extension/blob/8302a92f9ed90a4e48624a62e119e193fe2b278c/src/background/devtools/external.ts#L89-L89

What's interesting about that is it actually leaves off one of the expected fields: http://github.com/pixiebrix/pixiebrix-extension/blob/8302a92f9ed90a4e48624a62e119e193fe2b278c/src/background/devtools/contract.ts#L56-L56

I'm not sure if URL is available at that call site, as I don't see it on the type declaration for browser.devtools.inspectedWindow

Historic context: for the messaging protocol, I just copied the Flux Standard Actions approach used by Redux: https://github.com/redux-utilities/flux-standard-action

Copy link
Contributor

Choose a reason for hiding this comment

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

I wanted to pass the URL around

I'm not sure passing the URL around simplifies things due to the additional book keeping. I guess one benefit it we can sort of enforce this: w3c/webextensions#8

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, I’ll look into it again today. The types are indeed failing me here, likely due to to an assertion in the callBackground call. These kind of functions have complex types so it’s hard/impossible to get them right. I regularly run into TS limitations when trying to type type-preserving functions

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah the URL call was actually just in the temporary injection function, and I think it was available there, but as I’m working to have a “single injector” I moved it here without luck.

As for that issue, the only way to avoid it for us would be to do a URL check in the pre-contentScript we had discussed before

* @param error
* @returns
*/
export async function isPrivatePageError(error: unknown): Promise<boolean> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this should be async?

}

/** Checks whether a tab has the activeTab permission to inject scripts and CSS */
export async function isActiveTab(
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a confusing method name. At first glance, it looks as if it should be whether the tab is the active one in the browser. I would recommend hasActiveTabPermission, as it's wordier, but instant to understand

Additionally, this will also work if the page has permanent permissions

How does this method relate to testPagePermissions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It probably replaces it. The PR is still a WIP, trying things out

if (!isBackgroundPage()) {
throw new Error(
"injectContentScript can only be called from the background page"
);
}

// Already has permanent access
if (await isContentScriptRegistered(target.url)) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer return false so that it matches the return type

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why isn’t this caught by TS? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants