fix: handle service workers in extensions (#8807)

Closes #8800
This commit is contained in:
Alex Rudenko 2022-08-17 19:34:34 +02:00 committed by GitHub
parent 51b2f0576d
commit 2a0eefb99f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 6 deletions

View File

@ -317,11 +317,12 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
) {
this.#finishInitializationIfReady(targetInfo.targetId);
await silentDetach();
if (parentSession instanceof CDPSession) {
const target = this.#targetFactory(targetInfo);
this.#attachedTargetsByTargetId.set(targetInfo.targetId, target);
this.emit(TargetManagerEmittedEvents.TargetAvailable, target);
if (this.#attachedTargetsByTargetId.has(targetInfo.targetId)) {
return;
}
const target = this.#targetFactory(targetInfo);
this.#attachedTargetsByTargetId.set(targetInfo.targetId, target);
this.emit(TargetManagerEmittedEvents.TargetAvailable, target);
return;
}

View File

@ -0,0 +1 @@
// empty

View File

@ -0,0 +1,9 @@
{
"name": "Simple extension",
"version": "0.1",
"background": {
"service_worker": "background.js"
},
"permissions": ["background", "activeTab"],
"manifest_version": 3
}

View File

@ -36,6 +36,13 @@ const mkdtempAsync = promisify(fs.mkdtemp);
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
const extensionPath = path.join(__dirname, '../assets', 'simple-extension');
const serviceWorkerExtensionPath = path.join(
__dirname,
'..',
'assets',
'serviceworkers',
'extension'
);
describeChromeOnly('headful tests', function () {
/* These tests fire up an actual browser so let's
@ -120,7 +127,7 @@ describeChromeOnly('headful tests', function () {
);
const page = await browserWithExtension.newPage();
const backgroundPageTarget = await browserWithExtension.waitForTarget(
(target: {type: () => string}) => {
target => {
return target.type() === 'background_page';
}
);
@ -128,6 +135,26 @@ describeChromeOnly('headful tests', function () {
await browserWithExtension.close();
expect(backgroundPageTarget).toBeTruthy();
});
it('service_worker target type should be available', async () => {
const {puppeteer, defaultBrowserOptions} = getTestState();
const browserWithExtension = await launchBrowser(puppeteer, {
...defaultBrowserOptions,
headless: false,
args: [
`--disable-extensions-except=${serviceWorkerExtensionPath}`,
`--load-extension=${serviceWorkerExtensionPath}`,
],
});
const page = await browserWithExtension.newPage();
const serviceWorkerTarget = await browserWithExtension.waitForTarget(
target => {
return target.type() === 'service_worker';
}
);
await page.close();
await browserWithExtension.close();
expect(serviceWorkerTarget).toBeTruthy();
});
it('target.page() should return a background_page', async function () {
const {puppeteer} = getTestState();
const browserWithExtension = await launchBrowser(
@ -135,7 +162,7 @@ describeChromeOnly('headful tests', function () {
extensionOptions
);
const backgroundPageTarget = await browserWithExtension.waitForTarget(
(target: {type: () => string}) => {
target => {
return target.type() === 'background_page';
}
);