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

[Obsolete] Fix messaging in chrome extension pages (#2846) #2863

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

enkot
Copy link

@enkot enkot commented Apr 3, 2020

Fixed problem with not working onMessage handler in chrome extension pages.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@enkot
Copy link
Author

enkot commented Apr 3, 2020

@googlebot I signed it!

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

Copy link
Member

@zwu52 zwu52 left a comment

Choose a reason for hiding this comment

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

Hi Enkot, you were correct that the current SDK isn't working for Chrome Extension.
Thanks for making the fix and tests w/ it 👍

@@ -218,6 +218,30 @@ describe('SwController', () => {
expect(postMessageSpy).to.have.been.calledOnceWith(expectedMessage);
});

it('sends a message to chrome extension clients if a chrome extension client is visible', async () => {
const client: Writable<WindowClient> = (await self.clients.openWindow(
'chrome-extension://aihpiglmnhnhijdnjghpfnlledckkhja/popup.html'
Copy link
Member

Choose a reason for hiding this comment

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

looks like a real extension id. can we have it to be more generic? something like "chrome-extension://testExtensionId/popup.html" or whatever.

@@ -297,7 +297,7 @@ function hasVisibleClients(clientList: WindowClient[]): boolean {
client.visibilityState === 'visible' &&
// Ignore chrome-extension clients as that matches the background pages
// of extensions, which are always considered visible for some reason.
!client.url.startsWith('chrome-extension://')
!(client.url.startsWith('chrome-extension://') && client.url.endsWith("_generated_background_page.html"))
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this will work for auto-gen background.html. The next step is probably to have a more generic solution that would work for all situation (user defined background html) and across browser if applicable 😎

@@ -241,6 +265,32 @@ describe('SwController', () => {
});
});

it('does not send a message to chrome extension background pages if background page is visible', async () => {
const client: Writable<WindowClient> = (await self.clients.openWindow(
'chrome-extension://aihpiglmnhnhijdnjghpfnlledckkhja/_generated_background_page.html'
Copy link
Member

Choose a reason for hiding this comment

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

ditto

@enkot
Copy link
Author

enkot commented Apr 25, 2020

Hi @zwu52, can you please review last changes?

Copy link
Member

@zwu52 zwu52 left a comment

Choose a reason for hiding this comment

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

Enkot,
apologies for the delay. LGTM. Made some minor nits.

* @returns If client is the background page of browser extension, this method will
* resolve to true, otherwise false.
*/
async function isBackgroundClient(client: WindowClient): Promise<boolean> {
Copy link
Member

Choose a reason for hiding this comment

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

nit: how about isBackgroundExtension or isExtensionBackgroundClient. Just to make clear we're talking in the extension context

try {
const backgroundClient = await runtime.getBackgroundClient();
return client === backgroundClient;
} catch {
Copy link
Member

Choose a reason for hiding this comment

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

Enkot,
Can we have a debug log here on the error and error message so it would be easier for folks to debug if they hit this path and wanted to get information?

@enkot
Copy link
Author

enkot commented May 21, 2020

Hi @zwu52,
Maybe we can somehow boost review process?
That's small fix, but will allow a lot of people to use messaging in their browser extensions :)

@zwu52
Copy link
Member

zwu52 commented May 21, 2020

Enkot,
Thanks for the feedback and help on improving the product. Agreed. I have been working on iterating towards a process where git repo issues are getting a high priority in my task stack :)

@enkot
Copy link
Author

enkot commented Jul 28, 2020

Hi @zwu52,

Do we have any chance to merge this PR?

@zwu52
Copy link
Member

zwu52 commented Sep 9, 2020

Hi Enkot, apologies that I didn't realize the PR wasn't checked in. Thanks for pinging. This PR had some conflicts so I made this based on your PR. Feel free to use it to update your PR and I'll merge your PR.

@zwu52 zwu52 changed the title Fix messaging in chrome extension pages (#2846) [Obsolete] Fix messaging in chrome extension pages (#2846) Apr 14, 2021
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.

3 participants