const puppeteer = require("puppeteer-core");
const url = "";
const routes = [];
const notFoundRoutes = [];
let first = true;
let currentIndex = 0;
const getRoutes = (res) => {
res = JSON.parse(res);
res.data.permissions.forEach((parent) => {
// 一些逻辑
});
};
const routesTest = async (browser, index) => {
currentIndex++;
const newPage = await browser.newPage();
console.log(routes[index].url);
try {
await newPage.goto(routes[index].url, { waitUntil: "networkidle0" });
} catch {
setup();
}
const result = await newPage.evaluate(() => {
return window.location.href;
});
addNotFoundUrl(result, routes[index]);
await newPage.screenshot({
path: "",
});
await newPage.close();
if (index === routes.length - 1) {
console.log(notFoundRoutes);
return;
}
routesTest(browser, currentIndex);
};
const sleep = (delay) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, delay);
});
};
function addNotFoundUrl(url, route) {
const reg = /[/s/S]*[\/404]$/;
if (reg.test(url)) {
notFoundRoutes.push({
title: route.title,
url: route.url,
});
}
}
function containDouleRoutes(route) {
const reg = /^\/?[^\/]+\/[^\/]*/;
return reg.test(route);
}
async function setup() {
const browser = await puppeteer.launch({
executablePath:
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
headless: false,
});
const page = await browser.newPage();
await page.setRequestInterception(true);
await page.on("request", (request) => {
if (request.url().includes("getPermissionsNew")) {
page.on("response", (response) => {
if (response.url().includes("getPermissionsNew")) {
const message = response.text();
message.then((res) => {
if (first) {
first = false;
getRoutes(res);
routesTest(browser, currentIndex);
}
});
}
});
}
request.continue();
});
await page.goto(url, { waitUntil: "networkidle2" });
const node1 = await page.$(".login-select > .warp");
await node1.click();
await page.waitForNavigation();
await page.type('input[name="username"]', "");
await page.type('input[name="password"]', "");
const button = await page.$x('//*[@id="pane-1"]/form/button');
await button[0].click();
}
(async () => {
setup();
})();
页面菜单监控
最新推荐文章于 2024-08-16 19:02:09 发布