优化浏览器命令功能,新增通过URL、标题和ID获取标签的支持,重构标签激活逻辑以支持模糊匹配。更新相关命令为异步执行,简化配置项

This commit is contained in:
fofolee 2025-01-22 21:30:16 +08:00
parent c3ca34bd2a
commit c1e255dd56
2 changed files with 28 additions and 72 deletions

View File

@ -200,9 +200,7 @@ const initCDP = async (port) => {
await Promise.all([Page.enable(), Runtime.enable(), DOM.enable()]); await Promise.all([Page.enable(), Runtime.enable(), DOM.enable()]);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
throw new Error( throw new Error(`请先通过浏览器控制中的"启动浏览器"打开浏览器`);
`请先通过浏览器控制中的“启动浏览器”打开浏览器`
);
} }
} }
return { Page, Runtime, DOM }; return { Page, Runtime, DOM };
@ -246,16 +244,17 @@ const getTabs = async () => {
.map((target) => ({ .map((target) => ({
url: target.url, url: target.url,
title: target.title, title: target.title,
id: target.id,
})); }));
}; };
const activateTab = async (index) => { const activateTab = async (type, value) => {
const targets = await CDP.List(); const targets = await CDP.List();
const pages = targets.filter((target) => target.type === "page"); const target = targets.find((target) => target[type].includes(value));
if (index > 0 && index <= pages.length) { if (!target) {
const targetId = pages[index - 1].id; throw new Error(`未找到目标: ${type} = ${value}`);
await CDP.Activate({ id: targetId });
} }
await CDP.Activate({ id: target.id });
}; };
const clickElement = async (selector) => { const clickElement = async (selector) => {

View File

@ -105,6 +105,7 @@ export const browserCommands = {
value: "quickcomposer.browser.getTabs", value: "quickcomposer.browser.getTabs",
label: "获取/切换标签", label: "获取/切换标签",
icon: "tab", icon: "tab",
isAsync: true,
subCommands: [ subCommands: [
{ {
value: "quickcomposer.browser.getTabs", value: "quickcomposer.browser.getTabs",
@ -117,12 +118,22 @@ export const browserCommands = {
icon: "tab_unselected", icon: "tab_unselected",
config: [ config: [
{ {
label: "标签索引", component: "QSelect",
component: "NumberInput",
icon: "tab", icon: "tab",
min: 1, width: 3,
defaultValue: 1, options: [
width: 12, { 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", value: "quickcomposer.browser.executeScript",
label: "执行脚本", label: "执行脚本",
icon: "code", icon: "code",
isAsync: true,
config: [ config: [
{ {
label: "脚本内容", label: "脚本内容",
@ -152,6 +164,7 @@ export const browserCommands = {
value: "quickcomposer.browser.setCookie", value: "quickcomposer.browser.setCookie",
label: "Cookie操作", label: "Cookie操作",
icon: "cookie", icon: "cookie",
isAsync: true,
subCommands: [ subCommands: [
{ {
value: "quickcomposer.browser.setCookie", value: "quickcomposer.browser.setCookie",
@ -240,6 +253,7 @@ export const browserCommands = {
value: "quickcomposer.browser.injectCSS", value: "quickcomposer.browser.injectCSS",
label: "注入CSS", label: "注入CSS",
icon: "style", icon: "style",
isAsync: true,
config: [ config: [
{ {
label: "CSS内容", label: "CSS内容",
@ -255,6 +269,7 @@ export const browserCommands = {
label: "手动选择元素", label: "手动选择元素",
icon: "mouse", icon: "mouse",
isAsync: true, isAsync: true,
config: [],
}, },
{ {
value: "quickcomposer.browser.clickElement", value: "quickcomposer.browser.clickElement",
@ -284,13 +299,6 @@ export const browserCommands = {
label: "输入文本", label: "输入文本",
icon: "edit", icon: "edit",
config: [ config: [
{
label: "选择器",
component: "VariableInput",
icon: "code",
width: 12,
placeholder: "输入CSS选择器",
},
{ {
label: "文本内容", label: "文本内容",
component: "VariableInput", component: "VariableInput",
@ -304,84 +312,32 @@ export const browserCommands = {
value: "quickcomposer.browser.getText", value: "quickcomposer.browser.getText",
label: "获取文本", label: "获取文本",
icon: "text_fields", icon: "text_fields",
config: [
{
label: "选择器",
component: "VariableInput",
icon: "code",
width: 12,
placeholder: "输入CSS选择器",
},
],
}, },
{ {
value: "quickcomposer.browser.getHtml", value: "quickcomposer.browser.getHtml",
label: "获取HTML", label: "获取HTML",
icon: "code", icon: "code",
config: [
{
label: "选择器",
component: "VariableInput",
icon: "code",
width: 12,
placeholder: "输入CSS选择器",
},
],
}, },
{ {
value: "quickcomposer.browser.hideElement", value: "quickcomposer.browser.hideElement",
label: "隐藏元素", label: "隐藏元素",
icon: "visibility_off", icon: "visibility_off",
config: [
{
label: "选择器",
component: "VariableInput",
icon: "code",
width: 12,
placeholder: "输入CSS选择器",
},
],
}, },
{ {
value: "quickcomposer.browser.showElement", value: "quickcomposer.browser.showElement",
label: "显示元素", label: "显示元素",
icon: "visibility", icon: "visibility",
config: [
{
label: "选择器",
component: "VariableInput",
icon: "code",
width: 12,
placeholder: "输入CSS选择器",
},
],
}, },
{ {
value: "quickcomposer.browser.scrollToElement", value: "quickcomposer.browser.scrollToElement",
label: "滚动到元素", label: "滚动到元素",
icon: "open_in_full", icon: "open_in_full",
config: [
{
label: "选择器",
component: "VariableInput",
icon: "code",
width: 12,
placeholder: "输入CSS选择器",
},
],
}, },
{ {
value: "quickcomposer.browser.waitForElement", value: "quickcomposer.browser.waitForElement",
label: "等待元素", label: "等待元素",
icon: "hourglass_empty", icon: "hourglass_empty",
config: [ config: [
{
label: "选择器",
component: "VariableInput",
icon: "code",
width: 12,
placeholder: "输入CSS选择器",
},
{ {
label: "超时时间", label: "超时时间",
component: "NumberInput", component: "NumberInput",
@ -399,6 +355,7 @@ export const browserCommands = {
value: "quickcomposer.browser.scrollTo", value: "quickcomposer.browser.scrollTo",
label: "滚动及页面尺寸", label: "滚动及页面尺寸",
icon: "open_in_full", icon: "open_in_full",
isAsync: true,
subCommands: [ subCommands: [
{ {
value: "quickcomposer.browser.scrollTo", value: "quickcomposer.browser.scrollTo",