修复浏览器控制网络被阻塞的BUG

This commit is contained in:
fofolee 2025-02-17 00:06:08 +08:00
parent bf1d6ff007
commit 49fdd973c1
4 changed files with 58 additions and 5 deletions

View File

@ -9,12 +9,11 @@ const initCDP = async (targetId) => {
port,
});
const { Page, Runtime, Target, Network, Emulation, DOM, Fetch } = client;
const { Page, Runtime, Target, Network, Emulation, DOM } = client;
await Promise.all([
Page.enable(),
Runtime.enable(),
DOM.enable(),
Fetch.enable(),
]);
return {
@ -25,7 +24,6 @@ const initCDP = async (targetId) => {
Network,
Emulation,
DOM,
Fetch,
};
} catch (err) {
console.log(err);

View File

@ -1,5 +1,35 @@
const { initCDP, cleanupCDP } = require("./cdp");
const { searchTarget } = require("./tabs");
const { getCurrentClientPort } = require("./client");
const CDP = require("chrome-remote-interface");
// 不从cdp.js导入initCDP单独启用一个Fetch域的CDP
const initCDP = async (targetId) => {
try {
const port = await getCurrentClientPort();
const client = await CDP({
target: targetId,
port,
});
const { Fetch } = client;
await Promise.all([Fetch.enable()]);
return { Fetch };
} catch (err) {
throw new Error(`连接到浏览器失败: ${err.message}`);
}
};
const cleanupCDP = async (targetId) => {
try {
// 直接关闭传入的 client
if (targetId?.client) {
await targetId.client.close();
}
} catch (error) {
console.log("关闭CDP连接失败:", error);
}
};
// 使用正则替换内容
const replaceWithRegex = (content, pattern, replacement) => {

View File

@ -1,5 +1,7 @@
const { initCDP, cleanupCDP } = require("./cdp");
const { searchTarget } = require("./tabs");
const CDP = require("chrome-remote-interface");
const getUrl = async (tab) => {
const target = await searchTarget(tab);
@ -15,6 +17,26 @@ const setUrl = async (tab, url) => {
await Page.navigate({ url });
await Page.loadEventFired();
await cleanupCDP(target.id);
// let client;
// try {
// // 连接到浏览器实例(默认端口 9222
// client = await CDP();
// const { Page } = client;
// // 启用 Page 域的监听
// await Page.enable();
// // 导航到指定 URL
// await Page.navigate({ url });
// // 等待页面加载完成(可选)
// await new Promise((resolve) => Page.loadEventFired(resolve));
// console.log("页面加载完成");
// } catch (err) {
// console.error("发生错误:", err);
// } finally {
// if (client) await client.close();
// }
};
module.exports = {

View File

@ -15,7 +15,10 @@ export default defineComponent({
try {
const tab = await quickcomposer.browser.getCurrentTab();
window.utools.hideMainWindow();
const selectElement = await quickcomposer.browser.getSelector(tab);
const selectElement = await quickcomposer.browser.getSelector({
by: "id",
searchValue: tab.id,
});
window.utools.showMainWindow();
this.$emit("emitValue", "str", selectElement || "");
} catch (error) {