/* tslint:disable:max-line-length */ "use strict"; import React, { Component } from "react"; import ReactDOM from "react-dom"; import defaultOptions from "../../defaultOptions"; import Bridge from "./Bridge"; import EditableList from "./EditableList"; import bridge, { BridgeInfo } from "../../lib/bridge"; import options, { Options } from "../../lib/options"; import { REMOTE_MATCH_PATTERN_REGEX } from "../../lib/utils"; import { ReceiverSelectorType } from "../../background/receiverSelector"; const _ = browser.i18n.getMessage; // macOS styles browser.runtime.getPlatformInfo() .then(platformInfo => { const link = document.createElement("link"); link.rel = "stylesheet"; switch (platformInfo.os) { case "mac": { link.href = "styles/mac.css"; break; } // Fix issue with input[type="number"] height case "linux": { link.href = "styles/linux.css"; const input = document.createElement("input"); const inputWrapper = document.createElement("div"); inputWrapper.append(input); document.documentElement.append(inputWrapper); input.type = "text"; const textInputHeight = window.getComputedStyle(input).height; input.type = "number"; const numberInputHeight = window.getComputedStyle(input).height; inputWrapper.remove(); if (numberInputHeight !== textInputHeight) { const style = document.createElement("style"); style.textContent = ` input[type="number"] { height: ${textInputHeight}; } `; document.body.append(style); } break; } } if (link.href) { document.head.appendChild(link); } }); function getInputValue (input: HTMLInputElement) { switch (input.type) { case "checkbox": return input.checked; case "number": return parseFloat(input.value); default: return input.value; } } interface OptionsAppState { hasLoaded: boolean; options: Options; bridgeInfo: BridgeInfo; platform: string; bridgeLoading: boolean; isFormValid: boolean; hasSaved: boolean; } class OptionsApp extends Component<{}, OptionsAppState> { private form: HTMLFormElement; constructor (props: {}) { super(props); this.state = { hasLoaded: false , options: null , bridgeInfo: null , platform: null , bridgeLoading: true , isFormValid: true , hasSaved: false }; this.handleReset = this.handleReset.bind(this); this.handleFormSubmit = this.handleFormSubmit.bind(this); this.handleFormChange = this.handleFormChange.bind(this); this.handleInputChange = this.handleInputChange.bind(this); this.handleWhitelistChange = this.handleWhitelistChange.bind(this); this.handleReceiverSelectorTypeChange = this.handleReceiverSelectorTypeChange.bind(this); this.getWhitelistItemPatternError = this.getWhitelistItemPatternError.bind(this); } public async componentDidMount () { this.setState({ hasLoaded: true , options: await options.getAll() }); const bridgeInfo = await bridge.getInfo(); const { os } = await browser.runtime.getPlatformInfo(); this.setState({ bridgeInfo , platform: os , bridgeLoading: false }); } public render () { if (!this.state.hasLoaded) { return; } return (
{ this.form = form; }} onSubmit={ this.handleFormSubmit } onChange={ this.handleFormChange }>

{ _("optionsMediaCategoryName") }

{ _("optionsMediaCategoryDescription") }


{ _("optionsMirroringCategoryName") }

{ _("optionsMirroringCategoryDescription") }

{ _("optionsReceiverSelectorCategoryName") }

{ _("optionsReceiverSelectorCategoryDescription") }

{ this.state.platform === "mac" && }

{ _("optionsUserAgentWhitelistCategoryName") }

{ _("optionsUserAgentWhitelistCategoryDescription") }

{ _("optionsUserAgentWhitelistContent") }
{ this.state.hasSaved && _("optionsSaved") }
); } private handleReset () { this.setState({ options: { ...defaultOptions } }); } private async handleFormSubmit (ev: React.FormEvent) { ev.preventDefault(); this.form.reportValidity(); try { await options.setAll(this.state.options); this.setState({ hasSaved: true }, () => { window.setTimeout(() => { this.setState({ hasSaved: false }); }, 1000); }); } catch (err) { console.error("Failed to save options"); } } private handleFormChange (ev: React.FormEvent) { ev.preventDefault(); this.setState({ isFormValid: this.form.checkValidity() }); } private handleInputChange (ev: React.ChangeEvent) { this.setState(currentState => { currentState.options[ev.target.name] = getInputValue(ev.target); return currentState; }); } private handleReceiverSelectorTypeChange ( ev: React.ChangeEvent) { this.setState(currentState => { currentState.options[ev.target.name] = parseInt(ev.target.value); return currentState; }); } private handleWhitelistChange (whitelist: string[]) { this.setState(currentState => { currentState.options.userAgentWhitelist = whitelist; return currentState; }); } private getWhitelistItemPatternError (info: string): string { return _("optionsUserAgentWhitelistInvalidMatchPattern", info); } } ReactDOM.render( , document.querySelector("#root"));