eval done && fix findDOMNode warning

This commit is contained in:
go0p
2024-05-14 23:02:40 +08:00
parent cb65d57cef
commit dd1b2a9513
10 changed files with 87 additions and 102 deletions
+20 -15
View File
@@ -50,24 +50,29 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
});
break;
case ActionType.INJECT_SCRIPT:
chrome.scripting.executeScript({
target: {tabId: msg.tabId},
files: ['content.js']
}).then(() => {
chrome.tabs.sendMessage(msg.tabId,
{type: ActionType.INJECT_SCRIPT, value: msg.value}
).then(response => {
console.log("response", response)
(async () => {
try {
// 注入 JS 脚本
await chrome.scripting.executeScript({
target: {tabId: msg.tabId},
files: ['content.js']
});
// 发送消息
const response = await chrome.tabs.sendMessage(msg.tabId, {
type: ActionType.INJECT_SCRIPT,
value: msg.value
});
console.log("response", response);
if (response && response.action === ActionType.TO_EXTENSION_PAGE) {
chrome.runtime.sendMessage(response)
await chrome.runtime.sendMessage(response);
}
})
}).catch(err => {
console.error('Script injection failed:', err);
});
} catch (err) {
console.error('Script or CSS injection failed:', err);
}
})();
break
case ActionType.ECHO:
console.log("Echo ", msg.result)
}
})
+26 -1
View File
@@ -3,6 +3,31 @@
return;
}
window.contentScriptInjected = true;
// 检查并插入 CSS 样式
const styleId = 'injected-css-style';
if (!document.getElementById(styleId)) {
const style = document.createElement('style');
style.id = styleId;
style.textContent = `
body {
border: 3px solid red;
position: relative; /* Ensure the body is positioned to allow the pseudo-element */
}
body::after {
content: "Injection successful";
display: block;
position: fixed;
top: 10px;
right: 10px;
background: green;
color: white;
padding: 5px 10px;
font-size: 16px;
z-index: 1000;
}
`;
document.head.appendChild(style);
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'yakit_inject_script') {
const injectedScriptURL = chrome.runtime.getURL('inject.js');
@@ -14,7 +39,7 @@
};
(document.head || document.documentElement).appendChild(script);
window.addEventListener('message', function onMessage(event) {
if (event.source !== window || event.data.type !== 'FROM_PAGE') {
if (event.source !== window || event.data.type !== 'FROM_INJECT_JS') {
return;
}
window.removeEventListener('message', onMessage);
+11 -5
View File
@@ -8,24 +8,30 @@
if (event.source !== window) {
return;
}
let result = "";
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);
window.postMessage({type: 'FROM_INJECT_JS', result: result}, '*');
break;
case 'CONTENT_EVAL_CODE':
const code = event.data.value.code;
result = eval(code);
result = (() => {
try {
return eval(code);
} catch (e) {
// console.error("Error evaluating code:", e);
return e.toString();
}
})();
// console.log("CONTENT_EVAL_CODE result: ", result);
window.postMessage({type: 'FROM_INJECT_JS', result: result}, '*');
break;
default:
break;
}
if (result && typeof result === 'object' && Object.keys(result).length > 0) {
window.postMessage({type: 'FROM_PAGE', result: result}, '*');
}
});
})();
+1 -44
View File
@@ -79,48 +79,5 @@ export class WebSocketManager {
handleMessage(message) {
console.log("message", message)
// 解析消息
// try {
// // 解析从WebSocket接收到的JSON数据
// let code = message;
// getTabId().then((tabId) => {
// // 确保tabId和code有效
// if (typeof tabId === 'number' && typeof code === 'string') {
// chrome.webNavigation.getAllFrames({tabId: tabId}, function(frames) {
// for (let frame of frames) {
// console.log(`Frame ID: ${frame.frameId} with URL: ${frame.url}`);
// }
// });
// // 在指定的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);
}
// chrome.tabs.query({}, (tabs) => {
// tabs.forEach(function (tab) {
// console.log(tab.id); // 输出每个标签页的ID
// });
// })
}