eval in page context

This commit is contained in:
go0p
2024-05-15 14:43:04 +08:00
committed by go0p
parent c950ede429
commit fb0a20b065
16 changed files with 322 additions and 159 deletions
+45 -11
View File
@@ -1,13 +1,47 @@
console.log('This runs at document start, before any document content is parsed.');
// 保存原始的console.log函数的引用
const originalConsoleLog = console.log;
/**
* injectScript - Inject internal script to available access to the `window`
*
* @param {type} file_path Local path of the internal script.
* @param {type} tag The tag as string, where the script will be append (default: 'body').
* @see {@link http://stackoverflow.com/questions/20499994/access-window-variable-from-content-script}
*/
function injectScript(file_path, tag) {
var node = document.getElementsByTagName(tag)[0];
if (!node) {
console.error('The "' + tag + '" tag is not available in the document.');
return;
}
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', file_path);
node.appendChild(script);
}
// 重写console.log
console.log = function (message) {
// 调用原始的console.log函数
originalConsoleLog("trying to open " + message + " window.");
};
// document.addEventListener('DOMContentLoaded', function () {
injectScript(chrome.runtime.getURL('content.js'), 'body');
window.open = function (url) {
console.log("trying to open " + url + " window.");
}
const port = chrome.runtime.connect({name: "content-script"});
port.onDisconnect.addListener(function () {
console.error("Disconnected from port.");
});
port.onMessage.addListener(function (msg) {
console.log("我听到了成功的回响 :", msg);
window.postMessage(msg, "*")
});
window.addEventListener("message", (event) => {
// We only accept messages from ourselves
if (event.source !== window) {
return;
}
if (event.data.type && (event.data.type === "FROM_PAGE")) {
console.log(event)
console.log("Content script received: " + event.data.text);
if (port && port.postMessage) {
port.postMessage(event.data.text);
}
}
}, );
// });