import browser from 'webextension-polyfill'; import { BackgroundRequestMessage } from './background-common'; // Inject the script that will provide window.nostr // The script needs to run before any other scripts from the real // page run (and maybe check for window.nostr). const script = document.createElement('script'); script.setAttribute('async', 'false'); script.setAttribute('type', 'text/javascript'); script.setAttribute('src', browser.runtime.getURL('smesh-signer-extension.js')); (document.head || document.documentElement).appendChild(script); // listen for messages from that script window.addEventListener('message', async (message) => { // We will also receive our own messages, that we sent. // We have to ignore them (they will not have a params field). if (message.source !== window) return; if (!message.data) return; if (!message.data.params) return; if (message.data.ext !== 'smesh-signer') return; // pass on to background let response; try { const request: BackgroundRequestMessage = { method: message.data.method, params: message.data.params, host: location.host, }; response = await browser.runtime.sendMessage(request); } catch (error) { response = { error }; } // return response window.postMessage( { id: message.data.id, ext: 'smesh-signer', response }, message.origin ); }); // Relay MLS push messages from background → page. // The extension background pushes events (publish, DM, subscribe, status) // when the marmot WASM produces output asynchronously. browser.runtime.onMessage.addListener((message: any) => { if (message?.ext === 'smesh-signer' && message?.type === 'mls-push') { window.postMessage(message, '*'); } });