From df709d9c726dd5b4f9cffd596f13960a1fd581b2 Mon Sep 17 00:00:00 2001 From: fofolee Date: Wed, 22 Jan 2025 21:41:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8C=96=E5=90=8E=E7=AB=AF=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C=E5=B0=86=E5=85=83=E7=B4=A0=E6=93=8D=E4=BD=9C=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=87=BD=E6=95=B0=E7=A7=BB=E8=87=B3=E6=96=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6script.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/lib/quickcomposer/browser/browser.js | 130 -------------------- plugin/lib/quickcomposer/browser/index.js | 4 + plugin/lib/quickcomposer/browser/script.js | 112 +++++++++++++++++ 3 files changed, 116 insertions(+), 130 deletions(-) create mode 100644 plugin/lib/quickcomposer/browser/script.js diff --git a/plugin/lib/quickcomposer/browser/browser.js b/plugin/lib/quickcomposer/browser/browser.js index 93aff00..73308e1 100644 --- a/plugin/lib/quickcomposer/browser/browser.js +++ b/plugin/lib/quickcomposer/browser/browser.js @@ -237,76 +237,6 @@ const executeScript = async (script, args = {}) => { return result.value; }; -const getTabs = async () => { - const targets = await CDP.List(); - return targets - .filter((target) => target.type === "page") - .map((target) => ({ - url: target.url, - title: target.title, - id: target.id, - })); -}; - -const activateTab = async (type, value) => { - const targets = await CDP.List(); - 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) => { - return await executeScript(`document.querySelector('${selector}').click()`); -}; - -const inputText = async (selector, text) => { - return await executeScript( - ` - const el = document.querySelector('${selector}'); - el.value = '${text}'; - el.dispatchEvent(new Event('input')); - el.dispatchEvent(new Event('change')); - ` - ); -}; - -const getText = async (selector) => { - return await executeScript( - `document.querySelector('${selector}')?.textContent || ''` - ); -}; - -const getHtml = async (selector) => { - return await executeScript( - `const element = document.querySelector('${selector}'); - return element ? element.innerHTML : '';` - ); -}; - -const hideElement = async (selector) => { - return await executeScript( - `document.querySelector('${selector}').style.display = 'none'` - ); -}; - -const showElement = async (selector) => { - return await executeScript( - `document.querySelector('${selector}').style.display = ''` - ); -}; - -const injectCSS = async (css) => { - return await executeScript( - ` - const style = document.createElement('style'); - style.textContent = \`${css}\`; - document.head.appendChild(style); - ` - ); -}; - const setCookie = async (cookies, options = {}) => { const { Network } = await initCDP(); for (const cookie of cookies) { @@ -329,71 +259,11 @@ const getCookie = async (name) => { return cookies.find((cookie) => cookie.name === name); }; -const scrollTo = async (x, y) => { - return await executeScript(`window.scrollTo(${x}, ${y})`); -}; - -const scrollToElement = async (selector) => { - return await executeScript( - `document.querySelector('${selector}').scrollIntoView()` - ); -}; - -const getScrollPosition = async () => { - return await executeScript(` - return JSON.stringify({ - x: window.pageXOffset || document.documentElement.scrollLeft, - y: window.pageYOffset || document.documentElement.scrollTop - }); - `); -}; - -const getPageSize = async () => { - return await executeScript(` - return JSON.stringify({ - width: Math.max( - document.documentElement.scrollWidth, - document.documentElement.clientWidth - ), - height: Math.max( - document.documentElement.scrollHeight, - document.documentElement.clientHeight - ) - }); - `); -}; - -const waitForElement = async (selector, timeout = 5000) => { - const startTime = Date.now(); - while (Date.now() - startTime < timeout) { - const result = await executeScript( - `!!document.querySelector('${selector}')` - ); - if (result) return; - await new Promise((resolve) => setTimeout(resolve, 100)); - } - throw new Error(`等待元素 ${selector} 超时`); -}; - module.exports = { launchBrowser, getUrl, setUrl, executeScript, - getTabs, - activateTab, - clickElement, - inputText, - getText, - getHtml, - hideElement, - showElement, - injectCSS, setCookie, getCookie, - scrollTo, - scrollToElement, - getScrollPosition, - getPageSize, - waitForElement, }; diff --git a/plugin/lib/quickcomposer/browser/index.js b/plugin/lib/quickcomposer/browser/index.js index eb7a8dc..ac69262 100644 --- a/plugin/lib/quickcomposer/browser/index.js +++ b/plugin/lib/quickcomposer/browser/index.js @@ -1,7 +1,11 @@ const browser = require("./browser"); +const tabs = require("./tabs"); const getSelector = require("./getSelector"); +const script = require("./script"); module.exports = { ...browser, ...getSelector, + ...tabs, + ...script, }; diff --git a/plugin/lib/quickcomposer/browser/script.js b/plugin/lib/quickcomposer/browser/script.js new file mode 100644 index 0000000..b388365 --- /dev/null +++ b/plugin/lib/quickcomposer/browser/script.js @@ -0,0 +1,112 @@ +const { executeScript } = require("./browser"); + +const clickElement = async (selector) => { + return await executeScript(`document.querySelector('${selector}').click()`); +}; + +const inputText = async (selector, text) => { + return await executeScript( + ` + const el = document.querySelector('${selector}'); + el.value = '${text}'; + el.dispatchEvent(new Event('input')); + el.dispatchEvent(new Event('change')); + ` + ); +}; + +const getText = async (selector) => { + return await executeScript( + `document.querySelector('${selector}')?.textContent || ''` + ); +}; + +const getHtml = async (selector) => { + return await executeScript( + `const element = document.querySelector('${selector}'); + return element ? element.innerHTML : '';` + ); +}; + +const hideElement = async (selector) => { + return await executeScript( + `document.querySelector('${selector}').style.display = 'none'` + ); +}; + +const showElement = async (selector) => { + return await executeScript( + `document.querySelector('${selector}').style.display = ''` + ); +}; + +const scrollTo = async (x, y) => { + return await executeScript(`window.scrollTo(${x}, ${y})`); +}; + +const scrollToElement = async (selector) => { + return await executeScript( + `document.querySelector('${selector}').scrollIntoView()` + ); +}; + +const getScrollPosition = async () => { + return await executeScript(` + return JSON.stringify({ + x: window.pageXOffset || document.documentElement.scrollLeft, + y: window.pageYOffset || document.documentElement.scrollTop + }); + `); +}; + +const getPageSize = async () => { + return await executeScript(` + return JSON.stringify({ + width: Math.max( + document.documentElement.scrollWidth, + document.documentElement.clientWidth + ), + height: Math.max( + document.documentElement.scrollHeight, + document.documentElement.clientHeight + ) + }); + `); +}; + +const waitForElement = async (selector, timeout = 5000) => { + const startTime = Date.now(); + while (Date.now() - startTime < timeout) { + const result = await executeScript( + `!!document.querySelector('${selector}')` + ); + if (result) return; + await new Promise((resolve) => setTimeout(resolve, 100)); + } + throw new Error(`等待元素 ${selector} 超时`); +}; + +const injectCSS = async (css) => { + return await executeScript( + ` + const style = document.createElement('style'); + style.textContent = \`${css}\`; + document.head.appendChild(style); + ` + ); +}; + +module.exports = { + clickElement, + inputText, + getText, + getHtml, + hideElement, + showElement, + scrollTo, + scrollToElement, + getScrollPosition, + getPageSize, + waitForElement, + injectCSS, +};