diff --git a/package.json b/package.json index aa03281..4a19aa0 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", "ahooks": "^3.7.10", - "antd": "^5.17.0", + "antd": "^5.17.2", "babel-jest": "^27.4.2", "babel-plugin-named-asset-import": "^0.3.8", "babel-preset-react-app": "^10.0.1", diff --git a/public/background.js b/public/background.js index cebbcf9..868273a 100644 --- a/public/background.js +++ b/public/background.js @@ -50,24 +50,29 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { }); break; case ActionType.INJECT_SCRIPT: - chrome.scripting.executeScript({ - target: {tabId: msg.tabId}, - files: ['content.js'] - }).then(() => { - chrome.tabs.sendMessage(msg.tabId, - {type: ActionType.INJECT_SCRIPT, value: msg.value} - ).then(response => { - console.log("response", response) + (async () => { + try { + // 注入 JS 脚本 + await chrome.scripting.executeScript({ + target: {tabId: msg.tabId}, + files: ['content.js'] + }); + + // 发送消息 + const response = await chrome.tabs.sendMessage(msg.tabId, { + type: ActionType.INJECT_SCRIPT, + value: msg.value + }); + + console.log("response", response); if (response && response.action === ActionType.TO_EXTENSION_PAGE) { - chrome.runtime.sendMessage(response) + await chrome.runtime.sendMessage(response); } - }) - }).catch(err => { - console.error('Script injection failed:', err); - }); + } catch (err) { + console.error('Script or CSS injection failed:', err); + } + })(); break - case ActionType.ECHO: - console.log("Echo ", msg.result) } }) diff --git a/public/content.js b/public/content.js index 29eb0bb..fc46a8c 100644 --- a/public/content.js +++ b/public/content.js @@ -3,6 +3,31 @@ return; } window.contentScriptInjected = true; + // 检查并插入 CSS 样式 + const styleId = 'injected-css-style'; + if (!document.getElementById(styleId)) { + const style = document.createElement('style'); + style.id = styleId; + style.textContent = ` + body { + border: 3px solid red; + position: relative; /* Ensure the body is positioned to allow the pseudo-element */ + } + body::after { + content: "Injection successful"; + display: block; + position: fixed; + top: 10px; + right: 10px; + background: green; + color: white; + padding: 5px 10px; + font-size: 16px; + z-index: 1000; + } + `; + document.head.appendChild(style); + } chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.type === 'yakit_inject_script') { const injectedScriptURL = chrome.runtime.getURL('inject.js'); @@ -14,7 +39,7 @@ }; (document.head || document.documentElement).appendChild(script); window.addEventListener('message', function onMessage(event) { - if (event.source !== window || event.data.type !== 'FROM_PAGE') { + if (event.source !== window || event.data.type !== 'FROM_INJECT_JS') { return; } window.removeEventListener('message', onMessage); diff --git a/public/inject.js b/public/inject.js index 6da0c39..ed5c0ab 100644 --- a/public/inject.js +++ b/public/inject.js @@ -8,24 +8,30 @@ if (event.source !== window) { return; } - let result = ""; + let result switch (event.data.type) { case 'CONTENT_CALL_FUNCTION': const fn_name = event.data.value.fn_name; const args = event.data.value.args; result = window[fn_name](args); + window.postMessage({type: 'FROM_INJECT_JS', result: result}, '*'); break; case 'CONTENT_EVAL_CODE': const code = event.data.value.code; - result = eval(code); + result = (() => { + try { + return eval(code); + } catch (e) { + // console.error("Error evaluating code:", e); + return e.toString(); + } + })(); // console.log("CONTENT_EVAL_CODE result: ", result); + window.postMessage({type: 'FROM_INJECT_JS', result: result}, '*'); break; default: break; } - if (result && typeof result === 'object' && Object.keys(result).length > 0) { - window.postMessage({type: 'FROM_PAGE', result: result}, '*'); - } }); })(); diff --git a/public/socket.js b/public/socket.js index 400054f..5529545 100644 --- a/public/socket.js +++ b/public/socket.js @@ -79,48 +79,5 @@ export class WebSocketManager { handleMessage(message) { console.log("message", message) - // 解析消息 - // try { - // // 解析从WebSocket接收到的JSON数据 - // let code = message; - // getTabId().then((tabId) => { - // // 确保tabId和code有效 - // if (typeof tabId === 'number' && typeof code === 'string') { - // chrome.webNavigation.getAllFrames({tabId: tabId}, function(frames) { - // for (let frame of frames) { - // console.log(`Frame ID: ${frame.frameId} with URL: ${frame.url}`); - // } - // }); - // // 在指定的tabId上执行脚本 - // chrome.scripting.executeScript({ - // target: {tabId: tabId}, - // // function: getTitle, - // function: log, - // args: [code] - // }, (injectionResults) => { - // // 处理脚本执行的结果,如日志记录等 - // console.log('Script executed:', injectionResults); - // }); - // } else { - // console.error('Received malformed message:', message); - // } - // }); - // } catch (err) { - // console.error('Error parsing message from WebSocket:', err); - // } } -} - -function getTitle() { - return document.title; -} - -function log(message) { - return console.log(message); -} - -// chrome.tabs.query({}, (tabs) => { -// tabs.forEach(function (tab) { -// console.log(tab.id); // 输出每个标签页的ID -// }); -// }) \ No newline at end of file +} \ No newline at end of file diff --git a/src/App.tsx b/src/App.tsx index 3d91c4f..d01344b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -15,7 +15,7 @@ function App() { }} >