mirror of
https://github.com/yaklang/yaklang-chrome-extension.git
synced 2026-09-26 21:21:53 +08:00
fix: 注入js
This commit is contained in:
+19
-33
@@ -1,4 +1,4 @@
|
|||||||
import {ActionType, WebSocketManager} from './socket.js';
|
import { ActionType, WebSocketManager } from './socket.js';
|
||||||
|
|
||||||
|
|
||||||
console.info("Chrome Extenstion Background is loaded")
|
console.info("Chrome Extenstion Background is loaded")
|
||||||
@@ -45,10 +45,26 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
|
|||||||
proxy: `${proxyConfig.scheme}://${proxyConfig.host}:${proxyConfig.port}`
|
proxy: `${proxyConfig.scheme}://${proxyConfig.host}:${proxyConfig.port}`
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
chrome.runtime.sendMessage({enable: false, proxy: ""})
|
chrome.runtime.sendMessage({ enable: false, proxy: "" })
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
case ActionType.INJECTSCRIPT:
|
||||||
|
chrome.scripting.executeScript({
|
||||||
|
target: { tabId: msg.tabId },
|
||||||
|
files: ['content.js']
|
||||||
|
}).then(() => {
|
||||||
|
chrome.tabs.sendMessage(msg.tabId, { type: 'INJECT_CODE', value: msg.value });
|
||||||
|
}).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:
|
case ActionType.ECHO:
|
||||||
console.log("Echo ", msg.result)
|
console.log("Echo ", msg.result)
|
||||||
}
|
}
|
||||||
@@ -56,7 +72,7 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const pageFunction = (code) => {
|
const pageFunction = (code) => {
|
||||||
chrome.runtime.sendMessage({code}, response => {
|
chrome.runtime.sendMessage({ code }, response => {
|
||||||
if (response && response.success) {
|
if (response && response.success) {
|
||||||
console.log('Result:', response.result);
|
console.log('Result:', response.result);
|
||||||
} else {
|
} else {
|
||||||
@@ -64,33 +80,3 @@ const pageFunction = (code) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// chrome.runtime.onInstalled.addListener(() => {
|
|
||||||
// chrome.scripting.registerContentScripts([{
|
|
||||||
// id: 'myScript',
|
|
||||||
// matches: ['<all_urls>'], // 根据需要调整匹配模式
|
|
||||||
// js: ['content2.js'],
|
|
||||||
// // world: 'MAIN',
|
|
||||||
// // runAt: 'document_start'
|
|
||||||
// }], (result) => {
|
|
||||||
// if (chrome.runtime.lastError) {
|
|
||||||
// console.error(`Error registering script: ${chrome.runtime.lastError.message}`);
|
|
||||||
// } else {
|
|
||||||
// console.log('Script registered', result);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
|
|
||||||
chrome.runtime.onConnect.addListener(function (port) {
|
|
||||||
console.assert(port.name === "content-to-ex");
|
|
||||||
if (!port.hasListener) {
|
|
||||||
port.onMessage.addListener(function (msg) {
|
|
||||||
console.log("[content-to-ex]", msg)
|
|
||||||
});
|
|
||||||
port.onDisconnect.addListener(function () {
|
|
||||||
console.error("[content-to-ex] Disconnected from port.");
|
|
||||||
});
|
|
||||||
// port.postMessage({action: "TEST POST MESSAGE"});
|
|
||||||
port.hasListener = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|||||||
+28
-16
@@ -1,18 +1,30 @@
|
|||||||
console.log("Loaded content.js")
|
(() => {
|
||||||
|
if (window.contentScriptInjected) {
|
||||||
if (!window.hasAddedMessageListener) {
|
return;
|
||||||
window.addEventListener("message", (event) => {
|
}
|
||||||
if (event.data?.yakitex?.msg && (event.data.yakitex.msg.type === "TEST")) {
|
window.contentScriptInjected = true;
|
||||||
const name = event.data.yakitex.msg.fn_name;
|
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||||
const args = event.data.yakitex.msg.args;
|
if (request.type === 'INJECT_CODE') {
|
||||||
const a = (fn, args) => {
|
const injectedScriptURL = chrome.runtime.getURL('inject.js');
|
||||||
return window[fn](args);
|
const script = document.createElement('script');
|
||||||
}
|
script.src = injectedScriptURL;
|
||||||
const zz = a(name, args);
|
script.onload = () => {
|
||||||
console.log("res ", zz);
|
window.postMessage({ type: 'CALL_FUNCTION', value: request.value }, '*');
|
||||||
window.postMessage({type: "FROM_PAGE", text: zz}, "*");
|
script.remove();
|
||||||
return
|
};
|
||||||
|
(document.head || document.documentElement).appendChild(script);
|
||||||
|
window.addEventListener('message', function onMessage(event) {
|
||||||
|
if (event.source !== window || event.data.type !== 'FROM_PAGE') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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 });
|
||||||
|
});
|
||||||
|
// Return true to indicate that the response will be sent asynchronously
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
window.hasAddedMessageListener = true;
|
})()
|
||||||
}
|
|
||||||
|
|||||||
+14
-60
@@ -1,63 +1,17 @@
|
|||||||
function injectScript(src, id, message) {
|
(() => {
|
||||||
return new Promise((resolve) => {
|
if (window.injectedMessageListener) {
|
||||||
// Inject a script tag into the page to access methods of the window object
|
return;
|
||||||
const script = document.createElement('script')
|
}
|
||||||
|
window.injectedMessageListener = true;
|
||||||
|
|
||||||
script.onload = () => {
|
window.addEventListener('message', function onMessage(event) {
|
||||||
const onMessage = ({ data }) => {
|
if (event.source !== window || event.data.type !== 'CALL_FUNCTION') {
|
||||||
console.log(data)
|
return;
|
||||||
if (!data.yakitex || !data.yakitex.msg) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
window.removeEventListener('message', onMessage)
|
|
||||||
|
|
||||||
// resolve(data.yakitex.msg)
|
|
||||||
|
|
||||||
script.remove()
|
|
||||||
}
|
|
||||||
|
|
||||||
window.addEventListener('message', onMessage)
|
|
||||||
|
|
||||||
window.postMessage({
|
|
||||||
yakitex: message,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
script.setAttribute('src', chrome.runtime.getURL(src))
|
|
||||||
|
|
||||||
document.body.appendChild(script)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!window.hasAddedMessageListener2) {
|
|
||||||
window.addEventListener("message", (event) => {
|
|
||||||
if (event.data.type && (event.data.type === "FROM_PAGE")) {
|
|
||||||
const port = chrome.runtime.connect({ name: "content-to-ex" });
|
|
||||||
port.postMessage(event.data.text);
|
|
||||||
port.disconnect();
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
window.hasAddedMessageListener2 = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function connectAddListenerFun(port) {
|
|
||||||
console.assert(port.name === "ex-to-content");
|
|
||||||
if (!port.hasListener) {
|
|
||||||
port.onMessage.addListener(function (msg) {
|
|
||||||
console.log(111111111111111);
|
|
||||||
injectScript('content.js', 'sender', { yakitex: "123", id: "yakit", msg: msg })
|
|
||||||
});
|
|
||||||
|
|
||||||
port.onDisconnect.addListener(function () {
|
|
||||||
console.error("[ex-to-content] Disconnected from port.");
|
|
||||||
});
|
|
||||||
port.hasListener = true
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!chrome.runtime.onConnect.hasListener(connectAddListenerFun)) {
|
const value = event.data.value;
|
||||||
chrome.runtime.onConnect.addListener(connectAddListenerFun);
|
const result = window[value.fn_name](value.args);
|
||||||
}
|
window.postMessage({ type: 'FROM_PAGE', result: result }, '*');
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
|
|
||||||
"web_accessible_resources": [
|
"web_accessible_resources": [
|
||||||
{
|
{
|
||||||
"resources": ["content.js"],
|
"resources": ["inject.js"],
|
||||||
"matches": ["<all_urls>"],
|
"matches": ["<all_urls>"],
|
||||||
"use_dynamic_url": true
|
"use_dynamic_url": true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ export const ActionType = {
|
|||||||
PROXYSTATUS: 'proxystatus',
|
PROXYSTATUS: 'proxystatus',
|
||||||
SETPROXY: 'setproxy',
|
SETPROXY: 'setproxy',
|
||||||
CLEARPROXY: 'clearproxy',
|
CLEARPROXY: 'clearproxy',
|
||||||
|
INJECTSCRIPT: 'INJECT_SCRIPT',
|
||||||
|
SENDRESFROMPAGE: "SEND_RES_FROM_PAGE",
|
||||||
|
RESTOEVALINTAB: "RES_TO_EVALINTAB",
|
||||||
EVAL: 'eval',
|
EVAL: 'eval',
|
||||||
ECHO: 'echo',
|
ECHO: 'echo',
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,72 +1,64 @@
|
|||||||
import React, {useEffect, useState} from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import {Button} from "antd";
|
import { Button } from "antd";
|
||||||
import TextArea from "antd/lib/input/TextArea";
|
import TextArea from "antd/lib/input/TextArea";
|
||||||
import {wsc} from "@network/chrome";
|
import { wsc } from "@network/chrome";
|
||||||
|
import { ActionType } from "../../public/socket";
|
||||||
|
|
||||||
interface EvalInTabProps {
|
interface EvalInTabProps {}
|
||||||
}
|
|
||||||
|
|
||||||
export const EvalInTab: React.FC<EvalInTabProps> = () => {
|
export const EvalInTab: React.FC<EvalInTabProps> = () => {
|
||||||
const [inputData, setInputData] = useState("");
|
const [inputData, setInputData] = useState("");
|
||||||
const [port, setPort] = useState<chrome.runtime.Port>()
|
|
||||||
|
|
||||||
// useEffect(() => {
|
// useEffect(() => {
|
||||||
// // const onConnectListener = (port: chrome.runtime.Port) => {
|
// // const onConnectListener = (port: chrome.runtime.Port) => {
|
||||||
// // console.assert(port.name === "content-script");
|
// // console.assert(port.name === "content-script");
|
||||||
// // port.onMessage.addListener(function (msg) {
|
// // port.onMessage.addListener(function (msg) {
|
||||||
// // console.log("on connect", msg)
|
// // console.log("on connect", msg)
|
||||||
// // });
|
// // });
|
||||||
// // port.onDisconnect.addListener(function () {
|
// // port.onDisconnect.addListener(function () {
|
||||||
// // console.error("Disconnected from port.");
|
// // console.error("Disconnected from port.");
|
||||||
// // });
|
// // });
|
||||||
// // port.postMessage({type: "TEST", fn_name: "Encrypt", args: inputData});
|
// // port.postMessage({type: "TEST", fn_name: "Encrypt", args: inputData});
|
||||||
// // };
|
// // };
|
||||||
// //
|
// //
|
||||||
// // chrome.runtime.onConnect.addListener(onConnectListener);
|
// // chrome.runtime.onConnect.addListener(onConnectListener);
|
||||||
//
|
//
|
||||||
// // return () => {
|
// // return () => {
|
||||||
// // chrome.runtime.onConnect.removeListener(onConnectListener);
|
// // chrome.runtime.onConnect.removeListener(onConnectListener);
|
||||||
// // };
|
// // };
|
||||||
// }, [inputData]);
|
// }, [inputData]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const injectFun = async () => {
|
wsc.onWSCMessage((message) => {
|
||||||
const tabId = await wsc.getTabId();
|
if (message.action === ActionType.RESTOEVALINTAB) {
|
||||||
chrome.scripting.executeScript({
|
console.log("res:", message.result);
|
||||||
target: {tabId: tabId},
|
}
|
||||||
files: ['inject.js'],
|
});
|
||||||
}).then(() => {
|
}, []);
|
||||||
const newPort = chrome.tabs.connect(tabId, {name: "ex-to-content"})
|
|
||||||
setPort(newPort)
|
|
||||||
newPort.onMessage.addListener(function (msg) {
|
|
||||||
console.log(msg)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
injectFun()
|
|
||||||
return () => {
|
|
||||||
if (port) {
|
|
||||||
port.disconnect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const handleClick = async () => {
|
const handleClick = async () => {
|
||||||
port.postMessage({type: "TEST", fn_name: "Encrypt", args: inputData});
|
try {
|
||||||
};
|
const tabId = await wsc.getTabId();
|
||||||
|
chrome.runtime.sendMessage({
|
||||||
|
action: ActionType.INJECTSCRIPT,
|
||||||
|
tabId: tabId,
|
||||||
|
value: { fn_name: "Encrypt", args: inputData },
|
||||||
|
});
|
||||||
|
} catch (error) {}
|
||||||
|
};
|
||||||
|
|
||||||
const handleInputChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
const handleInputChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||||
setInputData(event.target.value);
|
setInputData(event.target.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<TextArea
|
<TextArea
|
||||||
value={inputData}
|
value={inputData}
|
||||||
onChange={handleInputChange}
|
onChange={handleInputChange}
|
||||||
placeholder="Enter your expression (e.g., 22 * 33)"
|
placeholder="Enter your expression (e.g., 22 * 33)"
|
||||||
/>
|
/>
|
||||||
<Button onClick={handleClick}>eval in tab me</Button>
|
<Button onClick={handleClick}>eval in tab me</Button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user