Bridge pages to Inscriboor service worker
This commit is contained in:
1 parent
5a618a760c
commit
881f1c5047
1 file changed
+20
@@ -0,0 +1,20 @@
|
||||
(() => {
|
||||
const script = document.createElement("script");
|
||||
script.src = chrome.runtime.getURL("injected.js");
|
||||
script.onload = () => script.remove();
|
||||
(document.documentElement || document.head).appendChild(script);
|
||||
|
||||
let port;
|
||||
const connect = () => {
|
||||
port = chrome.runtime.connect({ name: "inscriboor-provider" });
|
||||
port.onMessage.addListener(message => window.postMessage({ source: "inscriboor-extension", ...message }, "*"));
|
||||
port.onDisconnect.addListener(() => { port = null; });
|
||||
};
|
||||
connect();
|
||||
|
||||
window.addEventListener("message", event => {
|
||||
if (event.source !== window || event.data?.source !== "inscriboor-page") return;
|
||||
if (!port) connect();
|
||||
port.postMessage({ id:event.data.id, method:event.data.method, params:event.data.params, origin:location.origin });
|
||||
});
|
||||
})();
|
||||
Reference in new issue
Block a user