mirror of
https://github.com/yaklang/yaklang-chrome-extension.git
synced 2026-09-22 03:10:43 +08:00
完善一下
This commit is contained in:
+16
-15
@@ -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 {
|
||||
|
||||
+5
-5
@@ -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;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -8,6 +8,5 @@
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<iframe src="./sandbox.html" id="sandbox" style="position: absolute;width:0;height:0;border:0;"></iframe>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const button = document.getElementById('btn');
|
||||
button.addEventListener('click', clickHandler);
|
||||
});
|
||||
|
||||
function clickHandler() {
|
||||
const iframe = document.getElementById('sandbox');
|
||||
iframe.contentWindow.postMessage( "22 * 33", '*');
|
||||
|
||||
function listener(event) {
|
||||
// 这里处理从 iframe 发送回来的数据
|
||||
console.log('Received message:', event.data);
|
||||
}
|
||||
|
||||
|
||||
window.addEventListener('message', listener, {once: true});
|
||||
|
||||
}
|
||||
+27
-12
@@ -1,16 +1,31 @@
|
||||
(() => {
|
||||
if (window.injectedMessageListener) {
|
||||
return;
|
||||
}
|
||||
window.injectedMessageListener = true;
|
||||
|
||||
window.addEventListener('message', function onMessage(event) {
|
||||
if (event.source !== window || event.data.type !== 'CALL_FUNCTION') {
|
||||
return;
|
||||
if (window.injectedMessageListener) {
|
||||
return;
|
||||
}
|
||||
const value = event.data.value;
|
||||
const result = window[value.fn_name](value.args);
|
||||
window.postMessage({ type: 'FROM_PAGE', result: result }, '*');
|
||||
});
|
||||
window.injectedMessageListener = true;
|
||||
|
||||
window.addEventListener('message', function onMessage(event) {
|
||||
if (event.source !== window) {
|
||||
return;
|
||||
}
|
||||
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);
|
||||
break;
|
||||
case 'CONTENT_EVAL_CODE':
|
||||
const code = event.data.value.code;
|
||||
result = eval(code);
|
||||
// console.log("CONTENT_EVAL_CODE result: ", result);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (result && typeof result === 'object' && Object.keys(result).length > 0) {
|
||||
window.postMessage({type: 'FROM_PAGE', result: result}, '*');
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
||||
|
||||
@@ -15,11 +15,6 @@
|
||||
"service_worker": "background.js",
|
||||
"type": "module"
|
||||
},
|
||||
"sandbox": {
|
||||
"pages": [
|
||||
"sandbox.html"
|
||||
]
|
||||
},
|
||||
"permissions": [
|
||||
"webNavigation",
|
||||
"activeTab",
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
<script src="./src/crypto-js.min.js"></script>
|
||||
</head>
|
||||
<script>
|
||||
window.addEventListener('message', async function (event) {
|
||||
try {
|
||||
console.log("sandbox event", event)
|
||||
if (!event.data[0]) {
|
||||
throw new Error('格式化方法有问题');
|
||||
}
|
||||
const func = new Function('obj', event.data[0]);
|
||||
event.source.window.postMessage(eval(event.data), event.origin);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
event.source.window.postMessage(eval(event.data), event.origin);
|
||||
}
|
||||
|
||||
});
|
||||
</script>
|
||||
</html>
|
||||
+5
-8
@@ -2,14 +2,11 @@ export const ActionType = {
|
||||
CONNECT: 'connect',
|
||||
DISCONNECT: 'disconnect',
|
||||
STATUS: 'status',
|
||||
PROXYSTATUS: 'proxystatus',
|
||||
SETPROXY: 'setproxy',
|
||||
CLEARPROXY: 'clearproxy',
|
||||
INJECTSCRIPT: 'INJECT_SCRIPT',
|
||||
SENDRESFROMPAGE: "SEND_RES_FROM_PAGE",
|
||||
RESTOEVALINTAB: "RES_TO_EVALINTAB",
|
||||
EVAL: 'eval',
|
||||
ECHO: 'echo',
|
||||
PROXY_STATUS: 'proxy_status',
|
||||
SET_PROXY: 'set_proxy',
|
||||
CLEAR_PROXY: 'clear_proxy',
|
||||
INJECT_SCRIPT: 'yakit_inject_script',
|
||||
TO_EXTENSION_PAGE: "yakit_to_extension_page",
|
||||
}
|
||||
|
||||
export class WebSocketManager {
|
||||
|
||||
Vendored
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user