Chrome extensions does not allow unsafe-eval
, that is reason why puppeteer is not working on chrome extension. Set the following on manifest.json
.
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
Tested with following code,
const puppeteer = require('puppeteer');
async function getTitle() {
const browser = await puppeteer.connect({
browserWSEndpoint: 'ws://127.0.0.1:9222/devtools/browser/9f0a2240-2cb7-4efa-ac3c-8ef883d36d12',
});
const page = await browser.newPage();
await page.goto('http://example.com');
const title = await page.title();
await page.close();
await browser.disconnect();
return title;
}
getTitle().then(console.log);
Result:
How did I find it:
The code is running perfectly if I run it directly or put it on a page, but wasn't working from chrome extension only.
The asyncawait
check here helped me find the culprit.
let asyncawait = true;
try {
new Function('async function test(){await 1}');
} catch (error) {
asyncawait = false;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…