mirror of
https://github.com/yaklang/yaklang-chrome-extension.git
synced 2026-09-27 05:31:53 +08:00
add inject code
This commit is contained in:
+45
-34
@@ -8,41 +8,52 @@ const websocketManager = new WebSocketManager();
|
|||||||
|
|
||||||
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
|
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
|
||||||
console.log("msg", msg)
|
console.log("msg", msg)
|
||||||
if (msg.action === ActionType.CONNECT) {
|
switch (msg.action) {
|
||||||
console.info("Start to connect websocket")
|
case ActionType.CONNECT:
|
||||||
const host = msg['host'] || "127.0.0.1"
|
console.info("Start to connect websocket")
|
||||||
const port = msg['port'] || 11212
|
const host = msg['host'] || "127.0.0.1"
|
||||||
websocketManager.connectWebsocket(`ws://${host}:${port}/?token=${"a"}`)
|
const port = msg['port'] || 11212
|
||||||
} else if (msg.action === ActionType.DISCONNECT) {
|
websocketManager.connectWebsocket(`ws://${host}:${port}/?token=${"a"}`)
|
||||||
websocketManager.disconnectWebsocket()
|
break;
|
||||||
} else if (msg.action === ActionType.SETPROXY) {
|
case ActionType.DISCONNECT:
|
||||||
chrome.proxy.settings.set({
|
websocketManager.disconnectWebsocket();
|
||||||
value: {
|
break;
|
||||||
mode: "fixed_servers",
|
case ActionType.SETPROXY:
|
||||||
rules: {
|
chrome.proxy.settings.set({
|
||||||
singleProxy: {
|
value: {
|
||||||
scheme: msg.scheme,
|
mode: "fixed_servers",
|
||||||
host: msg.host,
|
rules: {
|
||||||
port: parseInt(`${msg.port}`)
|
singleProxy: {
|
||||||
|
scheme: msg.scheme,
|
||||||
|
host: msg.host,
|
||||||
|
port: parseInt(`${msg.port}`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
scope: 'regular',
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case ActionType.CLEARPROXY:
|
||||||
|
chrome.proxy.settings.clear({})
|
||||||
|
break;
|
||||||
|
case ActionType.PROXYSTATUS:
|
||||||
|
chrome.proxy.settings.get({}, function (details) {
|
||||||
|
if (details.value && details.value.mode === "fixed_servers") {
|
||||||
|
let proxyConfig = details.value.rules.singleProxy;
|
||||||
|
chrome.runtime.sendMessage({
|
||||||
|
enable: true,
|
||||||
|
proxy: `${proxyConfig.scheme}://${proxyConfig.host}:${proxyConfig.port}`
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
chrome.runtime.sendMessage({enable: false, proxy: ""})
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
scope: 'regular',
|
break;
|
||||||
})
|
|
||||||
|
|
||||||
} else if (msg.action === ActionType.CLEARPROXY) {
|
|
||||||
chrome.proxy.settings.clear({})
|
|
||||||
} else if (msg.action === ActionType.PROXYSTATUS) {
|
|
||||||
chrome.proxy.settings.get({}, function (details) {
|
|
||||||
if (details.value && details.value.mode === "fixed_servers") {
|
|
||||||
let proxyConfig = details.value.rules.singleProxy;
|
|
||||||
chrome.runtime.sendMessage({
|
|
||||||
enable: true,
|
|
||||||
proxy: `${proxyConfig.scheme}://${proxyConfig.host}:${proxyConfig.port}`
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
chrome.runtime.sendMessage({enable: false, proxy: ""})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
chrome.tabs.query({}, (tabs) => {
|
||||||
|
tabs.forEach(function (tab) {
|
||||||
|
console.log("Tab ID",tab.id); // 输出每个标签页的ID
|
||||||
|
});
|
||||||
|
});
|
||||||
+59
-1
@@ -22,6 +22,10 @@ export class WebSocketManager {
|
|||||||
this.startHeartbeat();
|
this.startHeartbeat();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.socket.onmessage = (event) => {
|
||||||
|
this.handleMessage(event.data);
|
||||||
|
};
|
||||||
|
|
||||||
this.socket.onclose = () => {
|
this.socket.onclose = () => {
|
||||||
chrome.runtime.sendMessage({action: ActionType.STATUS, connected: false});
|
chrome.runtime.sendMessage({action: ActionType.STATUS, connected: false});
|
||||||
};
|
};
|
||||||
@@ -69,4 +73,58 @@ export class WebSocketManager {
|
|||||||
isConnected() {
|
isConnected() {
|
||||||
return this.socket && this.socket.readyState === WebSocket.OPEN;
|
return this.socket && this.socket.readyState === WebSocket.OPEN;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
handleMessage(message) {
|
||||||
|
console.log("message", message)
|
||||||
|
// 解析消息
|
||||||
|
try {
|
||||||
|
// 解析从WebSocket接收到的JSON数据
|
||||||
|
let code = message;
|
||||||
|
getTabId().then((tabId) => {
|
||||||
|
// 确保tabId和code有效
|
||||||
|
if (typeof tabId === 'number' && typeof code === 'string') {
|
||||||
|
// 在指定的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);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTabId() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
|
||||||
|
if (tabs.length) {
|
||||||
|
resolve(tabs[0].id);
|
||||||
|
} else {
|
||||||
|
reject('No active tab found');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// chrome.tabs.query({}, (tabs) => {
|
||||||
|
// tabs.forEach(function (tab) {
|
||||||
|
// console.log(tab.id); // 输出每个标签页的ID
|
||||||
|
// });
|
||||||
|
// })
|
||||||
Reference in New Issue
Block a user