import './style.scss'; import browser from 'webextension-polyfill'; import {Component, render} from 'preact'; import {html} from 'htm/preact'; import {decodeBase64, fragmentPrefix} from '../../utilities/share-redirect.js'; import { RedirectParameters, parseRedirect, narrowMatcherType, narrowRedirectType, } from '../../redirect/exports.js'; import storage from '../../redirect/storage.js'; type Props = Record; type State = { error: string | undefined; imported: boolean; }; class ImportButton extends Component { constructor(props: Props) { super(props); this.state = { error: undefined, imported: false, }; } importRedirect = async () => { const decoded = decodeBase64( location.hash.slice(fragmentPrefix.length), ); const invalidRedirectError = "This isn't a valid redirect. ☹"; if ( !narrowMatcherType(decoded.matcherType) || !narrowRedirectType(decoded.redirectType) || typeof decoded.matcherValue !== 'string' || typeof decoded.redirectValue !== 'string' ) { this.setState({error: invalidRedirectError}); return; } const redirect = parseRedirect({ id: -1, enabled: true, matcherType: decoded.matcherType, matcherValue: decoded.matcherValue, redirectType: decoded.redirectType, redirectValue: decoded.redirectValue, }); if (redirect === undefined) { this.setState({error: invalidRedirectError}); return; } const id = await storage.nextRedirectId(); redirect.parameters.id = id; await storage.save(redirect); this.setState({imported: true}); }; render() { const {error, imported} = this.state; if (error !== undefined) { return html`

${error}

`; } if (imported) { return html`

The redirect has been imported!

`; } return html` `; } } function main() { if (!location.hash.startsWith(fragmentPrefix)) { return; } const importRoot = document.querySelector('.re-nav-import')!; for (const child of Array.from(importRoot.children)) { child.remove(); } render(html`<${ImportButton} />`, importRoot); } main();