调用active tab js func

This commit is contained in:
go0p
2024-05-13 15:01:38 +08:00
parent 95bb29d216
commit fffbb2a0a0
6 changed files with 99 additions and 87 deletions
+50 -49
View File
@@ -1,54 +1,55 @@
/**
* 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) {
const node = document.getElementsByTagName(tag)[0];
if (!node) {
console.error('The "' + tag + '" tag is not available in the document.');
return;
}
// 移除之前的脚本
const oldScript = document.getElementById('yakit-injected-script');
if (oldScript) {
node.removeChild(oldScript);
}
const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', file_path);
script.setAttribute('id', 'yakit-injected-script');
function injectScript(src, id, message) {
return new Promise((resolve) => {
// Inject a script tag into the page to access methods of the window object
const script = document.createElement('script')
node.appendChild(script);
script.onload = () => {
const onMessage = ({data}) => {
console.log(data)
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)
})
}
// document.addEventListener('DOMContentLoaded', function () {
injectScript(chrome.runtime.getURL('content.js'), 'body');
chrome.runtime.onConnect.addListener(function (port) {
console.assert(port.name === "ex-to-content");
port.onMessage.addListener(function (msg) {
injectScript('content.js', 'sender', {yakitex: "123", id: "yakit", msg: msg}).then((res) => {
window.addEventListener("message", (event) => {
// if (event.source !== window) {
// return;
// }
if (event.data.type && (event.data.type === "FROM_PAGE")) {
console.log(event)
const port2 = chrome.runtime.connect({name: "content-to-ex"});
port2.postMessage(event.data.text);
port2.disconnect();
}
},);
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);
}
}
}, );
// });
port.onDisconnect.addListener(function () {
console.error("[ex-to-content] Disconnected from port.");
});
});