From a39a1fa9e4700d1189dfc5073b5fcb2557965671 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 22 Aug 2019 19:44:31 -0400 Subject: Add support for Edge browser --- ext/bg/background.html | 2 ++ ext/bg/context.html | 2 ++ ext/bg/js/search.js | 2 +- ext/bg/js/settings.js | 31 ++++++++++++++------------- ext/bg/js/util.js | 16 +++++++++++++- ext/bg/search.html | 2 ++ ext/bg/settings.html | 2 ++ ext/fg/float.html | 2 ++ ext/fg/js/float.js | 2 +- ext/manifest.json | 6 +++++- ext/mixed/js/extension.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 100 insertions(+), 20 deletions(-) create mode 100644 ext/mixed/js/extension.js (limited to 'ext') diff --git a/ext/bg/background.html b/ext/bg/background.html index 3262f2a1..5978f10f 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -10,6 +10,8 @@ + + diff --git a/ext/bg/context.html b/ext/bg/context.html index 01b4fb30..198ccd42 100644 --- a/ext/bg/context.html +++ b/ext/bg/context.html @@ -32,6 +32,8 @@ + + diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index f08f22da..a3382398 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -25,7 +25,7 @@ class DisplaySearch extends Display { this.query = $('#query').on('input', this.onSearchInput.bind(this)); this.intro = $('#intro'); - this.dependencies = {...this.dependencies, ...{docRangeFromPoint, docSentenceExtract}}; + this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); window.wanakana.bind(this.query.get(0)); } diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 75082f3e..e542118c 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -116,7 +116,7 @@ async function formMainDictionaryOptionsPopulate(options) { select.append($('')); let mainDictionary = ''; - for (const dictRow of await utilDatabaseSummarize()) { + for (const dictRow of toIterable(await utilDatabaseSummarize())) { if (dictRow.sequenced) { select.append($(``)); if (dictRow.title === options.general.mainDictionary) { @@ -314,12 +314,12 @@ async function dictionaryGroupsPopulate(options) { const dictGroups = $('#dict-groups').empty(); const dictWarning = $('#dict-warning').hide(); - const dictRows = await utilDatabaseSummarize(); + const dictRows = toIterable(await utilDatabaseSummarize()); if (dictRows.length === 0) { dictWarning.show(); } - for (const dictRow of dictRowsSort(dictRows, options)) { + for (const dictRow of toIterable(dictRowsSort(dictRows, options))) { const dictOptions = options.dictionaries[dictRow.title] || { enabled: false, priority: 0, @@ -581,20 +581,19 @@ async function onAnkiFieldTemplatesReset(e) { */ async function getBrowser() { - if (typeof chrome !== "undefined") { - if (typeof browser !== "undefined") { - try { - const info = await browser.runtime.getBrowserInfo(); - if (info.name === "Fennec") { - return "firefox-mobile"; - } - } catch (e) { } - return "firefox"; - } else { - return "chrome"; - } + if (EXTENSION_IS_BROWSER_EDGE) { + return 'edge'; + } + if (typeof browser !== 'undefined') { + try { + const info = await browser.runtime.getBrowserInfo(); + if (info.name === 'Fennec') { + return 'firefox-mobile'; + } + } catch (e) { } + return 'firefox'; } else { - return "edge"; + return 'chrome'; } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 34b06ddb..3dc7c900 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -87,6 +87,20 @@ function utilDatabasePurge() { return utilBackend().translator.database.purge(); } -function utilDatabaseImport(data, progress, exceptions) { +async function utilDatabaseImport(data, progress, exceptions) { + // Edge cannot read data on the background page due to the File object + // being created from a different window. Read on the same page instead. + if (EXTENSION_IS_BROWSER_EDGE) { + data = await utilReadFile(data); + } return utilBackend().translator.database.importDictionary(data, progress, exceptions); } + +function utilReadFile(file) { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = () => resolve(reader.result); + reader.onerror = () => reject(reader.error); + reader.readAsBinaryString(file); + }); +} diff --git a/ext/bg/search.html b/ext/bg/search.html index ce156578..05c0daab 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -37,6 +37,8 @@ + + diff --git a/ext/bg/settings.html b/ext/bg/settings.html index c6677018..0704140e 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -399,6 +399,8 @@ + + diff --git a/ext/fg/float.html b/ext/fg/float.html index 07f2d58b..0133e653 100644 --- a/ext/fg/float.html +++ b/ext/fg/float.html @@ -34,6 +34,8 @@ + + diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 090839a1..c0ec8a15 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -23,7 +23,7 @@ class DisplayFloat extends Display { this.autoPlayAudioTimer = null; this.styleNode = null; - this.dependencies = {...this.dependencies, ...{docRangeFromPoint, docSentenceExtract}}; + this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); $(window).on('message', utilAsync(this.onMessage.bind(this))); } diff --git a/ext/manifest.json b/ext/manifest.json index e3d7718b..baec58f6 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -11,10 +11,14 @@ }, "author": "Alex Yatskov", - "background": {"page": "bg/background.html"}, + "background": { + "page": "bg/background.html", + "persistent": true + }, "content_scripts": [{ "matches": ["http://*/*", "https://*/*", "file://*/*"], "js": [ + "mixed/js/extension.js", "fg/js/api.js", "fg/js/document.js", "fg/js/popup.js", diff --git a/ext/mixed/js/extension.js b/ext/mixed/js/extension.js new file mode 100644 index 00000000..d7085e5b --- /dev/null +++ b/ext/mixed/js/extension.js @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2019 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function toIterable(value) { + if (typeof Symbol !== 'undefined' && typeof value[Symbol.iterator] !== 'undefined') { + return value; + } + + const array = JSON.parse(JSON.stringify(value)); + return Array.isArray(array) ? array : []; +} + +function extensionHasChrome() { + try { + return typeof chrome === 'object' && chrome !== null; + } catch (e) { + return false; + } +} + +function extensionHasBrowser() { + try { + return typeof browser === 'object' && browser !== null; + } catch (e) { + return false; + } +} + +const EXTENSION_IS_BROWSER_EDGE = ( + extensionHasBrowser() && + (!extensionHasChrome() || (typeof chrome.runtime === 'undefined' && typeof browser.runtime !== 'undefined')) +); + +if (EXTENSION_IS_BROWSER_EDGE) { + // Edge does not have chrome defined. + chrome = browser; +} -- cgit v1.2.3