From 49fdd973c1ee68c6289d08e4829f9268da33bb0c Mon Sep 17 00:00:00 2001 From: fofolee Date: Mon, 17 Feb 2025 00:06:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E7=BD=91=E7=BB=9C=E8=A2=AB=E9=98=BB=E5=A1=9E?= =?UTF-8?q?=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/lib/quickcomposer/browser/cdp.js | 4 +-- plugin/lib/quickcomposer/browser/network.js | 32 ++++++++++++++++++- plugin/lib/quickcomposer/browser/url.js | 22 +++++++++++++ .../composer/common/varinput/CssSelector.vue | 5 ++- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/plugin/lib/quickcomposer/browser/cdp.js b/plugin/lib/quickcomposer/browser/cdp.js index 02cf566..609deb9 100644 --- a/plugin/lib/quickcomposer/browser/cdp.js +++ b/plugin/lib/quickcomposer/browser/cdp.js @@ -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); diff --git a/plugin/lib/quickcomposer/browser/network.js b/plugin/lib/quickcomposer/browser/network.js index 0b37a9e..dc656ad 100644 --- a/plugin/lib/quickcomposer/browser/network.js +++ b/plugin/lib/quickcomposer/browser/network.js @@ -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) => { diff --git a/plugin/lib/quickcomposer/browser/url.js b/plugin/lib/quickcomposer/browser/url.js index 596db30..077944f 100644 --- a/plugin/lib/quickcomposer/browser/url.js +++ b/plugin/lib/quickcomposer/browser/url.js @@ -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 = { diff --git a/src/components/composer/common/varinput/CssSelector.vue b/src/components/composer/common/varinput/CssSelector.vue index 42a150a..00fddae 100644 --- a/src/components/composer/common/varinput/CssSelector.vue +++ b/src/components/composer/common/varinput/CssSelector.vue @@ -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) {