From 06bd651c5389d49618e130aedc67e26cb8d4990a Mon Sep 17 00:00:00 2001 From: fofolee Date: Thu, 23 Jan 2025 12:18:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=84=9A=E6=9C=AC=E6=B3=A8?= =?UTF-8?q?=E5=85=A5=E5=8A=9F=E8=83=BD=EF=BC=9A=E6=94=AF=E6=8C=81=E6=B3=A8?= =?UTF-8?q?=E5=85=A5=E8=BF=9C=E7=A8=8B=E5=92=8C=E6=9C=AC=E5=9C=B0JavaScrip?= =?UTF-8?q?t=E8=84=9A=E6=9C=AC=EF=BC=8C=E4=BC=98=E5=8C=96CSS=E6=B3=A8?= =?UTF-8?q?=E5=85=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/quickcomposer/browser/execScript.js | 49 ++++++++++- src/js/composer/commands/browserCommands.js | 83 +++++++++++++++---- 2 files changed, 114 insertions(+), 18 deletions(-) diff --git a/plugin/lib/quickcomposer/browser/execScript.js b/plugin/lib/quickcomposer/browser/execScript.js index 21aec07..4153fa0 100644 --- a/plugin/lib/quickcomposer/browser/execScript.js +++ b/plugin/lib/quickcomposer/browser/execScript.js @@ -1,4 +1,5 @@ const { executeScript } = require("./browser"); +const fs = require("fs"); const clickElement = async (tab, selector) => { return await executeScript( @@ -128,12 +129,54 @@ const injectCSS = async (tab, css) => { tab, ` const style = document.createElement('style'); - style.textContent = \`${css}\`; + style.textContent = css; document.head.appendChild(style); - ` + `, + { css } ); }; +const injectRemoteScript = async (tab, url) => { + return await executeScript( + tab, + ` + return await new Promise((resolve, reject) => { + const script = document.createElement('script'); + script.src = url; + script.type = 'text/javascript'; + script.onload = () => resolve(true); + script.onerror = () => reject(new Error('Failed to load script: ' + url)); + document.head.appendChild(script); + }); + `, + { url } + ); +}; + +const injectLocalScript = async (tab, filePath) => { + try { + if (!fs.existsSync(filePath)) { + throw new Error(`文件不存在: ${filePath}`); + } + + const content = fs.readFileSync(filePath, "utf8"); + + return await executeScript( + tab, + ` + const script = document.createElement('script'); + script.type = 'text/javascript'; + script.textContent = content; + document.head.appendChild(script); + return true; + `, + { content } + ); + } catch (error) { + throw new Error(`注入本地脚本失败: ${error.message}`); + } +}; + module.exports = { clickElement, inputText, @@ -148,4 +191,6 @@ module.exports = { getPageSize, waitForElement, injectCSS, + injectRemoteScript, + injectLocalScript, }; diff --git a/src/js/composer/commands/browserCommands.js b/src/js/composer/commands/browserCommands.js index e8e6998..bcc62a2 100644 --- a/src/js/composer/commands/browserCommands.js +++ b/src/js/composer/commands/browserCommands.js @@ -266,6 +266,73 @@ export const browserCommands = { }, ], }, + { + value: "quickcomposer.browser.injectRemoteScript", + label: "注入脚本/样式", + icon: "style", + isAsync: true, + config: [tabConfig], + subCommands: [ + { + label: "注入CDN脚本", + value: "quickcomposer.browser.injectRemoteScript", + icon: "javascript", + config: [ + { + component: "VariableInput", + icon: "link", + width: 12, + placeholder: "输入远程脚本URL", + }, + ], + }, + { + label: "注入本地脚本", + icon: "javascript", + value: "quickcomposer.browser.injectLocalScript", + config: [ + { + component: "VariableInput", + icon: "folder", + width: 12, + options: { + dialog: { + type: "open", + options: { + title: "选择脚本", + filters: [ + { + name: "JavaScript", + extensions: ["js"], + }, + { + name: "all", + extensions: ["*"], + }, + ], + properties: ["openFile"], + }, + }, + }, + placeholder: "输入本地脚本绝对路径", + }, + ], + }, + { + label: "注入CSS", + value: "quickcomposer.browser.injectCSS", + icon: "style", + config: [ + { + component: "CodeEditor", + icon: "style", + width: 12, + placeholder: "输入CSS代码", + }, + ], + }, + ], + }, { value: "quickcomposer.browser.setCookie", label: "Cookie操作", @@ -356,22 +423,6 @@ export const browserCommands = { }, ], }, - { - value: "quickcomposer.browser.injectCSS", - label: "注入CSS", - icon: "style", - isAsync: true, - config: [ - tabConfig, - { - label: "CSS内容", - component: "CodeEditor", - icon: "style", - width: 12, - placeholder: "输入CSS代码", - }, - ], - }, { value: "quickcomposer.browser.clickElement", label: "元素操作",