'Service worker not activated in Puppeteer for localhost domain
The service worker I'm talking about here is from a chrome extension (background script). Because it is the background script from a chrome extension, it gets loaded into the browser as follows
async function bootstrap() {
const browser = await puppeteer.launch({
headless: false,
devtools: false,
args: [
`--disable-extensions-except=${EXTENSION_PATH}`,
`--load-extension=${EXTENSION_PATH}`,
'--user-agent=PuppeteerAgent',
'--disable-web-security',
'--user-data-dir="./tmp/"'
// '--no-sandbox', '--disable-setuid-sandbox'
],
});
const appPage = await browser.newPage();
await appPage.goto('https://google.com', { waitUntil: 'load' });
const targets = await browser.targets();
const extensionTarget = targets.find((target: any) => target.type() === 'service_worker');
const partialExtensionUrl = (extensionTarget as any)._targetInfo.url || '';
....
This snippet works fine as long as I have an https url. When I use http://localhost:8000
the service worker is not activated and extensionTarget
is undefined.
I think this makes sense. From the docs:
These days, service workers are enabled by default in all modern browsers. To run code using service workers, you'll need to serve your code via HTTPS — Service workers are restricted to running across HTTPS for security reasons. GitHub is therefore a good place to host experiments, as it supports HTTPS. In order to facilitate local development, localhost is considered a secure origin by browsers as well.
So, the service worker will work if it is served via https or from localhost
. I get the impression that because it is a chrome extension, the localhost rule does not apply here. So my question is, is there a way to fix this or disable this restriction (unfortunately --disable-web-security
didn't do the trick)
Any help would be appreciated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|