本文介绍了用Puppeteer覆盖showOpenFilePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如此处此处所示,Puppeteer允许覆盖Javascript函数.我想覆盖showOpenFilePicker函数.也就是说,当showOpenFilePicker被网页调用时.我想在showOpenFilePicker之前运行另一个功能.

As illustrated in here here, Puppeteer allows to override Javascript functions. I want to override showOpenFilePicker function. That is, when the showOpenFilePicker invoked by the web page. I want to run another function before the 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();
})();

推荐答案

您可以像下面的代码示例一样覆盖Puppeteer中的内置函数.这会将原始函数替换为将参数记录到控制台的替代.

You can override built-in functions in Puppeteer like in the code sample below. This replaces the original function with an override that logs the arguments to the console.

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();

  await page.evaluateOnNewDocument(() => {
    const originalShowOpenFilePicker = window.showOpenFilePicker;
    window.showOpenFilePicker = (...args) => {
      console.log('Modified `showOpenFilePicker` called with these arguments:', args);
      return originalShowOpenFilePicker(...args);
    };
  });

  await page.goto("https://example.com");

  await page.evaluate(() => {
    console.log(showOpenFilePicker());
  });

  // await browser.close();
})();

这篇关于用Puppeteer覆盖showOpenFilePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 05:48