Puppeteer load Chrome Extension error :browser.targets cannot find the service_worker - Stack Overflow

admin2025-05-02  1

When I am starting Puppeteer locally, I can load the plugin normally and can detect service_worker when headerless is any value.Here's my code to configure the browser

const absoultePath = path.join(__dirname, 'my_extension')
const browser = await puppeteer.launch({
            headless: "new",
            devtools: true,
            env: {
                LD_LIBRARY_PATH: '/shared-lib/linux',
            },
            args: [
                `--disable-extensions-except=${extensionPath}`,
                `--load-extension=${extensionPath}`,
                '--no-sandbox',
                '--disable-setuid-sandbox',
                '--deterministic-fetch',
                '--no-first-run',
                '--disable-background-timer-throttling',
            ],
        })

This code is my test to see if service_worker is loaded.

 await browser.waitForTarget(
            target =>{
return target.type() === 'service_worker' && target.url().endsWith('background.js');
            },
            { timeout: 15000 }
        );
        const targets =await browser.targets();
        extensionTarget = targets.find(target => target.type() === 'service_worker');

When I run it under ubuntu / Linux, I can't find service_woker until I get an error in the super timeout. But when I can't detect service_woker, the browser is running normally without interruption just can't determine if the plugin is loaded at this point. I really don't use the plugin functionality at the moment either so I can't be sure if the plugin is working or not.

To reiterate: I'm not reporting any other errors, just a timeout when checking service_woker, but no matter how long I extend it I can never find it!

I initially thought it was because my plugin file wasn't successfully placed in my directory, but I used this code to determine that the printout was there.

function validExtensionPath(extensionPath,absoultePath) {
    const manifestPath = path.join(extensionPath, 'manifest.json');
    if (fs.existsSync(manifestPath)) {
        console.log(`Manifest file found at: ${manifestPath}`);
    } else {
        console.log(`Manifest file not found in: ${extensionPath}`);
        throw new Error(`Extension path is not correct: ${extensionPath}`);
    }
    if(fs.existsSync(path.join(extensionPath,'node_modules'))) {
        console.log(`node_modules found in: ${extensionPath}`);
    } else {
        console.log(`node_modules not found in: ${extensionPath}`);
        // throw new Error(`Extension path is not correct: ${extensionPath}`);
    }
}

Here's my version of the package

"puppeteer": "^23.9.0",
转载请注明原文地址:http://www.anycun.com/QandA/1746136653a92079.html