Add daemon host extension option

This commit is contained in:
hensm
2020-03-26 20:12:23 +00:00
parent 1bd626ffbc
commit ff0847441b
6 changed files with 35 additions and 11 deletions

View File

@@ -202,10 +202,10 @@
} }
, "optionsBridgeBackupEnabled": { , "optionsBridgeBackupEnabled": {
"message": "Enable backup daemon connection on port $numberInput$" "message": "Enable backup daemon connection on $hostPort$"
, "description": "Backup daemon checkbox label. An HTML number input is inserted inline at the numberInput substitution." , "description": "Backup daemon checkbox label. Host/port inputs are inserted inline at the hostPort substitution."
, "placeholders": { , "placeholders": {
"numberInput": { "hostPort": {
"content": "$1" "content": "$1"
} }
} }

View File

@@ -7,6 +7,7 @@ import { Options } from "./lib/options";
export default { export default {
bridgeApplicationName: APPLICATION_NAME bridgeApplicationName: APPLICATION_NAME
, bridgeBackupEnabled: false , bridgeBackupEnabled: false
, bridgeBackupHost: "localhost"
, bridgeBackupPort: 9556 , bridgeBackupPort: 9556
, mediaEnabled: true , mediaEnabled: true
, mediaOverlayEnabled: false , mediaOverlayEnabled: false

View File

@@ -4,8 +4,6 @@ import logger from "./logger";
import options from "./options"; import options from "./options";
const WEBSOCKET_DAEMON_URL_PREFIX = "ws://localhost:";
type DisconnectListener = (port: browser.runtime.Port) => void; type DisconnectListener = (port: browser.runtime.Port) => void;
type MessageListener = (message: any) => void; type MessageListener = (message: any) => void;
@@ -99,7 +97,11 @@ function connectNative (application: string) {
port.onDisconnect.addListener(async () => { port.onDisconnect.addListener(async () => {
if (!(await options.get("bridgeBackupEnabled"))) { const { bridgeBackupEnabled
, bridgeBackupHost
, bridgeBackupPort } = await options.getAll();
if (!bridgeBackupEnabled) {
portObject.error = { portObject.error = {
message: "" message: ""
}; };
@@ -114,8 +116,8 @@ function connectNative (application: string) {
if (port.error && !isNativeHostStatusKnown) { if (port.error && !isNativeHostStatusKnown) {
isNativeHostStatusKnown = true; isNativeHostStatusKnown = true;
const port = await options.get("bridgeBackupPort"); socket = new WebSocket(
socket = new WebSocket(`${WEBSOCKET_DAEMON_URL_PREFIX}${port}`); `ws://${bridgeBackupHost}:${bridgeBackupPort}`);
socket.addEventListener("open", () => { socket.addEventListener("open", () => {
// Send all messages in queue // Send all messages in queue
@@ -168,14 +170,19 @@ async function sendNativeMessage (
try { try {
return await browser.runtime.sendNativeMessage(application, message); return await browser.runtime.sendNativeMessage(application, message);
} catch { } catch {
if (!(await options.get("bridgeBackupEnabled"))) { const { bridgeBackupEnabled
, bridgeBackupHost
, bridgeBackupPort } = await options.getAll();
if (!bridgeBackupEnabled) {
throw logger.error("Bridge connection failed and backup not enabled."); throw logger.error("Bridge connection failed and backup not enabled.");
} }
const port = await options.get("bridgeBackupPort"); const port = await options.get("bridgeBackupPort");
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
const ws = new WebSocket(`${WEBSOCKET_DAEMON_URL_PREFIX}${port}`); const ws = new WebSocket(
`ws://${bridgeBackupHost}:${bridgeBackupPort}`);
ws.addEventListener("open", () => { ws.addEventListener("open", () => {
ws.send(JSON.stringify(message)); ws.send(JSON.stringify(message));

View File

@@ -16,6 +16,7 @@ const storageArea = new TypedStorageArea<{
export interface Options { export interface Options {
bridgeApplicationName: string; bridgeApplicationName: string;
bridgeBackupEnabled: boolean; bridgeBackupEnabled: boolean;
bridgeBackupHost: string;
bridgeBackupPort: number; bridgeBackupPort: number;
mediaEnabled: boolean; mediaEnabled: boolean;
mediaOverlayEnabled: boolean; mediaOverlayEnabled: boolean;

View File

@@ -118,6 +118,13 @@ export default class Bridge extends Component<BridgeProps, BridgeState> {
</div> </div>
<div className="option__label"> <div className="option__label">
{ backupMessageStart } { backupMessageStart }
<input className="bridge__backup-host"
name="bridgeBackupHost"
type="text"
required
value={ this.props.options.bridgeBackupHost }
onChange={ this.props.onChange } />
:
<input className="bridge__backup-port" <input className="bridge__backup-port"
name="bridgeBackupPort" name="bridgeBackupPort"
type="number" type="number"

View File

@@ -195,12 +195,20 @@
margin-left: 10px; margin-left: 10px;
} }
.bridge__backup-host,
.bridge__backup-port { .bridge__backup-port {
width: 75px;
margin-left: 0.5em; margin-left: 0.5em;
margin-right: 0.5em; margin-right: 0.5em;
} }
.bridge__backup-host {
width: 125px;
}
.bridge__backup-port {
width: 75px;
}
.category { .category {
border: initial; border: initial;