| export default class MainWorldContentScriptBridgeServer { |
| constructor(CSTarget, MWTarget) { |
| if (!CSTarget || !MWTarget) { |
| `[MWOptionsWatcherServer] CSTarget and MWTarget are compulsory.`); |
| this.CSTarget = CSTarget; |
| this.MWTarget = MWTarget; |
| // Handler should be an action of the form (uuid, action, request) => {...}. |
| window.addEventListener('message', e => this.#handleMessage(e)); |
| const uuid = e.data?.uuid; |
| if (e.source !== window || e.data?.target !== this.CSTarget || !uuid) |
| // If chrome.runtime.id is undefined, then this content script belongs to a |
| // dead extension (see https://stackoverflow.com/a/69603416). |
| if (typeof chrome.runtime.id === 'undefined') { |
| '[MainWorldContentScriptBridgeServer] Not handling message because this is a dead server.'); |
| const action = e.data?.action; |
| const request = e.data?.request; |
| return this.handler(uuid, action, request); |
| _respond(uuid, response) { |
| window.postMessage(data, window.origin); |