如此处所示,Puppeteer 允许覆盖 Javascript 函数。我想覆盖 showOpenFilePicker 函数。也就是当showOpenFilePicker 被网页调用时。我想在 showOpenFilePicker 之前运行另一个函数。
const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.evaluateOnNewDocument(() => {
Object.defineProperty(HTMLCanvasElement.prototype, "toBlob", {
value: () => {
console.log("Hey there");
},
});
});
await page.goto("https://example.com");
await page.evaluate(() => {
console.log(HTMLCanvasElement.prototype.toBlob.toString());
});
// await browser.close();
})();