diff --git a/public/background.js b/public/background.js index dd4fa3d..cebbcf9 100644 --- a/public/background.js +++ b/public/background.js @@ -1,4 +1,4 @@ -import { ActionType, WebSocketManager } from './socket.js'; +import {ActionType, WebSocketManager} from './socket.js'; console.info("Chrome Extenstion Background is loaded") @@ -18,7 +18,7 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { case ActionType.DISCONNECT: websocketManager.disconnectWebsocket(); break; - case ActionType.SETPROXY: + case ActionType.SET_PROXY: chrome.proxy.settings.set({ value: { mode: "fixed_servers", @@ -33,10 +33,10 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { scope: 'regular', }); break; - case ActionType.CLEARPROXY: + case ActionType.CLEAR_PROXY: chrome.proxy.settings.clear({}) break; - case ActionType.PROXYSTATUS: + case ActionType.PROXY_STATUS: chrome.proxy.settings.get({}, function (details) { if (details.value && details.value.mode === "fixed_servers") { let proxyConfig = details.value.rules.singleProxy; @@ -45,26 +45,27 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { proxy: `${proxyConfig.scheme}://${proxyConfig.host}:${proxyConfig.port}` }) } else { - chrome.runtime.sendMessage({ enable: false, proxy: "" }) + chrome.runtime.sendMessage({enable: false, proxy: ""}) } }); break; - case ActionType.INJECTSCRIPT: + case ActionType.INJECT_SCRIPT: chrome.scripting.executeScript({ - target: { tabId: msg.tabId }, + target: {tabId: msg.tabId}, files: ['content.js'] }).then(() => { - chrome.tabs.sendMessage(msg.tabId, { type: 'INJECT_CODE', value: msg.value }); + chrome.tabs.sendMessage(msg.tabId, + {type: ActionType.INJECT_SCRIPT, value: msg.value} + ).then(response => { + console.log("response", response) + if (response && response.action === ActionType.TO_EXTENSION_PAGE) { + chrome.runtime.sendMessage(response) + } + }) }).catch(err => { console.error('Script injection failed:', err); }); break - case ActionType.SENDRESFROMPAGE: - chrome.runtime.sendMessage({ - action: ActionType.RESTOEVALINTAB, - result: msg.result - }) - break case ActionType.ECHO: console.log("Echo ", msg.result) } @@ -72,7 +73,7 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { }) const pageFunction = (code) => { - chrome.runtime.sendMessage({ code }, response => { + chrome.runtime.sendMessage({code}, response => { if (response && response.success) { console.log('Result:', response.result); } else { diff --git a/public/content.js b/public/content.js index d80f3c4..29eb0bb 100644 --- a/public/content.js +++ b/public/content.js @@ -4,12 +4,12 @@ } window.contentScriptInjected = true; chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { - if (request.type === 'INJECT_CODE') { + if (request.type === 'yakit_inject_script') { const injectedScriptURL = chrome.runtime.getURL('inject.js'); const script = document.createElement('script'); script.src = injectedScriptURL; script.onload = () => { - window.postMessage({ type: 'CALL_FUNCTION', value: request.value }, '*'); + window.postMessage({type: request.value.mode, value: request.value}, '*'); script.remove(); }; (document.head || document.documentElement).appendChild(script); @@ -19,10 +19,10 @@ } window.removeEventListener('message', onMessage); // Send the result to the background script - chrome.runtime.sendMessage({ action: 'SEND_RES_FROM_PAGE', result: event.data.result }); - sendResponse({ result: event.data.result }); + // chrome.runtime.sendMessage({ action: 'yakit_to_extension_page', result: event.data.result }); + // 直接向向发送端返回结果 + sendResponse({action: 'yakit_to_extension_page', result: event.data.result}); }); - // Return true to indicate that the response will be sent asynchronously return true; } }); diff --git a/public/index.html b/public/index.html index e90ad47..3ae8d4b 100644 --- a/public/index.html +++ b/public/index.html @@ -8,6 +8,5 @@
-