Merge pull request #188 from toasted-nutbread/edge-support

Add support for Edge browser
This commit is contained in:
Alex Yatskov 2019-08-28 19:41:30 -07:00 committed by GitHub
commit cd75f5b97d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 100 additions and 20 deletions

View File

@ -10,6 +10,8 @@
<script src="/mixed/lib/jszip.min.js"></script> <script src="/mixed/lib/jszip.min.js"></script>
<script src="/mixed/lib/wanakana.min.js"></script> <script src="/mixed/lib/wanakana.min.js"></script>
<script src="/mixed/js/extension.js"></script>
<script src="/bg/js/anki.js"></script> <script src="/bg/js/anki.js"></script>
<script src="/bg/js/api.js"></script> <script src="/bg/js/api.js"></script>
<script src="/bg/js/audio.js"></script> <script src="/bg/js/audio.js"></script>

View File

@ -32,6 +32,8 @@
<script src="/mixed/lib/jquery.min.js"></script> <script src="/mixed/lib/jquery.min.js"></script>
<script src="/mixed/lib/bootstrap-toggle/bootstrap-toggle.min.js"></script> <script src="/mixed/lib/bootstrap-toggle/bootstrap-toggle.min.js"></script>
<script src="/mixed/js/extension.js"></script>
<script src="/bg/js/api.js"></script> <script src="/bg/js/api.js"></script>
<script src="/bg/js/options.js"></script> <script src="/bg/js/options.js"></script>
<script src="/bg/js/util.js"></script> <script src="/bg/js/util.js"></script>

View File

@ -25,7 +25,7 @@ class DisplaySearch extends Display {
this.query = $('#query').on('input', this.onSearchInput.bind(this)); this.query = $('#query').on('input', this.onSearchInput.bind(this));
this.intro = $('#intro'); this.intro = $('#intro');
this.dependencies = {...this.dependencies, ...{docRangeFromPoint, docSentenceExtract}}; this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract});
window.wanakana.bind(this.query.get(0)); window.wanakana.bind(this.query.get(0));
} }

View File

@ -116,7 +116,7 @@ async function formMainDictionaryOptionsPopulate(options) {
select.append($('<option class="text-muted" value="">Not selected</option>')); select.append($('<option class="text-muted" value="">Not selected</option>'));
let mainDictionary = ''; let mainDictionary = '';
for (const dictRow of await utilDatabaseSummarize()) { for (const dictRow of toIterable(await utilDatabaseSummarize())) {
if (dictRow.sequenced) { if (dictRow.sequenced) {
select.append($(`<option value="${dictRow.title}">${dictRow.title}</option>`)); select.append($(`<option value="${dictRow.title}">${dictRow.title}</option>`));
if (dictRow.title === options.general.mainDictionary) { if (dictRow.title === options.general.mainDictionary) {
@ -314,12 +314,12 @@ async function dictionaryGroupsPopulate(options) {
const dictGroups = $('#dict-groups').empty(); const dictGroups = $('#dict-groups').empty();
const dictWarning = $('#dict-warning').hide(); const dictWarning = $('#dict-warning').hide();
const dictRows = await utilDatabaseSummarize(); const dictRows = toIterable(await utilDatabaseSummarize());
if (dictRows.length === 0) { if (dictRows.length === 0) {
dictWarning.show(); dictWarning.show();
} }
for (const dictRow of dictRowsSort(dictRows, options)) { for (const dictRow of toIterable(dictRowsSort(dictRows, options))) {
const dictOptions = options.dictionaries[dictRow.title] || { const dictOptions = options.dictionaries[dictRow.title] || {
enabled: false, enabled: false,
priority: 0, priority: 0,
@ -581,20 +581,19 @@ async function onAnkiFieldTemplatesReset(e) {
*/ */
async function getBrowser() { async function getBrowser() {
if (typeof chrome !== "undefined") { if (EXTENSION_IS_BROWSER_EDGE) {
if (typeof browser !== "undefined") { return 'edge';
}
if (typeof browser !== 'undefined') {
try { try {
const info = await browser.runtime.getBrowserInfo(); const info = await browser.runtime.getBrowserInfo();
if (info.name === "Fennec") { if (info.name === 'Fennec') {
return "firefox-mobile"; return 'firefox-mobile';
} }
} catch (e) { } } catch (e) { }
return "firefox"; return 'firefox';
} else { } else {
return "chrome"; return 'chrome';
}
} else {
return "edge";
} }
} }

View File

@ -87,6 +87,20 @@ function utilDatabasePurge() {
return utilBackend().translator.database.purge(); 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); 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);
});
}

View File

@ -37,6 +37,8 @@
<script src="/mixed/lib/jquery.min.js"></script> <script src="/mixed/lib/jquery.min.js"></script>
<script src="/mixed/lib/wanakana.min.js"></script> <script src="/mixed/lib/wanakana.min.js"></script>
<script src="/mixed/js/extension.js"></script>
<script src="/bg/js/api.js"></script> <script src="/bg/js/api.js"></script>
<script src="/bg/js/audio.js"></script> <script src="/bg/js/audio.js"></script>
<script src="/bg/js/dictionary.js"></script> <script src="/bg/js/dictionary.js"></script>

View File

@ -399,6 +399,8 @@
<script src="/mixed/lib/bootstrap/js/bootstrap.min.js"></script> <script src="/mixed/lib/bootstrap/js/bootstrap.min.js"></script>
<script src="/mixed/lib/handlebars.min.js"></script> <script src="/mixed/lib/handlebars.min.js"></script>
<script src="/mixed/js/extension.js"></script>
<script src="/bg/js/anki.js"></script> <script src="/bg/js/anki.js"></script>
<script src="/bg/js/api.js"></script> <script src="/bg/js/api.js"></script>
<script src="/bg/js/dictionary.js"></script> <script src="/bg/js/dictionary.js"></script>

View File

@ -34,6 +34,8 @@
<script src="/mixed/lib/jquery.min.js"></script> <script src="/mixed/lib/jquery.min.js"></script>
<script src="/mixed/lib/wanakana.min.js"></script> <script src="/mixed/lib/wanakana.min.js"></script>
<script src="/mixed/js/extension.js"></script>
<script src="/fg/js/api.js"></script> <script src="/fg/js/api.js"></script>
<script src="/fg/js/util.js"></script> <script src="/fg/js/util.js"></script>
<script src="/fg/js/document.js"></script> <script src="/fg/js/document.js"></script>

View File

@ -23,7 +23,7 @@ class DisplayFloat extends Display {
this.autoPlayAudioTimer = null; this.autoPlayAudioTimer = null;
this.styleNode = 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))); $(window).on('message', utilAsync(this.onMessage.bind(this)));
} }

View File

@ -11,10 +11,14 @@
}, },
"author": "Alex Yatskov", "author": "Alex Yatskov",
"background": {"page": "bg/background.html"}, "background": {
"page": "bg/background.html",
"persistent": true
},
"content_scripts": [{ "content_scripts": [{
"matches": ["http://*/*", "https://*/*", "file://*/*"], "matches": ["http://*/*", "https://*/*", "file://*/*"],
"js": [ "js": [
"mixed/js/extension.js",
"fg/js/api.js", "fg/js/api.js",
"fg/js/document.js", "fg/js/document.js",
"fg/js/popup.js", "fg/js/popup.js",

53
ext/mixed/js/extension.js Normal file
View File

@ -0,0 +1,53 @@
/*
* Copyright (C) 2019 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
}