const puppeteer = require("puppeteer");
puppeteer
.launch({
executablePath: "C:/Program Files/Google/Chrome/Application/chrome.exe",
defaultViewport: null,
headless: false,
})
.then(async (browser) => {
const page = await browser.newPage();
await expose(page, "fs");
})
.catch((err) => {});
async function expose(page, name) {
const Require = async (name) => {
const object = require(name);
for (const key in object) {
if (Object.hasOwnProperty.call(object, key)) {
const element = object[key];
if ((element && typeof element === "function") || typeof element?.default === "function") {
await page.exposeFunction(name + "_expose_" + key, element);
}
}
}
};
await Require(name);
await page.evaluate((name) => {
const keys = Reflect.ownKeys(window)
.filter((key) => key.startsWith(name + "_expose_"))
.map((key) => key.replace(name + "_expose_", ""));
const object = {};
for (const key of keys) {
Reflect.set(object, key, window[name + "_expose_" + key]);
}
Reflect.set(window, name, object);
}, name);
}
效果
|