This commit is contained in:
go0p
2024-05-15 14:43:04 +08:00
committed by go0p
parent fb0a20b065
commit 0c8f82b441
3 changed files with 32 additions and 14 deletions
+9 -2
View File
@@ -6,14 +6,21 @@
* @see {@link http://stackoverflow.com/questions/20499994/access-window-variable-from-content-script} * @see {@link http://stackoverflow.com/questions/20499994/access-window-variable-from-content-script}
*/ */
function injectScript(file_path, tag) { function injectScript(file_path, tag) {
var node = document.getElementsByTagName(tag)[0]; const node = document.getElementsByTagName(tag)[0];
if (!node) { if (!node) {
console.error('The "' + tag + '" tag is not available in the document.'); console.error('The "' + tag + '" tag is not available in the document.');
return; return;
} }
var script = document.createElement('script'); // 移除之前的脚本
const oldScript = document.getElementById('yakit-injected-script');
if (oldScript) {
node.removeChild(oldScript);
}
const script = document.createElement('script');
script.setAttribute('type', 'text/javascript'); script.setAttribute('type', 'text/javascript');
script.setAttribute('src', file_path); script.setAttribute('src', file_path);
script.setAttribute('id', 'yakit-injected-script');
node.appendChild(script); node.appendChild(script);
} }
+11 -10
View File
@@ -1,6 +1,7 @@
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";
interface EvalInTabProps { interface EvalInTabProps {
} }
@@ -23,23 +24,23 @@ export const EvalInTab: React.FC<EvalInTabProps> = () => {
chrome.runtime.onConnect.addListener(onConnectListener); chrome.runtime.onConnect.addListener(onConnectListener);
return () => { // return () => {
chrome.runtime.onConnect.removeListener(onConnectListener); // chrome.runtime.onConnect.removeListener(onConnectListener);
}; // };
}, [inputData]); }, [inputData]);
const handleClick = () => { const handleClick = async () => {
if (!isScriptInjected) {
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {
console.log(tabs[0].title);
chrome.scripting.executeScript({ chrome.scripting.executeScript({
target: { tabId: tabs[0].id }, target: {tabId: await wsc.getTabId()},
files: ['inject.js'], files: ['inject.js'],
}).then(injectResults => { }).then(injectResults => {
console.log(injectResults) console.log(injectResults)
setIsScriptInjected(true);
}); });
}); }
console.log("injected script")
}; };
const handleInputChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => { const handleInputChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
+11 -1
View File
@@ -50,5 +50,15 @@ export namespace wsc {
chrome.runtime.sendMessage({action: ActionType.CLEARPROXY}) chrome.runtime.sendMessage({action: ActionType.CLEARPROXY})
} }
export function getTabId() {
return new Promise<number>((resolve, reject) => {
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {
if (tabs.length) {
resolve(tabs[0].id);
} else {
reject('No active tab found');
}
});
});
}
} }