From c1e255dd56050e6700fe4b636312e6e3152f97b7 Mon Sep 17 00:00:00 2001 From: fofolee Date: Wed, 22 Jan 2025 21:30:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E9=80=9A=E8=BF=87URL=E3=80=81=E6=A0=87=E9=A2=98=E5=92=8CID?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=A0=87=E7=AD=BE=E7=9A=84=E6=94=AF=E6=8C=81?= =?UTF-8?q?=EF=BC=8C=E9=87=8D=E6=9E=84=E6=A0=87=E7=AD=BE=E6=BF=80=E6=B4=BB?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BB=A5=E6=94=AF=E6=8C=81=E6=A8=A1=E7=B3=8A?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E3=80=82=E6=9B=B4=E6=96=B0=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E4=B8=BA=E5=BC=82=E6=AD=A5=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=EF=BC=8C=E7=AE=80=E5=8C=96=E9=85=8D=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/lib/quickcomposer/browser/browser.js | 15 ++-- src/js/composer/commands/browserCommands.js | 85 +++++---------------- 2 files changed, 28 insertions(+), 72 deletions(-) diff --git a/plugin/lib/quickcomposer/browser/browser.js b/plugin/lib/quickcomposer/browser/browser.js index 195c64e..93aff00 100644 --- a/plugin/lib/quickcomposer/browser/browser.js +++ b/plugin/lib/quickcomposer/browser/browser.js @@ -200,9 +200,7 @@ const initCDP = async (port) => { await Promise.all([Page.enable(), Runtime.enable(), DOM.enable()]); } catch (err) { console.log(err); - throw new Error( - `请先通过浏览器控制中的“启动浏览器”打开浏览器` - ); + throw new Error(`请先通过浏览器控制中的"启动浏览器"打开浏览器`); } } return { Page, Runtime, DOM }; @@ -246,16 +244,17 @@ const getTabs = async () => { .map((target) => ({ url: target.url, title: target.title, + id: target.id, })); }; -const activateTab = async (index) => { +const activateTab = async (type, value) => { const targets = await CDP.List(); - const pages = targets.filter((target) => target.type === "page"); - if (index > 0 && index <= pages.length) { - const targetId = pages[index - 1].id; - await CDP.Activate({ id: targetId }); + const target = targets.find((target) => target[type].includes(value)); + if (!target) { + throw new Error(`未找到目标: ${type} = ${value}`); } + await CDP.Activate({ id: target.id }); }; const clickElement = async (selector) => { diff --git a/src/js/composer/commands/browserCommands.js b/src/js/composer/commands/browserCommands.js index 00e61aa..5cafb76 100644 --- a/src/js/composer/commands/browserCommands.js +++ b/src/js/composer/commands/browserCommands.js @@ -105,6 +105,7 @@ export const browserCommands = { value: "quickcomposer.browser.getTabs", label: "获取/切换标签", icon: "tab", + isAsync: true, subCommands: [ { value: "quickcomposer.browser.getTabs", @@ -117,12 +118,22 @@ export const browserCommands = { icon: "tab_unselected", config: [ { - label: "标签索引", - component: "NumberInput", + component: "QSelect", icon: "tab", - min: 1, - defaultValue: 1, - width: 12, + width: 3, + options: [ + { label: "通过URL", value: "url" }, + { label: "通过标题", value: "title" }, + { label: "通过ID", value: "id" }, + ], + defaultValue: "url", + }, + { + label: "URL/标题/ID", + component: "VariableInput", + icon: "tab", + width: 9, + placeholder: "支持模糊匹配", }, ], }, @@ -132,6 +143,7 @@ export const browserCommands = { value: "quickcomposer.browser.executeScript", label: "执行脚本", icon: "code", + isAsync: true, config: [ { label: "脚本内容", @@ -152,6 +164,7 @@ export const browserCommands = { value: "quickcomposer.browser.setCookie", label: "Cookie操作", icon: "cookie", + isAsync: true, subCommands: [ { value: "quickcomposer.browser.setCookie", @@ -240,6 +253,7 @@ export const browserCommands = { value: "quickcomposer.browser.injectCSS", label: "注入CSS", icon: "style", + isAsync: true, config: [ { label: "CSS内容", @@ -255,6 +269,7 @@ export const browserCommands = { label: "手动选择元素", icon: "mouse", isAsync: true, + config: [], }, { value: "quickcomposer.browser.clickElement", @@ -284,13 +299,6 @@ export const browserCommands = { label: "输入文本", icon: "edit", config: [ - { - label: "选择器", - component: "VariableInput", - icon: "code", - width: 12, - placeholder: "输入CSS选择器", - }, { label: "文本内容", component: "VariableInput", @@ -304,84 +312,32 @@ export const browserCommands = { value: "quickcomposer.browser.getText", label: "获取文本", icon: "text_fields", - config: [ - { - label: "选择器", - component: "VariableInput", - icon: "code", - width: 12, - placeholder: "输入CSS选择器", - }, - ], }, { value: "quickcomposer.browser.getHtml", label: "获取HTML", icon: "code", - config: [ - { - label: "选择器", - component: "VariableInput", - icon: "code", - width: 12, - placeholder: "输入CSS选择器", - }, - ], }, { value: "quickcomposer.browser.hideElement", label: "隐藏元素", icon: "visibility_off", - config: [ - { - label: "选择器", - component: "VariableInput", - icon: "code", - width: 12, - placeholder: "输入CSS选择器", - }, - ], }, { value: "quickcomposer.browser.showElement", label: "显示元素", icon: "visibility", - config: [ - { - label: "选择器", - component: "VariableInput", - icon: "code", - width: 12, - placeholder: "输入CSS选择器", - }, - ], }, { value: "quickcomposer.browser.scrollToElement", label: "滚动到元素", icon: "open_in_full", - config: [ - { - label: "选择器", - component: "VariableInput", - icon: "code", - width: 12, - placeholder: "输入CSS选择器", - }, - ], }, { value: "quickcomposer.browser.waitForElement", label: "等待元素", icon: "hourglass_empty", config: [ - { - label: "选择器", - component: "VariableInput", - icon: "code", - width: 12, - placeholder: "输入CSS选择器", - }, { label: "超时时间", component: "NumberInput", @@ -399,6 +355,7 @@ export const browserCommands = { value: "quickcomposer.browser.scrollTo", label: "滚动及页面尺寸", icon: "open_in_full", + isAsync: true, subCommands: [ { value: "quickcomposer.browser.scrollTo",