Files
fx_cast/ext/src/shim/contentBridge.ts
Matt Hensman ba8c28bf39 Restructure background script (#70)
Splits some background script functionality into separate modules:
 - Receiver selector handling is moved to ./SelectorManager.
 - Status bridge handling is moved to ./StatusManager.
 - Menu creation and updates are handled in ./createMenus.
 - Shim creation is handled in ./createShim.

TypedEventTarget allows EventTarget-derived classes to export typed events.

Options type definition is moved to ./lib/options, module assumes more responsibility for update handling and provides a "changed" event.

Private cast._requestSession method allows bypassing receiver selector.
2019-07-26 00:09:51 +01:00

35 lines
1004 B
TypeScript

"use strict";
import { loadScript } from "../lib/utils";
import { Message } from "../types";
import { onMessageResponse, sendMessage } from "./eventMessageChannel";
const { isFramework }
: { isFramework: boolean } = (window as any);
/**
* Framework API library requires webcomponents for the cast
* button custom element (<google-cast-launcher>).
*/
if (isFramework) {
loadScript(browser.runtime.getURL("vendor/webcomponents-lite.js"));
}
// Message port to background script
export const backgroundPort = browser.runtime.connect({ name: "shim" });
const forwardToShim = (message: Message) => sendMessage(message);
const forwardToMain = (message: Message) => backgroundPort.postMessage(message);
// Add message listeners
backgroundPort.onMessage.addListener(forwardToShim);
const listener = onMessageResponse(forwardToMain);
// Remove listeners
backgroundPort.onDisconnect.addListener(() => {
backgroundPort.onMessage.removeListener(forwardToShim);
listener.disconnect();
});