From 2bfc7e39725420a2a1a89d968b0a1f27687f70ea Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 11 Oct 2019 17:56:17 -0400 Subject: Fix order of {screenshot} tag --- ext/bg/js/dictionary.js | 4 ++-- ext/bg/js/settings.js | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 498eafcd..191058c1 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -342,10 +342,10 @@ async function dictFieldFormat(field, definition, mode, options) { 'kunyomi', 'onyomi', 'reading', + 'screenshot', 'sentence', 'tags', - 'url', - 'screenshot' + 'url' ]; for (const marker of markers) { diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index f3b5ff16..84b54ba9 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -607,10 +607,10 @@ async function ankiFieldsPopulate(element, options) { 'glossary', 'glossary-brief', 'reading', + 'screenshot', 'sentence', 'tags', - 'url', - 'screenshot' + 'url' ], 'kanji': [ 'character', @@ -618,6 +618,7 @@ async function ankiFieldsPopulate(element, options) { 'glossary', 'kunyomi', 'onyomi', + 'screenshot', 'sentence', 'tags', 'url' -- cgit v1.2.3 From 6014fe5344cd22ff783af18db2b567e2cd7ef819 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 11 Oct 2019 19:08:54 -0400 Subject: Add support for persistent storage --- ext/bg/css/settings.css | 11 +++++++++++ ext/bg/js/settings.js | 32 ++++++++++++++++++++++++++++++-- ext/bg/settings.html | 13 +++++++++++-- 3 files changed, 52 insertions(+), 4 deletions(-) (limited to 'ext') diff --git a/ext/bg/css/settings.css b/ext/bg/css/settings.css index 6284058a..100478aa 100644 --- a/ext/bg/css/settings.css +++ b/ext/bg/css/settings.css @@ -137,6 +137,17 @@ white-space: pre; } +.btn-inner-middle { + vertical-align: middle; +} +.storage-persist-button-inner { + pointer-events: none; +} +input[type=checkbox]#storage-persist-button-checkbox { + margin: 0 0.375em 0 0; + padding: 0; +} + [data-show-for-browser] { display: none; } diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 84b54ba9..7ad628ba 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -724,14 +724,13 @@ async function storageEstimate() { storageEstimate.mostRecent = null; async function storageInfoInitialize() { + storagePersistInitialize(); const browser = await getBrowser(); const container = document.querySelector('#storage-info'); container.setAttribute('data-browser', browser); await storageShowInfo(); - container.classList.remove('storage-hidden'); - document.querySelector('#storage-refresh').addEventListener('click', () => storageShowInfo(), false); } @@ -770,6 +769,35 @@ function storageSpinnerShow(show) { } } +async function storagePersistInitialize() { + if (!(navigator.storage && navigator.storage.persist)) { + // Not supported + return; + } + + const info = document.querySelector('#storage-persist-info'); + const button = document.querySelector('#storage-persist-button'); + const checkbox = document.querySelector('#storage-persist-button-checkbox'); + + info.classList.remove('storage-hidden'); + button.classList.remove('storage-hidden'); + + let persisted = await navigator.storage.persisted(); + if (persisted) { + checkbox.checked = true; + } + + button.addEventListener('click', async () => { + if (persisted) { + return; + } + if (await navigator.storage.persist()) { + persisted = true; + checkbox.checked = true; + } + }, false); +} + /* * Information diff --git a/ext/bg/settings.html b/ext/bg/settings.html index e4710283..19dee8b3 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -395,12 +395,20 @@ -
+

Storage

+
+

+ Web browsers may sometimes clear stored data if the device is running low on storage space. + This can result in the stored dictionary data being deleted unexpectedly, causing Yomichan to stop working for no apparent reason. + In order to prevent this, persistent storage must be enable by clicking the "Persistent Storage" button below. +

+
+

Yomichan is using approximately of . @@ -425,7 +433,8 @@

- + +
-- cgit v1.2.3 From 70bceb5b567ade151d0299917187f4c075ea55ac Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 11 Oct 2019 19:22:46 -0400 Subject: Improve display of storage stats --- ext/bg/js/settings.js | 24 +++++++++++++++++++----- ext/bg/settings.html | 5 ++++- 2 files changed, 23 insertions(+), 6 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 7ad628ba..bd15f5d0 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -705,13 +705,13 @@ async function getBrowser() { function storageBytesToLabeledString(size) { const base = 1000; - const labels = ['bytes', 'KB', 'MB', 'GB']; + const labels = [' bytes', 'KB', 'MB', 'GB']; let labelIndex = 0; while (size >= base) { size /= base; ++labelIndex; } - const label = size.toFixed(1); + const label = labelIndex === 0 ? `${size}` : size.toFixed(1); return `${label}${labels[labelIndex]}`; } @@ -723,6 +723,13 @@ async function storageEstimate() { } storageEstimate.mostRecent = null; +async function isStoragePeristent() { + try { + return await navigator.storage.persisted(); + } catch (e) { } + return false; +} + async function storageInfoInitialize() { storagePersistInitialize(); const browser = await getBrowser(); @@ -741,8 +748,14 @@ async function storageUpdateStats() { const valid = (estimate !== null); if (valid) { - document.querySelector('#storage-usage').textContent = storageBytesToLabeledString(estimate.usage); - document.querySelector('#storage-quota').textContent = storageBytesToLabeledString(estimate.quota); + // Firefox reports usage as 0 when persistent storage is enabled. + const finite = (estimate.usage > 0 || !(await isStoragePeristent())); + if (finite) { + document.querySelector('#storage-usage').textContent = storageBytesToLabeledString(estimate.usage); + document.querySelector('#storage-quota').textContent = storageBytesToLabeledString(estimate.quota); + } + document.querySelector('#storage-use-finite').classList.toggle('storage-hidden', !finite); + document.querySelector('#storage-use-infinite').classList.toggle('storage-hidden', finite); } storageUpdateStats.isUpdating = false; @@ -782,7 +795,7 @@ async function storagePersistInitialize() { info.classList.remove('storage-hidden'); button.classList.remove('storage-hidden'); - let persisted = await navigator.storage.persisted(); + let persisted = await isStoragePeristent(); if (persisted) { checkbox.checked = true; } @@ -794,6 +807,7 @@ async function storagePersistInitialize() { if (await navigator.storage.persist()) { persisted = true; checkbox.checked = true; + storageShowInfo(); } }, false); } diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 19dee8b3..76955b2c 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -410,9 +410,12 @@
-

+

Yomichan is using approximately of .

+

+ Yomichan is permitted unlimited storage. +

-- cgit v1.2.3 From a6903d68a409544f0f26f4cefb2ad6c40f9994c9 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 11 Oct 2019 23:24:08 -0400 Subject: Revert default audio source behaviour This more closely matches the upgrade process --- ext/bg/js/options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index d0aa6fd3..1021e18d 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -281,7 +281,7 @@ function profileOptionsCreateDefaults() { audio: { enabled: true, - sources: ['jpod101', 'jpod101-alternate', 'jisho', 'custom'], + sources: ['jpod101'], volume: 100, autoPlay: false, customSourceUrl: '' -- cgit v1.2.3 From cc72514ce6260bc489b8dd9b51ea27b9fb6e3ce8 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 11 Oct 2019 22:35:59 -0400 Subject: Frontend updates --- ext/bg/js/search-frontend.js | 3 ++- ext/fg/js/float.js | 9 ++++++--- ext/fg/js/frontend-initialize.js | 20 ++++++++++++++++++++ ext/fg/js/frontend.js | 15 ++++++++++++--- ext/fg/js/popup-nested.js | 3 ++- ext/fg/js/popup.js | 4 +++- ext/manifest.json | 3 ++- 7 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 ext/fg/js/frontend-initialize.js (limited to 'ext') diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-frontend.js index 0c1a61ea..f55f06e6 100644 --- a/ext/bg/js/search-frontend.js +++ b/ext/bg/js/search-frontend.js @@ -29,7 +29,8 @@ async function searchFrontendSetup() { '/fg/js/frontend-api-receiver.js', '/fg/js/popup.js', '/fg/js/popup-proxy-host.js', - '/fg/js/frontend.js' + '/fg/js/frontend.js', + '/fg/js/frontend-initialize.js' ]; for (const src of scriptSrcs) { const script = document.createElement('script'); diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 8fdb6925..533d98e1 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -96,7 +96,7 @@ class DisplayFloat extends Display { } } - initialize(options, popupInfo, url) { + initialize(options, popupInfo, url, childrenSupported) { const css = options.general.customPopupCss; if (css) { this.setStyle(css); @@ -105,7 +105,10 @@ class DisplayFloat extends Display { const {id, depth, parentFrameId} = popupInfo; this.optionsContext.depth = depth; this.optionsContext.url = url; - popupNestedInitialize(id, depth, parentFrameId, url); + + if (childrenSupported) { + popupNestedInitialize(id, depth, parentFrameId, url); + } } setStyle(css) { @@ -138,7 +141,7 @@ DisplayFloat.messageHandlers = { kanjiShow: (self, {definitions, options, context}) => self.kanjiShow(definitions, options, context), clearAutoPlayTimer: (self) => self.clearAutoPlayTimer(), orphaned: (self) => self.onOrphaned(), - initialize: (self, {options, popupInfo, url}) => self.initialize(options, popupInfo, url) + initialize: (self, {options, popupInfo, url, childrenSupported}) => self.initialize(options, popupInfo, url, childrenSupported) }; window.yomichan_display = new DisplayFloat(); diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js new file mode 100644 index 00000000..37a82faa --- /dev/null +++ b/ext/fg/js/frontend-initialize.js @@ -0,0 +1,20 @@ +/* + * 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 . + */ + + +window.yomichan_frontend = Frontend.create(); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 88cb93a9..1c41cad1 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -41,6 +41,9 @@ class Frontend { this.enabled = false; this.eventListeners = []; + + this.isPreparedPromiseResolve = null; + this.isPreparedPromise = new Promise((resolve) => { this.isPreparedPromiseResolve = resolve; }); } static create() { @@ -59,11 +62,16 @@ class Frontend { await this.updateOptions(); chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this)); + this.isPreparedPromiseResolve(); } catch (e) { this.onError(e); } } + isPrepared() { + return this.isPreparedPromise; + } + onMouseOver(e) { if (e.target === this.popup.container && this.popupTimer !== null) { this.popupTimerClear(); @@ -303,6 +311,10 @@ class Frontend { } const textSource = docRangeFromPoint(x, y, this.options); + return await this.searchSource(textSource, cause); + } + + async searchSource(textSource, cause) { let hideResults = textSource === null; let searched = false; let success = false; @@ -560,6 +572,3 @@ Frontend.runtimeMessageHandlers = { self.popup.setVisibleOverride(visible); } }; - - -window.yomichan_frontend = Frontend.create(); diff --git a/ext/fg/js/popup-nested.js b/ext/fg/js/popup-nested.js index b36de2ec..f7309466 100644 --- a/ext/fg/js/popup-nested.js +++ b/ext/fg/js/popup-nested.js @@ -41,7 +41,8 @@ async function popupNestedInitialize(id, depth, parentFrameId, url) { '/fg/js/frontend-api-sender.js', '/fg/js/popup.js', '/fg/js/popup-proxy.js', - '/fg/js/frontend.js' + '/fg/js/frontend.js', + '/fg/js/frontend-initialize.js' ]; for (const src of scriptSrcs) { const script = document.createElement('script'); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 9ca91afa..7f96fe97 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -25,6 +25,7 @@ class Popup { this.frameId = null; this.parent = null; this.child = null; + this.childrenSupported = true; this.container = document.createElement('iframe'); this.container.id = 'yomichan-float'; this.container.addEventListener('mousedown', e => e.stopPropagation()); @@ -70,7 +71,8 @@ class Popup { depth: this.depth, parentFrameId }, - url: this.url + url: this.url, + childrenSupported: this.childrenSupported }); resolve(); }); diff --git a/ext/manifest.json b/ext/manifest.json index 927861bd..25b86023 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -26,7 +26,8 @@ "fg/js/source.js", "fg/js/util.js", "fg/js/popup-proxy-host.js", - "fg/js/frontend.js" + "fg/js/frontend.js", + "fg/js/frontend-initialize.js" ], "css": ["fg/css/client.css"], "all_frames": true -- cgit v1.2.3 From 89a8494208d12b04c75724d2ea885adfa87349ef Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 11 Oct 2019 23:24:27 -0400 Subject: Add function for (de)activating event listeners in Display --- ext/mixed/js/display.js | 79 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 24 deletions(-) (limited to 'ext') diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 22181301..f879f5b3 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -30,14 +30,17 @@ class Display { this.audioFallback = null; this.audioCache = {}; this.optionsContext = {}; + this.eventListeners = []; + this.persistentEventListeners = []; + this.interactive = false; + this.eventListenersActive = false; this.dependencies = {}; this.windowScroll = new WindowScroll(); - document.addEventListener('keydown', this.onKeyDown.bind(this)); - document.addEventListener('wheel', this.onWheel.bind(this), {passive: false}); + this.setInteractive(true); } onError(error) { @@ -172,9 +175,48 @@ class Display { } } + setInteractive(interactive) { + interactive = !!interactive; + if (this.interactive === interactive) { return; } + this.interactive = interactive; + + if (interactive) { + Display.addEventListener(this.persistentEventListeners, document, 'keydown', this.onKeyDown.bind(this), false); + Display.addEventListener(this.persistentEventListeners, document, 'wheel', this.onWheel.bind(this), {passive: false}); + } else { + Display.clearEventListeners(this.persistentEventListeners); + } + this.setEventListenersActive(this.eventListenersActive); + } + + setEventListenersActive(active) { + active = !!active && this.interactive; + if (this.eventListenersActive === active) { return; } + this.eventListenersActive = active; + + if (active) { + this.addEventListeners('.action-add-note', 'click', this.onNoteAdd.bind(this)); + this.addEventListeners('.action-view-note', 'click', this.onNoteView.bind(this)); + this.addEventListeners('.action-play-audio', 'click', this.onAudioPlay.bind(this)); + this.addEventListeners('.kanji-link', 'click', this.onKanjiLookup.bind(this)); + this.addEventListeners('.source-term', 'click', this.onSourceTermView.bind(this)); + if (this.options.scanning.enablePopupSearch) { + this.addEventListeners('.glossary-item', 'click', this.onTermLookup.bind(this)); + } + } else { + Display.clearEventListeners(this.eventListeners); + } + } + + addEventListeners(selector, type, listener, options) { + this.container.querySelectorAll(selector).forEach((node) => { + Display.addEventListener(this.eventListeners, node, type, listener, options); + }); + } + async termsShow(definitions, options, context) { try { - this.clearEventListeners(); + this.setEventListenersActive(false); if (!context || context.focus !== false) { window.focus(); @@ -215,14 +257,7 @@ class Display { this.autoPlayAudio(); } - this.addEventListeners('.action-add-note', 'click', this.onNoteAdd.bind(this)); - this.addEventListeners('.action-view-note', 'click', this.onNoteView.bind(this)); - this.addEventListeners('.action-play-audio', 'click', this.onAudioPlay.bind(this)); - this.addEventListeners('.kanji-link', 'click', this.onKanjiLookup.bind(this)); - this.addEventListeners('.source-term', 'click', this.onSourceTermView.bind(this)); - if (this.options.scanning.enablePopupSearch) { - this.addEventListeners('.glossary-item', 'click', this.onTermLookup.bind(this)); - } + this.setEventListenersActive(true); await this.adderButtonUpdate(['term-kanji', 'term-kana'], sequence); } catch (e) { @@ -232,7 +267,7 @@ class Display { async kanjiShow(definitions, options, context) { try { - this.clearEventListeners(); + this.setEventListenersActive(false); if (!context || context.focus !== false) { window.focus(); @@ -265,9 +300,7 @@ class Display { const {index, scroll} = context || {}; this.entryScrollIntoView(index || 0, scroll); - this.addEventListeners('.action-add-note', 'click', this.onNoteAdd.bind(this)); - this.addEventListeners('.action-view-note', 'click', this.onNoteView.bind(this)); - this.addEventListeners('.source-term', 'click', this.onSourceTermView.bind(this)); + this.setEventListenersActive(true); await this.adderButtonUpdate(['kanji'], sequence); } catch (e) { @@ -544,18 +577,16 @@ class Display { return -1; } - addEventListeners(selector, type, listener, options) { - this.container.querySelectorAll(selector).forEach((node) => { - node.addEventListener(type, listener, options); - this.eventListeners.push([node, type, listener, options]); - }); + static addEventListener(eventListeners, object, type, listener, options) { + object.addEventListener(type, listener, options); + eventListeners.push([object, type, listener, options]); } - clearEventListeners() { - for (const [node, type, listener, options] of this.eventListeners) { - node.removeEventListener(type, listener, options); + static clearEventListeners(eventListeners) { + for (const [object, type, listener, options] of eventListeners) { + object.removeEventListener(type, listener, options); } - this.eventListeners = []; + eventListeners.length = 0; } static getElementTop(element) { -- cgit v1.2.3 From 3e249e19ac5f5650553c6303b87adfdb2a688536 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 14:20:54 -0400 Subject: Update Display initialization process --- ext/bg/js/search.js | 46 ++++++++++++++++++++++++++++++++-------------- ext/fg/js/float.js | 8 +++++++- ext/fg/js/popup.js | 6 +----- ext/mixed/js/display.js | 37 +++++++++++++++++++++++++++++++------ 4 files changed, 71 insertions(+), 26 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index ead9ba6f..7a8fdf5e 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -33,23 +33,37 @@ class DisplaySearch extends Display { this.introAnimationTimer = null; this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); + } - if (this.search !== null) { - this.search.addEventListener('click', (e) => this.onSearch(e), false); - } - if (this.query !== null) { - this.query.addEventListener('input', () => this.onSearchInput(), false); + static create() { + const instance = new DisplaySearch(); + instance.prepare(); + return instance; + } + + async prepare() { + try { + await this.initialize(); - const query = DisplaySearch.getSearchQueryFromLocation(window.location.href); - if (query !== null) { - this.query.value = window.wanakana.toKana(query); - this.onSearchQueryUpdated(query, false); + if (this.search !== null) { + this.search.addEventListener('click', (e) => this.onSearch(e), false); } + if (this.query !== null) { + this.query.addEventListener('input', () => this.onSearchInput(), false); - window.wanakana.bind(this.query); - } + const query = DisplaySearch.getSearchQueryFromLocation(window.location.href); + if (query !== null) { + this.query.value = window.wanakana.toKana(query); + this.onSearchQueryUpdated(query, false); + } - this.updateSearchButton(); + window.wanakana.bind(this.query); + } + + this.updateSearchButton(); + } catch (e) { + this.onError(e); + } } onError(error) { @@ -89,7 +103,7 @@ class DisplaySearch extends Display { this.updateSearchButton(); if (valid) { const {definitions} = await apiTermsFind(query, this.optionsContext); - this.termsShow(definitions, await apiOptionsGet(this.optionsContext)); + this.termsShow(definitions, this.options); } else { this.container.textContent = ''; } @@ -98,6 +112,10 @@ class DisplaySearch extends Display { } } + getOptionsContext() { + return this.optionsContext; + } + setIntroVisible(visible, animate) { if (this.introVisible === visible) { return; @@ -164,4 +182,4 @@ class DisplaySearch extends Display { } } -window.yomichan_search = new DisplaySearch(); +window.yomichan_search = DisplaySearch.create(); diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 533d98e1..d9b483d7 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -84,6 +84,10 @@ class DisplayFloat extends Display { super.onKeyDown(e); } + getOptionsContext() { + return this.optionsContext; + } + autoPlayAudio() { this.clearAutoPlayTimer(); this.autoPlayAudioTimer = window.setTimeout(() => super.autoPlayAudio(), 400); @@ -96,7 +100,9 @@ class DisplayFloat extends Display { } } - initialize(options, popupInfo, url, childrenSupported) { + async initialize(options, popupInfo, url, childrenSupported) { + await super.initialize(options); + const css = options.general.customPopupCss; if (css) { this.setStyle(css); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 7f96fe97..0fd6a9d0 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -61,11 +61,7 @@ class Popup { const parentFrameId = (typeof this.frameId === 'number' ? this.frameId : null); this.container.addEventListener('load', () => { this.invokeApi('initialize', { - options: { - general: { - customPopupCss: options.general.customPopupCss - } - }, + options: options, popupInfo: { id: this.id, depth: this.depth, diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index f879f5b3..0d7be355 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -29,7 +29,6 @@ class Display { this.audioPlaying = null; this.audioFallback = null; this.audioCache = {}; - this.optionsContext = {}; this.eventListeners = []; this.persistentEventListeners = []; @@ -76,7 +75,7 @@ class Display { context.source.source = this.context.source; } - const kanjiDefs = await apiKanjiFind(link.textContent, this.optionsContext); + const kanjiDefs = await apiKanjiFind(link.textContent, this.getOptionsContext()); this.kanjiShow(kanjiDefs, this.options, context); } catch (e) { this.onError(e); @@ -99,7 +98,7 @@ class Display { try { textSource.setEndOffset(this.options.scanning.length); - ({definitions, length} = await apiTermsFind(textSource.text(), this.optionsContext)); + ({definitions, length} = await apiTermsFind(textSource.text(), this.getOptionsContext())); if (definitions.length === 0) { return false; } @@ -175,6 +174,28 @@ class Display { } } + onRuntimeMessage({action, params}, sender, callback) { + const handlers = Display.runtimeMessageHandlers; + if (handlers.hasOwnProperty(action)) { + const handler = handlers[action]; + handler(this, params); + callback(); + } + } + + getOptionsContext() { + throw new Error('Override me'); + } + + async initialize(options=null) { + await this.updateOptions(options); + chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this)); + } + + async updateOptions(options) { + this.options = options ? options : await apiOptionsGet(this.getOptionsContext()); + } + setInteractive(interactive) { interactive = !!interactive; if (this.interactive === interactive) { return; } @@ -314,7 +335,7 @@ class Display { async adderButtonUpdate(modes, sequence) { try { - const states = await apiDefinitionsAddable(this.definitions, modes, this.optionsContext); + const states = await apiDefinitionsAddable(this.definitions, modes, this.getOptionsContext()); if (!states || sequence !== this.sequence) { return; } @@ -416,7 +437,7 @@ class Display { } } - const noteId = await apiDefinitionAdd(definition, mode, context, this.optionsContext); + const noteId = await apiDefinitionAdd(definition, mode, context, this.getOptionsContext()); if (noteId) { const index = this.definitions.indexOf(definition); const adderButton = this.adderButtonFind(index, mode); @@ -446,7 +467,7 @@ class Display { } const sources = this.options.audio.sources; - let {audio, source} = await audioGetFromSources(expression, sources, this.optionsContext, true, this.audioCache); + let {audio, source} = await audioGetFromSources(expression, sources, this.getOptionsContext(), true, this.audioCache); let info; if (audio === null) { if (this.audioFallback === null) { @@ -706,3 +727,7 @@ Display.onKeyDownHandlers = { return false; } }; + +Display.runtimeMessageHandlers = { + optionsUpdate: (self) => self.updateOptions(null) +}; -- cgit v1.2.3 From 194615ef21557236cd2b9ea390b4164a4d5338c5 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 14:34:45 -0400 Subject: Make popups store options --- ext/fg/js/frontend.js | 1 + ext/fg/js/popup-proxy-host.js | 6 ++++++ ext/fg/js/popup-proxy.js | 5 +++++ ext/fg/js/popup.js | 5 +++++ 4 files changed, 17 insertions(+) (limited to 'ext') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 1c41cad1..f67441af 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -287,6 +287,7 @@ class Frontend { async updateOptions() { this.options = await apiOptionsGet(this.getOptionsContext()); this.setEnabled(this.options.general.enable); + await this.popup.setOptions(this.options); } popupTimerSet(callback) { diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index f933639c..5edee8dd 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -38,6 +38,7 @@ class PopupProxyHost { this.apiReceiver = new FrontendApiReceiver(`popup-proxy-host#${frameId}`, { createNestedPopup: ({parentId}) => this.createNestedPopup(parentId), + setOptions: ({id, options}) => this.setOptions(id, options), show: ({id, elementRect, options}) => this.show(id, elementRect, options), showOrphaned: ({id, elementRect, options}) => this.show(id, elementRect, options), hide: ({id, changeFocus}) => this.hide(id, changeFocus), @@ -86,6 +87,11 @@ class PopupProxyHost { return new DOMRect(x, y, jsonRect.width, jsonRect.height); } + async setOptions(id, options) { + const popup = this.getPopup(id); + return await popup.setOptions(options); + } + async show(id, elementRect, options) { const popup = this.getPopup(id); elementRect = this.jsonRectToDOMRect(popup, elementRect); diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index efbd28b2..22b95785 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -46,6 +46,11 @@ class PopupProxy { return id; } + async setOptions(options) { + const id = await this.getPopupId(); + return await this.invokeHostApi('setOptions', {id, options}); + } + async show(elementRect, options) { const id = await this.getPopupId(); elementRect = PopupProxy.DOMRectToJson(elementRect); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 0fd6a9d0..396a5be9 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -37,6 +37,7 @@ class Popup { this.isInjected = false; this.visible = false; this.visibleOverride = null; + this.options = null; this.updateVisibility(); } @@ -78,6 +79,10 @@ class Popup { }); } + async setOptions(options) { + this.options = options; + } + async show(elementRect, writingMode, options) { await this.inject(options); -- cgit v1.2.3 From 8a1637f6b31719969473f1b393d46721a2398f11 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 14:38:16 -0400 Subject: Remove .show popup proxy API since it's not used --- ext/fg/js/popup-proxy-host.js | 9 +-------- ext/fg/js/popup-proxy.js | 6 ------ 2 files changed, 1 insertion(+), 14 deletions(-) (limited to 'ext') diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index 5edee8dd..c3acec7f 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -39,8 +39,7 @@ class PopupProxyHost { this.apiReceiver = new FrontendApiReceiver(`popup-proxy-host#${frameId}`, { createNestedPopup: ({parentId}) => this.createNestedPopup(parentId), setOptions: ({id, options}) => this.setOptions(id, options), - show: ({id, elementRect, options}) => this.show(id, elementRect, options), - showOrphaned: ({id, elementRect, options}) => this.show(id, elementRect, options), + showOrphaned: ({id, elementRect, options}) => this.showOrphaned(id, elementRect, options), hide: ({id, changeFocus}) => this.hide(id, changeFocus), setVisibleOverride: ({id, visible}) => this.setVisibleOverride(id, visible), containsPoint: ({id, x, y}) => this.containsPoint(id, x, y), @@ -92,12 +91,6 @@ class PopupProxyHost { return await popup.setOptions(options); } - async show(id, elementRect, options) { - const popup = this.getPopup(id); - elementRect = this.jsonRectToDOMRect(popup, elementRect); - return await popup.show(elementRect, options); - } - async showOrphaned(id, elementRect, options) { const popup = this.getPopup(id); elementRect = this.jsonRectToDOMRect(popup, elementRect); diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index 22b95785..96fc8890 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -51,12 +51,6 @@ class PopupProxy { return await this.invokeHostApi('setOptions', {id, options}); } - async show(elementRect, options) { - const id = await this.getPopupId(); - elementRect = PopupProxy.DOMRectToJson(elementRect); - return await this.invokeHostApi('show', {id, elementRect, options}); - } - async showOrphaned(elementRect, options) { const id = await this.getPopupId(); elementRect = PopupProxy.DOMRectToJson(elementRect); -- cgit v1.2.3 From a5b208fb895d46793223910451d177dc53d9463a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 14:41:24 -0400 Subject: Check if objects are properly initialized before showing content --- ext/fg/js/popup.js | 7 +++++++ ext/mixed/js/display.js | 8 ++++++++ 2 files changed, 15 insertions(+) (limited to 'ext') diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 396a5be9..a9fde7b6 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -79,6 +79,10 @@ class Popup { }); } + isInitialized() { + return this.options !== null; + } + async setOptions(options) { this.options = options; } @@ -212,6 +216,7 @@ class Popup { } async showOrphaned(elementRect, writingMode, options) { + if (!this.isInitialized()) { return; } await this.show(elementRect, writingMode, options); this.invokeApi('orphaned'); } @@ -275,11 +280,13 @@ class Popup { } async termsShow(elementRect, writingMode, definitions, options, context) { + if (!this.isInitialized()) { return; } await this.show(elementRect, writingMode, options); this.invokeApi('termsShow', {definitions, options, context}); } async kanjiShow(elementRect, writingMode, definitions, options, context) { + if (!this.isInitialized()) { return; } await this.show(elementRect, writingMode, options); this.invokeApi('kanjiShow', {definitions, options, context}); } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 0d7be355..d5d055e0 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -187,6 +187,10 @@ class Display { throw new Error('Override me'); } + isInitialized() { + return this.options !== null; + } + async initialize(options=null) { await this.updateOptions(options); chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this)); @@ -236,6 +240,8 @@ class Display { } async termsShow(definitions, options, context) { + if (!this.isInitialized()) { return; } + try { this.setEventListenersActive(false); @@ -287,6 +293,8 @@ class Display { } async kanjiShow(definitions, options, context) { + if (!this.isInitialized()) { return; } + try { this.setEventListenersActive(false); -- cgit v1.2.3 From 6da76835524fbf6b95902f06822d77c54ccf735b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 14:55:18 -0400 Subject: Don't pass options around for calls to termsShow, kanjiShow, etc. --- ext/bg/js/search.js | 6 +++++- ext/fg/js/float.js | 4 ++-- ext/fg/js/frontend.js | 5 +---- ext/fg/js/popup-proxy-host.js | 18 +++++++++--------- ext/fg/js/popup-proxy.js | 12 ++++++------ ext/fg/js/popup.js | 30 +++++++++++++++--------------- ext/mixed/js/display.js | 18 ++++++++++-------- 7 files changed, 48 insertions(+), 45 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 7a8fdf5e..80519f4a 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -103,7 +103,11 @@ class DisplaySearch extends Display { this.updateSearchButton(); if (valid) { const {definitions} = await apiTermsFind(query, this.optionsContext); - this.termsShow(definitions, this.options); + this.termsShow(definitions, { + focus: false, + sentence: null, + url: window.location.href + }); } else { this.container.textContent = ''; } diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index d9b483d7..4a571466 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -143,8 +143,8 @@ DisplayFloat.onKeyDownHandlers = { }; DisplayFloat.messageHandlers = { - termsShow: (self, {definitions, options, context}) => self.termsShow(definitions, options, context), - kanjiShow: (self, {definitions, options, context}) => self.kanjiShow(definitions, options, context), + termsShow: (self, {definitions, context}) => self.termsShow(definitions, context), + kanjiShow: (self, {definitions, context}) => self.kanjiShow(definitions, context), clearAutoPlayTimer: (self) => self.clearAutoPlayTimer(), orphaned: (self) => self.onOrphaned(), initialize: (self, {options, popupInfo, url, childrenSupported}) => self.initialize(options, popupInfo, url, childrenSupported) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index f67441af..52a23889 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -333,8 +333,7 @@ class Frontend { if (textSource && this.options.scanning.modifier !== 'none') { this.popup.showOrphaned( textSource.getRect(), - textSource.getWritingMode(), - this.options + textSource.getWritingMode() ); } } else { @@ -374,7 +373,6 @@ class Frontend { textSource.getRect(), textSource.getWritingMode(), definitions, - this.options, {sentence, url, focus} ); @@ -405,7 +403,6 @@ class Frontend { textSource.getRect(), textSource.getWritingMode(), definitions, - this.options, {sentence, url, focus} ); diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index c3acec7f..74a5153a 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -39,12 +39,12 @@ class PopupProxyHost { this.apiReceiver = new FrontendApiReceiver(`popup-proxy-host#${frameId}`, { createNestedPopup: ({parentId}) => this.createNestedPopup(parentId), setOptions: ({id, options}) => this.setOptions(id, options), - showOrphaned: ({id, elementRect, options}) => this.showOrphaned(id, elementRect, options), + showOrphaned: ({id, elementRect}) => this.showOrphaned(id, elementRect), hide: ({id, changeFocus}) => this.hide(id, changeFocus), setVisibleOverride: ({id, visible}) => this.setVisibleOverride(id, visible), containsPoint: ({id, x, y}) => this.containsPoint(id, x, y), - termsShow: ({id, elementRect, writingMode, definitions, options, context}) => this.termsShow(id, elementRect, writingMode, definitions, options, context), - kanjiShow: ({id, elementRect, writingMode, definitions, options, context}) => this.kanjiShow(id, elementRect, writingMode, definitions, options, context), + termsShow: ({id, elementRect, writingMode, definitions, context}) => this.termsShow(id, elementRect, writingMode, definitions, context), + kanjiShow: ({id, elementRect, writingMode, definitions, context}) => this.kanjiShow(id, elementRect, writingMode, definitions, context), clearAutoPlayTimer: ({id}) => this.clearAutoPlayTimer(id) }); } @@ -91,10 +91,10 @@ class PopupProxyHost { return await popup.setOptions(options); } - async showOrphaned(id, elementRect, options) { + async showOrphaned(id, elementRect) { const popup = this.getPopup(id); elementRect = this.jsonRectToDOMRect(popup, elementRect); - return await popup.showOrphaned(elementRect, options); + return await popup.showOrphaned(elementRect); } async hide(id, changeFocus) { @@ -112,18 +112,18 @@ class PopupProxyHost { return await popup.containsPoint(x, y); } - async termsShow(id, elementRect, writingMode, definitions, options, context) { + async termsShow(id, elementRect, writingMode, definitions, context) { const popup = this.getPopup(id); elementRect = this.jsonRectToDOMRect(popup, elementRect); if (!PopupProxyHost.popupCanShow(popup)) { return false; } - return await popup.termsShow(elementRect, writingMode, definitions, options, context); + return await popup.termsShow(elementRect, writingMode, definitions, context); } - async kanjiShow(id, elementRect, writingMode, definitions, options, context) { + async kanjiShow(id, elementRect, writingMode, definitions, context) { const popup = this.getPopup(id); elementRect = this.jsonRectToDOMRect(popup, elementRect); if (!PopupProxyHost.popupCanShow(popup)) { return false; } - return await popup.kanjiShow(elementRect, writingMode, definitions, options, context); + return await popup.kanjiShow(elementRect, writingMode, definitions, context); } async clearAutoPlayTimer(id) { diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index 96fc8890..e8d6bc98 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -51,10 +51,10 @@ class PopupProxy { return await this.invokeHostApi('setOptions', {id, options}); } - async showOrphaned(elementRect, options) { + async showOrphaned(elementRect) { const id = await this.getPopupId(); elementRect = PopupProxy.DOMRectToJson(elementRect); - return await this.invokeHostApi('showOrphaned', {id, elementRect, options}); + return await this.invokeHostApi('showOrphaned', {id, elementRect}); } async hide(changeFocus) { @@ -76,16 +76,16 @@ class PopupProxy { return await this.invokeHostApi('containsPoint', {id: this.id, x, y}); } - async termsShow(elementRect, writingMode, definitions, options, context) { + async termsShow(elementRect, writingMode, definitions, context) { const id = await this.getPopupId(); elementRect = PopupProxy.DOMRectToJson(elementRect); - return await this.invokeHostApi('termsShow', {id, elementRect, writingMode, definitions, options, context}); + return await this.invokeHostApi('termsShow', {id, elementRect, writingMode, definitions, context}); } - async kanjiShow(elementRect, writingMode, definitions, options, context) { + async kanjiShow(elementRect, writingMode, definitions, context) { const id = await this.getPopupId(); elementRect = PopupProxy.DOMRectToJson(elementRect); - return await this.invokeHostApi('kanjiShow', {id, elementRect, writingMode, definitions, options, context}); + return await this.invokeHostApi('kanjiShow', {id, elementRect, writingMode, definitions, context}); } async clearAutoPlayTimer() { diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index a9fde7b6..f36bb436 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -41,14 +41,14 @@ class Popup { this.updateVisibility(); } - inject(options) { + inject() { if (this.injectPromise === null) { - this.injectPromise = this.createInjectPromise(options); + this.injectPromise = this.createInjectPromise(); } return this.injectPromise; } - async createInjectPromise(options) { + async createInjectPromise() { try { const {frameId} = await this.frameIdPromise; if (typeof frameId === 'number') { @@ -62,7 +62,7 @@ class Popup { const parentFrameId = (typeof this.frameId === 'number' ? this.frameId : null); this.container.addEventListener('load', () => { this.invokeApi('initialize', { - options: options, + options: this.options, popupInfo: { id: this.id, depth: this.depth, @@ -87,10 +87,10 @@ class Popup { this.options = options; } - async show(elementRect, writingMode, options) { - await this.inject(options); + async show(elementRect, writingMode) { + await this.inject(); - const optionsGeneral = options.general; + const optionsGeneral = this.options.general; const container = this.container; const containerRect = container.getBoundingClientRect(); const getPosition = ( @@ -215,9 +215,9 @@ class Popup { return [position, size, after]; } - async showOrphaned(elementRect, writingMode, options) { + async showOrphaned(elementRect, writingMode) { if (!this.isInitialized()) { return; } - await this.show(elementRect, writingMode, options); + await this.show(elementRect, writingMode); this.invokeApi('orphaned'); } @@ -279,16 +279,16 @@ class Popup { return false; } - async termsShow(elementRect, writingMode, definitions, options, context) { + async termsShow(elementRect, writingMode, definitions, context) { if (!this.isInitialized()) { return; } - await this.show(elementRect, writingMode, options); - this.invokeApi('termsShow', {definitions, options, context}); + await this.show(elementRect, writingMode); + this.invokeApi('termsShow', {definitions, context}); } - async kanjiShow(elementRect, writingMode, definitions, options, context) { + async kanjiShow(elementRect, writingMode, definitions, context) { if (!this.isInitialized()) { return; } - await this.show(elementRect, writingMode, options); - this.invokeApi('kanjiShow', {definitions, options, context}); + await this.show(elementRect, writingMode); + this.invokeApi('kanjiShow', {definitions, context}); } clearAutoPlayTimer() { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index d5d055e0..b3ddae72 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -76,7 +76,7 @@ class Display { } const kanjiDefs = await apiKanjiFind(link.textContent, this.getOptionsContext()); - this.kanjiShow(kanjiDefs, this.options, context); + this.kanjiShow(kanjiDefs, context); } catch (e) { this.onError(e); } @@ -125,7 +125,7 @@ class Display { context.source.source = this.context.source; } - this.termsShow(definitions, this.options, context); + this.termsShow(definitions, context); } catch (e) { this.onError(e); } @@ -239,10 +239,12 @@ class Display { }); } - async termsShow(definitions, options, context) { + async termsShow(definitions, context) { if (!this.isInitialized()) { return; } try { + const options = this.options; + this.setEventListenersActive(false); if (!context || context.focus !== false) { @@ -250,7 +252,6 @@ class Display { } this.definitions = definitions; - this.options = options; this.context = context; const sequence = ++this.sequence; @@ -280,7 +281,7 @@ class Display { const {index, scroll} = context || {}; this.entryScrollIntoView(index || 0, scroll); - if (this.options.audio.enabled && this.options.audio.autoPlay) { + if (options.audio.enabled && options.audio.autoPlay) { this.autoPlayAudio(); } @@ -292,10 +293,12 @@ class Display { } } - async kanjiShow(definitions, options, context) { + async kanjiShow(definitions, context) { if (!this.isInitialized()) { return; } try { + const options = this.options; + this.setEventListenersActive(false); if (!context || context.focus !== false) { @@ -303,7 +306,6 @@ class Display { } this.definitions = definitions; - this.options = options; this.context = context; const sequence = ++this.sequence; @@ -415,7 +417,7 @@ class Display { source: this.context.source.source }; - this.termsShow(this.context.source.definitions, this.options, context); + this.termsShow(this.context.source.definitions, context); } } -- cgit v1.2.3 From 537d2ef532aa7b7498de13ab039bd23f28d32714 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 15:00:39 -0400 Subject: Remove Display.dependencies --- ext/bg/js/search.js | 2 -- ext/fg/js/float.js | 2 -- ext/mixed/js/display.js | 4 ---- 3 files changed, 8 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 80519f4a..1d780d70 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -31,8 +31,6 @@ class DisplaySearch extends Display { this.intro = document.querySelector('#intro'); this.introVisible = true; this.introAnimationTimer = null; - - this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); } static create() { diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 4a571466..5164cd8f 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -28,8 +28,6 @@ class DisplayFloat extends Display { url: window.location.href }; - this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); - window.addEventListener('message', (e) => this.onMessage(e), false); } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index b3ddae72..cd9f41bd 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -35,8 +35,6 @@ class Display { this.interactive = false; this.eventListenersActive = false; - this.dependencies = {}; - this.windowScroll = new WindowScroll(); this.setInteractive(true); @@ -86,8 +84,6 @@ class Display { try { e.preventDefault(); - const {docRangeFromPoint, docSentenceExtract} = this.dependencies; - const clickedElement = e.target; const textSource = docRangeFromPoint(e.clientX, e.clientY, this.options); if (textSource === null) { -- cgit v1.2.3 From be7fa57d5ced7f6969c5d66f0a35fafb9de3bcee Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 12:59:51 -0400 Subject: Add support for a popup preview --- ext/bg/css/settings.css | 9 +++ ext/bg/js/settings-popup-preview.js | 147 ++++++++++++++++++++++++++++++++++++ ext/bg/js/settings.js | 38 ++++++++++ ext/bg/settings-popup-preview.html | 125 ++++++++++++++++++++++++++++++ ext/bg/settings.html | 13 ++++ ext/fg/js/frontend.js | 8 +- 6 files changed, 337 insertions(+), 3 deletions(-) create mode 100644 ext/bg/js/settings-popup-preview.js create mode 100644 ext/bg/settings-popup-preview.html (limited to 'ext') diff --git a/ext/bg/css/settings.css b/ext/bg/css/settings.css index 100478aa..09d60b26 100644 --- a/ext/bg/css/settings.css +++ b/ext/bg/css/settings.css @@ -148,6 +148,15 @@ input[type=checkbox]#storage-persist-button-checkbox { padding: 0; } +#settings-popup-preview-frame { + background-color: transparent; + border: none; + margin: 0; + padding: 0; + width: 100%; + height: 320px; +} + [data-show-for-browser] { display: none; } diff --git a/ext/bg/js/settings-popup-preview.js b/ext/bg/js/settings-popup-preview.js new file mode 100644 index 00000000..ec21e7e1 --- /dev/null +++ b/ext/bg/js/settings-popup-preview.js @@ -0,0 +1,147 @@ +/* + * 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 . + */ + + +class SettingsPopupPreview { + constructor() { + this.frontend = null; + this.apiOptionsGetOld = apiOptionsGet; + this.popupShown = false; + } + + static create() { + const instance = new SettingsPopupPreview(); + instance.prepare(); + return instance; + } + + async prepare() { + // Setup events + window.addEventListener('resize', (e) => this.onWindowResize(e), false); + window.addEventListener('message', (e) => this.onMessage(e), false); + + const themeDarkCheckbox = document.querySelector('#theme-dark-checkbox'); + if (themeDarkCheckbox !== null) { + themeDarkCheckbox.addEventListener('change', () => this.onThemeDarkCheckboxChanged(themeDarkCheckbox), false); + } + + // Overwrite API functions + window.apiOptionsGet = (...args) => this.apiOptionsGet(...args); + + // Overwrite frontend + this.frontend = Frontend.create(); + window.yomichan_frontend = this.frontend; + + this.frontend.setEnabled = function () {}; + this.frontend.searchClear = function () {}; + + this.frontend.popup.childrenSupported = false; + this.frontend.popup.interactive = false; + + await this.frontend.isPrepared(); + + // Update search + this.updateSearch(); + } + + async apiOptionsGet(...args) { + const options = await this.apiOptionsGetOld(...args); + options.general.enable = true; + options.general.debugInfo = false; + options.general.popupWidth = 400; + options.general.popupHeight = 250; + options.general.popupHorizontalOffset = 0; + options.general.popupVerticalOffset = 10; + options.general.popupHorizontalOffset2 = 10; + options.general.popupVerticalOffset2 = 0; + options.general.popupHorizontalTextPosition = 'below'; + options.general.popupVerticalTextPosition = 'before'; + options.scanning.selectText = false; + return options; + } + + onWindowResize() { + if (this.frontend === null) { return; } + const textSource = this.frontend.textSourceLast; + if (textSource === null) { return; } + + const elementRect = textSource.getRect(); + const writingMode = textSource.getWritingMode(); + const options = this.frontend.options; + this.frontend.popup.show(elementRect, writingMode, options); + } + + onMessage(e) { + const {action, params} = e.data; + const handlers = SettingsPopupPreview.messageHandlers; + if (handlers.hasOwnProperty(action)) { + const handler = handlers[action]; + handler(this, params); + } + } + + onThemeDarkCheckboxChanged(node) { + document.documentElement.classList.toggle('dark', node.checked); + } + + setText(text) { + const exampleText = document.querySelector('#example-text'); + if (exampleText === null) { return; } + + exampleText.textContent = text; + this.updateSearch(); + } + + async updateSearch() { + const exampleText = document.querySelector('#example-text'); + if (exampleText === null) { return; } + + const textNode = exampleText.firstChild; + if (textNode === null) { return; } + + const range = document.createRange(); + range.selectNode(textNode); + const source = new TextSourceRange(range, range.toString(), null); + + this.frontend.textSourceLast = null; + await this.frontend.searchSource(source, 'script'); + await this.frontend.lastShowPromise; + + if (this.frontend.popup.isVisible()) { + this.popupShown = true; + } + + this.setInfoVisible(!this.popupShown); + } + + setInfoVisible(visible) { + const node = document.querySelector('.placeholder-info'); + if (node === null) { return; } + + node.classList.toggle('placeholder-info-visible', visible); + } +} + +SettingsPopupPreview.messageHandlers = { + setText: (self, {text}) => self.setText(text) +}; + +SettingsPopupPreview.instance = SettingsPopupPreview.create(); + + + diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index bd15f5d0..5732b8ae 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -248,6 +248,7 @@ async function onReady() { showExtensionInformation(); formSetupEventListeners(); + appearanceInitialize(); await audioSettingsInitialize(); await profileOptionsSetup(); @@ -259,6 +260,43 @@ async function onReady() { $(document).ready(utilAsync(onReady)); +/* + * Appearance + */ + +function appearanceInitialize() { + let previewVisible = false; + $('#settings-popup-preview-button').on('click', () => { + if (previewVisible) { return; } + showAppearancePreview(); + previewVisible = true; + }); +} + +function showAppearancePreview() { + const container = $('#settings-popup-preview-container'); + const buttonContainer = $('#settings-popup-preview-button-container'); + const settings = $('#settings-popup-preview-settings'); + const text = $('#settings-popup-preview-text'); + + const frame = document.createElement('iframe'); + frame.src = '/bg/settings-popup-preview.html'; + frame.id = 'settings-popup-preview-frame'; + + window.wanakana.bind(text[0]); + + text.on('input', () => { + const action = 'setText'; + const params = {text: text.val()}; + frame.contentWindow.postMessage({action, params}, '*'); + }); + + container.append(frame); + buttonContainer.remove(); + settings.css('display', ''); +} + + /* * Audio */ diff --git a/ext/bg/settings-popup-preview.html b/ext/bg/settings-popup-preview.html new file mode 100644 index 00000000..3d426f7a --- /dev/null +++ b/ext/bg/settings-popup-preview.html @@ -0,0 +1,125 @@ + + + + + + Yomichan Popup Preview + + + + +
+
+
+ 読め +
+ +
+ + + + + + + + + + + + + diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 76955b2c..9dd71490 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -235,6 +235,18 @@
+ + + +
+
+ +
+
+
@@ -603,6 +615,7 @@ + diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 52a23889..3ddeae78 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -44,6 +44,8 @@ class Frontend { this.isPreparedPromiseResolve = null; this.isPreparedPromise = new Promise((resolve) => { this.isPreparedPromiseResolve = resolve; }); + + this.lastShowPromise = Promise.resolve(); } static create() { @@ -331,7 +333,7 @@ class Frontend { } catch (e) { if (window.yomichan_orphaned) { if (textSource && this.options.scanning.modifier !== 'none') { - this.popup.showOrphaned( + this.lastShowPromise = this.popup.showOrphaned( textSource.getRect(), textSource.getWritingMode() ); @@ -369,7 +371,7 @@ class Frontend { const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); const url = window.location.href; - this.popup.termsShow( + this.lastShowPromise = this.popup.termsShow( textSource.getRect(), textSource.getWritingMode(), definitions, @@ -399,7 +401,7 @@ class Frontend { const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); const url = window.location.href; - this.popup.kanjiShow( + this.lastShowPromise = this.popup.kanjiShow( textSource.getRect(), textSource.getWritingMode(), definitions, -- cgit v1.2.3 From 696ea80e0681c9dab71e7b253acf4c87155004ba Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 13:48:23 -0400 Subject: Add option for popup theme --- ext/bg/js/options.js | 1 + ext/bg/js/settings.js | 2 ++ ext/bg/settings.html | 8 ++++++++ 3 files changed, 11 insertions(+) (limited to 'ext') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 1021e18d..088945c0 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -276,6 +276,7 @@ function profileOptionsCreateDefaults() { compactTags: false, compactGlossaries: false, mainDictionary: '', + popupTheme: 'default', customPopupCss: '' }, diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 5732b8ae..f09b35d9 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -39,6 +39,7 @@ async function formRead(options) { options.general.popupVerticalOffset = parseInt($('#popup-vertical-offset').val(), 10); options.general.popupHorizontalOffset2 = parseInt($('#popup-horizontal-offset2').val(), 0); options.general.popupVerticalOffset2 = parseInt($('#popup-vertical-offset2').val(), 10); + options.general.popupTheme = $('#popup-theme').val(); options.general.customPopupCss = $('#custom-popup-css').val(); options.audio.enabled = $('#audio-playback-enabled').prop('checked'); @@ -107,6 +108,7 @@ async function formWrite(options) { $('#popup-vertical-offset').val(options.general.popupVerticalOffset); $('#popup-horizontal-offset2').val(options.general.popupHorizontalOffset2); $('#popup-vertical-offset2').val(options.general.popupVerticalOffset2); + $('#popup-theme').val(options.general.popupTheme); $('#custom-popup-css').val(options.general.customPopupCss); $('#audio-playback-enabled').prop('checked', options.audio.enabled); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 9dd71490..531c0e86 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -231,6 +231,14 @@
+
+ + +
+
-- cgit v1.2.3 From ceaeeb32dd23403c516f5aa5cb5527c40f2f21aa Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 16:27:49 -0400 Subject: Remove bootstrap styles from float.html --- ext/bg/search.html | 2 +- ext/fg/float.html | 10 +--------- ext/mixed/css/display.css | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 11 deletions(-) (limited to 'ext') diff --git a/ext/bg/search.html b/ext/bg/search.html index 3284ed43..e63b4ac1 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -1,5 +1,5 @@ - + diff --git a/ext/fg/float.html b/ext/fg/float.html index fe1aee8f..ac443c01 100644 --- a/ext/fg/float.html +++ b/ext/fg/float.html @@ -1,18 +1,10 @@ - + - - -
diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css index 8a4cf4a7..e88372bb 100644 --- a/ext/mixed/css/display.css +++ b/ext/mixed/css/display.css @@ -30,9 +30,27 @@ * General */ +body { + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + font-size: 14px; + line-height: 1.42857143; + color: #333; + background-color: #fff; + margin: 0; + border: 0; + padding: 0; +} + hr { padding: 0px; margin: 0px; + border: 0; + border-top: 1px solid #eee; +} + +ol, ul { + margin-top: 0; + margin-bottom: 10px; } #spinner { @@ -60,6 +78,12 @@ hr { padding-bottom: 10px; } +html:root.yomichan-float .entry, +html:root.yomichan-float .note { + padding-left: 10px; + padding-right: 10px; +} + .tag-default { background-color: #8a8a91; } @@ -103,6 +127,7 @@ hr { .actions .disabled img { -webkit-filter: grayscale(100%); + filter: grayscale(100%); opacity: 0.25; } @@ -111,7 +136,7 @@ hr { } .actions { - display: inline-block; + display: block; float: right; } @@ -234,3 +259,16 @@ div.glossary-item.compact-glossary { .entry:not(.entry-current) .current { display: none; } + +.label { + display: inline; + padding: 0.2em 0.6em 0.3em; + font-size: 75%; + font-weight: 700; + line-height: 1; + color: #fff; + text-align: center; + white-space: nowrap; + vertical-align: baseline; + border-radius: 0.25em; +} -- cgit v1.2.3 From c90bc75eb89f5a731f6e3366f6388b594a27b2aa Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 17:06:03 -0400 Subject: Create themes --- ext/bg/search.html | 2 ++ ext/fg/css/client.css | 8 ++++- ext/fg/float.html | 2 ++ ext/fg/js/popup.js | 1 + ext/mixed/css/display-dark.css | 51 +++++++++++++++++++++++++++++ ext/mixed/css/display-default.css | 51 +++++++++++++++++++++++++++++ ext/mixed/css/display.css | 68 ++++++--------------------------------- ext/mixed/js/display.js | 11 +++++++ 8 files changed, 134 insertions(+), 60 deletions(-) create mode 100644 ext/mixed/css/display-dark.css create mode 100644 ext/mixed/css/display-default.css (limited to 'ext') diff --git a/ext/bg/search.html b/ext/bg/search.html index e63b4ac1..12b62797 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -7,6 +7,8 @@ + +
diff --git a/ext/fg/css/client.css b/ext/fg/css/client.css index a2b06d0f..84098653 100644 --- a/ext/fg/css/client.css +++ b/ext/fg/css/client.css @@ -21,7 +21,7 @@ iframe#yomichan-float { all: initial; background-color: #fff; border: 1px solid #999; - box-shadow: 0 0 10px rgba(0, 0, 0, .5); + box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); position: fixed; resize: both; visibility: hidden; @@ -29,6 +29,12 @@ iframe#yomichan-float { box-sizing: border-box; } +iframe#yomichan-float[data-yomichan-theme=dark] { + background-color: #1e1e1e; + border: 1px solid #666; + box-shadow: 0 0 10px rgba(255, 255, 255, 0.5); +} + iframe#yomichan-float.yomichan-float-full-width { border-left: none; border-right: none; diff --git a/ext/fg/float.html b/ext/fg/float.html index ac443c01..01bc4250 100644 --- a/ext/fg/float.html +++ b/ext/fg/float.html @@ -5,6 +5,8 @@ + +
diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index f36bb436..3556a52e 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -85,6 +85,7 @@ class Popup { async setOptions(options) { this.options = options; + this.container.dataset.yomichanTheme = options.general.popupTheme; } async show(elementRect, writingMode) { diff --git a/ext/mixed/css/display-dark.css b/ext/mixed/css/display-dark.css new file mode 100644 index 00000000..75e42565 --- /dev/null +++ b/ext/mixed/css/display-dark.css @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2019 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the entrys 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 . + */ + + +body, +html.yomichan-float body { background-color: #1e1e1e; color: #d4d4d4; } + +hr { border-top-color: #2f2f2f; } + +.tag-default { background-color: #69696e; } +.tag-name { background-color: #489148; } +.tag-expression { background-color: #b07f39; } +.tag-popular { background-color: #025caa; } +.tag-frequent { background-color: #4490a7; } +.tag-archaism { background-color: #b04340; } +.tag-dictionary { background-color: #9057ad; } +.tag-frequency { background-color: #489148; } +.tag-partOfSpeech { background-color: #565656; } + +.reasons { color: #888888; } +.glossary li { color: #888888; } +.glossary-item { color: #d4d4d4; } +.label { color: #e1e1e1; } + +.expression .kanji-link { + border-bottom-color: #888888; + color: #CCCCCC; +} + +.expression-popular, .expression-popular .kanji-link { + color: #0275d8; +} + +.expression-rare, .expression-rare .kanji-link { + color: #666666; +} diff --git a/ext/mixed/css/display-default.css b/ext/mixed/css/display-default.css new file mode 100644 index 00000000..ce1008a8 --- /dev/null +++ b/ext/mixed/css/display-default.css @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2019 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the entrys 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 . + */ + + +body, +html.yomichan-float body { background-color: #ffffff; color: #333333; } + +hr { border-top-color: #eeeeee; } + +.tag-default { background-color: #8a8a91; } +.tag-name { background-color: #5cb85c; } +.tag-expression { background-color: #f0ad4e; } +.tag-popular { background-color: #0275d8; } +.tag-frequent { background-color: #5bc0de; } +.tag-archaism { background-color: #d9534f; } +.tag-dictionary { background-color: #aa66cc; } +.tag-frequency { background-color: #5cb85c; } +.tag-partOfSpeech { background-color: #565656; } + +.reasons { color: #777777; } +.glossary li { color: #777777; } +.glossary-item { color: #000000; } +.label { color: #ffffff; } + +.expression .kanji-link { + border-bottom-color: #777777; + color: #333333; +} + +.expression-popular, .expression-popular .kanji-link { + color: #0275d8; +} + +.expression-rare, .expression-rare .kanji-link { + color: #999999; +} diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css index e88372bb..d899e9fc 100644 --- a/ext/mixed/css/display.css +++ b/ext/mixed/css/display.css @@ -30,12 +30,15 @@ * General */ +html.yomichan-float, +html.yomichan-float body { + background-color: transparent; +} + body { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.42857143; - color: #333; - background-color: #fff; margin: 0; border: 0; padding: 0; @@ -45,7 +48,8 @@ hr { padding: 0px; margin: 0px; border: 0; - border-top: 1px solid #eee; + border-top-width: 1px; + border-top-style: solid; } ol, ul { @@ -84,42 +88,6 @@ html:root.yomichan-float .note { padding-right: 10px; } -.tag-default { - background-color: #8a8a91; -} - -.tag-name { - background-color: #5cb85c; -} - -.tag-expression { - background-color: #f0ad4e; -} - -.tag-popular { - background-color: #0275d8; -} - -.tag-frequent { - background-color: #5bc0de; -} - -.tag-archaism { - background-color: #d9534f; -} - -.tag-dictionary { - background-color: #aa66cc; -} - -.tag-frequency { - background-color: #5cb85c; -} - -.tag-partOfSpeech { - background-color: #565656; -} - .actions .disabled { pointer-events: none; cursor: default; @@ -152,19 +120,11 @@ html:root.yomichan-float .note { } .expression .kanji-link { - border-bottom: 1px #777 dashed; - color: #333; + border-bottom-width: 1px; + border-bottom-style: dashed; text-decoration: none; } -.expression-popular, .expression-popular .kanji-link { - color: #0275d8; -} - -.expression-rare, .expression-rare .kanji-link { - color: #999; -} - .expression .peek-wrapper { font-size: 14px; white-space: nowrap; @@ -198,7 +158,6 @@ html:root.yomichan-float .note { } .reasons { - color: #777; display: inline-block; } @@ -224,14 +183,6 @@ html:root.yomichan-float .note { content: " | "; } -.glossary li { - color: #777; -} - -.glossary-item { - color: #000; -} - div.glossary-item.compact-glossary { display: inline; } @@ -266,7 +217,6 @@ div.glossary-item.compact-glossary { font-size: 75%; font-weight: 700; line-height: 1; - color: #fff; text-align: center; white-space: nowrap; vertical-align: baseline; diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index cd9f41bd..2bf917aa 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -194,6 +194,17 @@ class Display { async updateOptions(options) { this.options = options ? options : await apiOptionsGet(this.getOptionsContext()); + this.updateTheme(this.options.general.popupTheme); + } + + updateTheme(themeName) { + document.documentElement.dataset.yomichanTheme = themeName; + + const stylesheets = document.querySelectorAll('link[data-yomichan-theme-name]'); + for (const stylesheet of stylesheets) { + const match = (stylesheet.dataset.yomichanThemeName === themeName); + stylesheet.rel = (match ? 'stylesheet' : 'stylesheet alternate'); + } } setInteractive(interactive) { -- cgit v1.2.3 From 883226b0451350a0c01841e6c9a192bbb76dd8b6 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 17:12:34 -0400 Subject: Update how custom CSS is applied --- ext/bg/js/search.js | 4 ++++ ext/fg/js/float.js | 21 +-------------------- ext/fg/js/popup-proxy-host.js | 6 ++++++ ext/fg/js/popup-proxy.js | 5 +++++ ext/fg/js/popup.js | 4 ++++ ext/mixed/js/display.js | 16 ++++++++++++++++ 6 files changed, 36 insertions(+), 20 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 1d780d70..68afe47e 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -118,6 +118,10 @@ class DisplaySearch extends Display { return this.optionsContext; } + setCustomCss() { + // No custom CSS + } + setIntroVisible(visible, animate) { if (this.introVisible === visible) { return; diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 5164cd8f..4b3cd848 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -21,7 +21,6 @@ class DisplayFloat extends Display { constructor() { super(document.querySelector('#spinner'), document.querySelector('#definitions')); this.autoPlayAudioTimer = null; - this.styleNode = null; this.optionsContext = { depth: 0, @@ -101,11 +100,6 @@ class DisplayFloat extends Display { async initialize(options, popupInfo, url, childrenSupported) { await super.initialize(options); - const css = options.general.customPopupCss; - if (css) { - this.setStyle(css); - } - const {id, depth, parentFrameId} = popupInfo; this.optionsContext.depth = depth; this.optionsContext.url = url; @@ -114,20 +108,6 @@ class DisplayFloat extends Display { popupNestedInitialize(id, depth, parentFrameId, url); } } - - setStyle(css) { - const parent = document.head; - - if (this.styleNode === null) { - this.styleNode = document.createElement('style'); - } - - this.styleNode.textContent = css; - - if (this.styleNode.parentNode !== parent) { - parent.appendChild(this.styleNode); - } - } } DisplayFloat.onKeyDownHandlers = { @@ -145,6 +125,7 @@ DisplayFloat.messageHandlers = { kanjiShow: (self, {definitions, context}) => self.kanjiShow(definitions, context), clearAutoPlayTimer: (self) => self.clearAutoPlayTimer(), orphaned: (self) => self.onOrphaned(), + setCustomCss: (self, {css}) => self.setCustomCss(css), initialize: (self, {options, popupInfo, url, childrenSupported}) => self.initialize(options, popupInfo, url, childrenSupported) }; diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index 74a5153a..bb323f64 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -45,6 +45,7 @@ class PopupProxyHost { containsPoint: ({id, x, y}) => this.containsPoint(id, x, y), termsShow: ({id, elementRect, writingMode, definitions, context}) => this.termsShow(id, elementRect, writingMode, definitions, context), kanjiShow: ({id, elementRect, writingMode, definitions, context}) => this.kanjiShow(id, elementRect, writingMode, definitions, context), + setCustomCss: ({id, css}) => this.setCustomCss(id, css), clearAutoPlayTimer: ({id}) => this.clearAutoPlayTimer(id) }); } @@ -126,6 +127,11 @@ class PopupProxyHost { return await popup.kanjiShow(elementRect, writingMode, definitions, context); } + async setCustomCss(id, css) { + const popup = this.getPopup(id); + return popup.setCustomCss(css); + } + async clearAutoPlayTimer(id) { const popup = this.getPopup(id); return popup.clearAutoPlayTimer(); diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index e8d6bc98..6ea94b6a 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -88,6 +88,11 @@ class PopupProxy { return await this.invokeHostApi('kanjiShow', {id, elementRect, writingMode, definitions, context}); } + async setCustomCss(css) { + const id = await this.getPopupId(); + return await this.invokeHostApi('setCustomCss', {id, css}); + } + async clearAutoPlayTimer() { if (this.id === null) { return; diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 3556a52e..5ca8643f 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -292,6 +292,10 @@ class Popup { this.invokeApi('kanjiShow', {definitions, context}); } + async setCustomCss(css) { + this.invokeApi('setCustomCss', {css}); + } + clearAutoPlayTimer() { if (this.isInjected) { this.invokeApi('clearAutoPlayTimer'); diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 2bf917aa..51a3dc22 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -29,6 +29,7 @@ class Display { this.audioPlaying = null; this.audioFallback = null; this.audioCache = {}; + this.styleNode = null; this.eventListeners = []; this.persistentEventListeners = []; @@ -195,6 +196,7 @@ class Display { async updateOptions(options) { this.options = options ? options : await apiOptionsGet(this.getOptionsContext()); this.updateTheme(this.options.general.popupTheme); + this.setCustomCss(this.options.general.customPopupCss); } updateTheme(themeName) { @@ -207,6 +209,20 @@ class Display { } } + setCustomCss(css) { + if (this.styleNode === null) { + if (css.length === 0) { return; } + this.styleNode = document.createElement('style'); + } + + this.styleNode.textContent = css; + + const parent = document.head; + if (this.styleNode.parentNode !== parent) { + parent.appendChild(this.styleNode); + } + } + setInteractive(interactive) { interactive = !!interactive; if (this.interactive === interactive) { return; } -- cgit v1.2.3 From 1da60aae2dcd08dd139abf14ca145510e136ee53 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 17:21:36 -0400 Subject: Update live preview custom CSS on input event --- ext/bg/js/settings-popup-preview.js | 22 ++++++++++++++-------- ext/bg/js/settings.js | 6 ++++++ 2 files changed, 20 insertions(+), 8 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/settings-popup-preview.js b/ext/bg/js/settings-popup-preview.js index ec21e7e1..6f64c240 100644 --- a/ext/bg/js/settings-popup-preview.js +++ b/ext/bg/js/settings-popup-preview.js @@ -107,6 +107,18 @@ class SettingsPopupPreview { this.updateSearch(); } + setInfoVisible(visible) { + const node = document.querySelector('.placeholder-info'); + if (node === null) { return; } + + node.classList.toggle('placeholder-info-visible', visible); + } + + setCustomCss(css) { + if (this.frontend === null) { return; } + this.frontend.popup.setCustomCss(css); + } + async updateSearch() { const exampleText = document.querySelector('#example-text'); if (exampleText === null) { return; } @@ -128,17 +140,11 @@ class SettingsPopupPreview { this.setInfoVisible(!this.popupShown); } - - setInfoVisible(visible) { - const node = document.querySelector('.placeholder-info'); - if (node === null) { return; } - - node.classList.toggle('placeholder-info-visible', visible); - } } SettingsPopupPreview.messageHandlers = { - setText: (self, {text}) => self.setText(text) + setText: (self, {text}) => self.setText(text), + setCustomCss: (self, {css}) => self.setCustomCss(css) }; SettingsPopupPreview.instance = SettingsPopupPreview.create(); diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index f09b35d9..b98754ab 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -280,6 +280,7 @@ function showAppearancePreview() { const buttonContainer = $('#settings-popup-preview-button-container'); const settings = $('#settings-popup-preview-settings'); const text = $('#settings-popup-preview-text'); + const customCss = $('#custom-popup-css'); const frame = document.createElement('iframe'); frame.src = '/bg/settings-popup-preview.html'; @@ -292,6 +293,11 @@ function showAppearancePreview() { const params = {text: text.val()}; frame.contentWindow.postMessage({action, params}, '*'); }); + customCss.on('input', () => { + const action = 'setCustomCss'; + const params = {css: customCss.val()}; + frame.contentWindow.postMessage({action, params}, '*'); + }); container.append(frame); buttonContainer.remove(); -- cgit v1.2.3 From b086fca69fdbc74a44d31a06203b302493656151 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 17:59:56 -0400 Subject: Add separate theme option for outer popup style --- ext/bg/js/options.js | 1 + ext/bg/js/settings-popup-preview.js | 8 +++++++ ext/bg/js/settings.js | 2 ++ ext/bg/settings.html | 22 ++++++++++++++----- ext/fg/js/popup.js | 43 ++++++++++++++++++++++++++++++++++++- 5 files changed, 70 insertions(+), 6 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 088945c0..cadc4443 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -277,6 +277,7 @@ function profileOptionsCreateDefaults() { compactGlossaries: false, mainDictionary: '', popupTheme: 'default', + popupOuterTheme: 'default', customPopupCss: '' }, diff --git a/ext/bg/js/settings-popup-preview.js b/ext/bg/js/settings-popup-preview.js index 6f64c240..53a5f1d0 100644 --- a/ext/bg/js/settings-popup-preview.js +++ b/ext/bg/js/settings-popup-preview.js @@ -22,6 +22,7 @@ class SettingsPopupPreview { this.frontend = null; this.apiOptionsGetOld = apiOptionsGet; this.popupShown = false; + this.themeChangeTimeout = null; } static create() { @@ -97,6 +98,13 @@ class SettingsPopupPreview { onThemeDarkCheckboxChanged(node) { document.documentElement.classList.toggle('dark', node.checked); + if (this.themeChangeTimeout !== null) { + clearTimeout(this.themeChangeTimeout); + } + this.themeChangeTimeout = setTimeout(() => { + this.themeChangeTimeout = null; + this.frontend.popup.updateTheme(); + }, 300); } setText(text) { diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index b98754ab..900b89bb 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -40,6 +40,7 @@ async function formRead(options) { options.general.popupHorizontalOffset2 = parseInt($('#popup-horizontal-offset2').val(), 0); options.general.popupVerticalOffset2 = parseInt($('#popup-vertical-offset2').val(), 10); options.general.popupTheme = $('#popup-theme').val(); + options.general.popupOuterTheme = $('#popup-outer-theme').val(); options.general.customPopupCss = $('#custom-popup-css').val(); options.audio.enabled = $('#audio-playback-enabled').prop('checked'); @@ -109,6 +110,7 @@ async function formWrite(options) { $('#popup-horizontal-offset2').val(options.general.popupHorizontalOffset2); $('#popup-vertical-offset2').val(options.general.popupVerticalOffset2); $('#popup-theme').val(options.general.popupTheme); + $('#popup-outer-theme').val(options.general.popupOuterTheme); $('#custom-popup-css').val(options.general.customPopupCss); $('#audio-playback-enabled').prop('checked', options.audio.enabled); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 531c0e86..08e56a09 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -232,11 +232,23 @@
- - +
+
+ + +
+
+ + +
+
diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 5ca8643f..ef4cdb67 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -85,7 +85,7 @@ class Popup { async setOptions(options) { this.options = options; - this.container.dataset.yomichanTheme = options.general.popupTheme; + this.updateTheme(); } async show(elementRect, writingMode) { @@ -270,6 +270,47 @@ class Popup { } } + updateTheme() { + this.container.dataset.yomichanTheme = this.getTheme(this.options.general.popupOuterTheme); + } + + getTheme(themeName) { + if (themeName === 'auto') { + const color = [255, 255, 255]; + Popup.addColor(color, Popup.getColorInfo(window.getComputedStyle(document.documentElement).backgroundColor)); + Popup.addColor(color, Popup.getColorInfo(window.getComputedStyle(document.body).backgroundColor)); + const dark = (color[0] < 128 && color[1] < 128 && color[2] < 128); + themeName = dark ? 'dark' : 'default'; + } + + return themeName; + } + + static addColor(target, color) { + if (color === null) { return; } + + const a = color[3]; + if (a <= 0.0) { return; } + + const aInv = 1.0 - a; + for (let i = 0; i < 3; ++i) { + target[i] = target[i] * aInv + color[i] * a; + } + } + + static getColorInfo(cssColor) { + const m = /^\s*rgba?\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d\.]+)\s*)?\)\s*$/.exec(cssColor); + if (m === null) { return null; } + + const m4 = m[4]; + return [ + Number.parseInt(m[1], 10), + Number.parseInt(m[2], 10), + Number.parseInt(m[3], 10), + m4 ? Math.max(0.0, Math.min(1.0, Number.parseFloat(m4))) : 1.0 + ]; + } + async containsPoint(x, y) { for (let popup = this; popup !== null && popup.isVisible(); popup = popup.child) { const rect = popup.container.getBoundingClientRect(); -- cgit v1.2.3 From 57db18c31b117591982795c930cc9f07efc28641 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Oct 2019 18:10:01 -0400 Subject: Leave default stylesheet enabled by default --- ext/bg/search.html | 2 +- ext/fg/float.html | 2 +- ext/mixed/css/display-dark.css | 3 +-- ext/mixed/css/display-default.css | 3 +-- ext/mixed/css/display.css | 4 ++-- 5 files changed, 6 insertions(+), 8 deletions(-) (limited to 'ext') diff --git a/ext/bg/search.html b/ext/bg/search.html index 12b62797..6930830a 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -7,7 +7,7 @@ - + diff --git a/ext/fg/float.html b/ext/fg/float.html index 01bc4250..2504f448 100644 --- a/ext/fg/float.html +++ b/ext/fg/float.html @@ -5,7 +5,7 @@ - + diff --git a/ext/mixed/css/display-dark.css b/ext/mixed/css/display-dark.css index 75e42565..34a0ccd1 100644 --- a/ext/mixed/css/display-dark.css +++ b/ext/mixed/css/display-dark.css @@ -17,8 +17,7 @@ */ -body, -html.yomichan-float body { background-color: #1e1e1e; color: #d4d4d4; } +body { background-color: #1e1e1e; color: #d4d4d4; } hr { border-top-color: #2f2f2f; } diff --git a/ext/mixed/css/display-default.css b/ext/mixed/css/display-default.css index ce1008a8..176c5387 100644 --- a/ext/mixed/css/display-default.css +++ b/ext/mixed/css/display-default.css @@ -17,8 +17,7 @@ */ -body, -html.yomichan-float body { background-color: #ffffff; color: #333333; } +body { background-color: #ffffff; color: #333333; } hr { border-top-color: #eeeeee; } diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css index d899e9fc..7ebad090 100644 --- a/ext/mixed/css/display.css +++ b/ext/mixed/css/display.css @@ -30,8 +30,8 @@ * General */ -html.yomichan-float, -html.yomichan-float body { +html.yomichan-float:not([data-yomichan-theme]), +html.yomichan-float:not([data-yomichan-theme]) body { background-color: transparent; } -- cgit v1.2.3 From 118f200500b3a27afc873a896b8304387456e2e7 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 13 Oct 2019 10:50:56 -0400 Subject: Simplified how the auto theme works --- ext/fg/css/client.css | 3 ++- ext/fg/js/popup.js | 19 ++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) (limited to 'ext') diff --git a/ext/fg/css/client.css b/ext/fg/css/client.css index 84098653..4b824da3 100644 --- a/ext/fg/css/client.css +++ b/ext/fg/css/client.css @@ -29,7 +29,8 @@ iframe#yomichan-float { box-sizing: border-box; } -iframe#yomichan-float[data-yomichan-theme=dark] { +iframe#yomichan-float[data-yomichan-theme=dark], +iframe#yomichan-float[data-yomichan-theme=auto][data-yomichan-site-color=dark] { background-color: #1e1e1e; border: 1px solid #666; box-shadow: 0 0 10px rgba(255, 255, 255, 0.5); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index ef4cdb67..880e3efe 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -271,19 +271,16 @@ class Popup { } updateTheme() { - this.container.dataset.yomichanTheme = this.getTheme(this.options.general.popupOuterTheme); + this.container.dataset.yomichanTheme = this.options.general.popupOuterTheme; + this.container.dataset.yomichanSiteColor = this.getSiteColor(); } - getTheme(themeName) { - if (themeName === 'auto') { - const color = [255, 255, 255]; - Popup.addColor(color, Popup.getColorInfo(window.getComputedStyle(document.documentElement).backgroundColor)); - Popup.addColor(color, Popup.getColorInfo(window.getComputedStyle(document.body).backgroundColor)); - const dark = (color[0] < 128 && color[1] < 128 && color[2] < 128); - themeName = dark ? 'dark' : 'default'; - } - - return themeName; + getSiteColor() { + const color = [255, 255, 255]; + Popup.addColor(color, Popup.getColorInfo(window.getComputedStyle(document.documentElement).backgroundColor)); + Popup.addColor(color, Popup.getColorInfo(window.getComputedStyle(document.body).backgroundColor)); + const dark = (color[0] < 128 && color[1] < 128 && color[2] < 128); + return dark ? 'dark' : 'light'; } static addColor(target, color) { -- cgit v1.2.3 From 32729482844d7af9e9f307a69c96ea34f1e66011 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 13 Oct 2019 11:05:21 -0400 Subject: Adds setting for controlling the outer style of the popup --- ext/bg/css/settings.css | 3 ++- ext/bg/js/options.js | 3 ++- ext/bg/js/settings.js | 2 ++ ext/bg/settings.html | 12 ++++++++++-- 4 files changed, 16 insertions(+), 4 deletions(-) (limited to 'ext') diff --git a/ext/bg/css/settings.css b/ext/bg/css/settings.css index 09d60b26..21cbe256 100644 --- a/ext/bg/css/settings.css +++ b/ext/bg/css/settings.css @@ -128,7 +128,8 @@ content: counter(audio-source-id); } -#custom-popup-css { +#custom-popup-css, +#custom-popup-outer-css { width: 100%; min-height: 34px; height: 96px; diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index cadc4443..fac17d68 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -278,7 +278,8 @@ function profileOptionsCreateDefaults() { mainDictionary: '', popupTheme: 'default', popupOuterTheme: 'default', - customPopupCss: '' + customPopupCss: '', + customPopupOuterCss: '' }, audio: { diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 900b89bb..7eee7bce 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -42,6 +42,7 @@ async function formRead(options) { options.general.popupTheme = $('#popup-theme').val(); options.general.popupOuterTheme = $('#popup-outer-theme').val(); options.general.customPopupCss = $('#custom-popup-css').val(); + options.general.customPopupOuterCss = $('#custom-popup-outer-css').val(); options.audio.enabled = $('#audio-playback-enabled').prop('checked'); options.audio.autoPlay = $('#auto-play-audio').prop('checked'); @@ -112,6 +113,7 @@ async function formWrite(options) { $('#popup-theme').val(options.general.popupTheme); $('#popup-outer-theme').val(options.general.popupOuterTheme); $('#custom-popup-css').val(options.general.customPopupCss); + $('#custom-popup-outer-css').val(options.general.customPopupOuterCss); $('#audio-playback-enabled').prop('checked', options.audio.enabled); $('#auto-play-audio').prop('checked', options.audio.autoPlay); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 08e56a09..cb223e72 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -252,8 +252,16 @@
- -
+
+
+ +
+
+
+ +
+
+
diff --git a/ext/bg/js/context.js b/ext/bg/js/context.js index a16c8769..8e1dbce6 100644 --- a/ext/bg/js/context.js +++ b/ext/bg/js/context.js @@ -25,9 +25,9 @@ function showExtensionInfo() { node.textContent = `${manifest.name} v${manifest.version}`; } -function setupButtonEvents(selector, command) { - $(selector) - .on('click', (e) => { +function setupButtonEvents(selector, command, url) { + const node = $(selector); + node.on('click', (e) => { if (e.button !== 0) { return; } apiCommandExec(command, {newTab: e.ctrlKey}); e.preventDefault(); @@ -37,6 +37,12 @@ function setupButtonEvents(selector, command) { apiCommandExec(command, {newTab: true}); e.preventDefault(); }); + + if (typeof url === 'string') { + node.attr('href', url); + node.attr('target', '_blank'); + node.attr('rel', 'noopener'); + } } $(document).ready(utilAsync(() => { @@ -47,8 +53,10 @@ $(document).ready(utilAsync(() => { document.documentElement.dataset.mode = (browser === 'firefox-mobile' ? 'full' : 'mini'); }); - setupButtonEvents('.action-open-search', 'search'); - setupButtonEvents('.action-open-options', 'options'); + const manifest = chrome.runtime.getManifest(); + + setupButtonEvents('.action-open-search', 'search', chrome.extension.getURL('/bg/search.html')); + setupButtonEvents('.action-open-options', 'options', chrome.extension.getURL(manifest.options_ui.page)); setupButtonEvents('.action-open-help', 'help'); const optionsContext = { -- cgit v1.2.3 From ce92591b636c8525696b18f5dc844e12865ee6fc Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 19 Oct 2019 22:55:32 -0400 Subject: Fix window focus not always working --- ext/bg/js/api.js | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 9ae7c841..94a53e67 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -367,24 +367,6 @@ async function apiFindTab(timeout, checkUrl) { } async function apiFocusTab(tab) { - if (typeof chrome.windows === 'object' && chrome.windows !== null) { - const tabWindow = await new Promise((resolve) => { - chrome.windows.get(tab.windowId, {}, (tabWindow) => { - const e = chrome.runtime.lastError; - if (e) { reject(e); } - else { resolve(tabWindow); } - }); - }); - if (!tabWindow.focused) { - await new Promise((resolve, reject) => { - chrome.windows.update(tab.windowId, {focused: true}, () => { - const e = chrome.runtime.lastError; - if (e) { reject(e); } - else { resolve(); } - }); - }); - } - } await new Promise((resolve, reject) => { chrome.tabs.update(tab.id, {active: true}, () => { const e = chrome.runtime.lastError; @@ -392,4 +374,26 @@ async function apiFocusTab(tab) { else { resolve(); } }); }); + + if (!(typeof chrome.windows === 'object' && chrome.windows !== null)) { + // Windows not supported (e.g. on Firefox mobile) + return; + } + + const tabWindow = await new Promise((resolve) => { + chrome.windows.get(tab.windowId, {}, (tabWindow) => { + const e = chrome.runtime.lastError; + if (e) { reject(e); } + else { resolve(tabWindow); } + }); + }); + if (!tabWindow.focused) { + await new Promise((resolve, reject) => { + chrome.windows.update(tab.windowId, {focused: true}, () => { + const e = chrome.runtime.lastError; + if (e) { reject(e); } + else { resolve(); } + }); + }); + } } -- cgit v1.2.3 From d7dc8ac9cddc95971f542c8e0b98e16f1f00645e Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 20 Oct 2019 10:30:44 -0400 Subject: Add tooltip about middle click behaviour --- ext/bg/context.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ext') diff --git a/ext/bg/context.html b/ext/bg/context.html index ace40656..48fa463f 100644 --- a/ext/bg/context.html +++ b/ext/bg/context.html @@ -91,8 +91,8 @@
- - + +
-- cgit v1.2.3 From 362a1ed9e4621c47b4dca99777015b90fc90451c Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 20 Oct 2019 10:58:22 -0400 Subject: Catch exception thrown on Edge --- ext/bg/js/api.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 94a53e67..93d9c155 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -380,20 +380,24 @@ async function apiFocusTab(tab) { return; } - const tabWindow = await new Promise((resolve) => { - chrome.windows.get(tab.windowId, {}, (tabWindow) => { - const e = chrome.runtime.lastError; - if (e) { reject(e); } - else { resolve(tabWindow); } - }); - }); - if (!tabWindow.focused) { - await new Promise((resolve, reject) => { - chrome.windows.update(tab.windowId, {focused: true}, () => { + try { + const tabWindow = await new Promise((resolve) => { + chrome.windows.get(tab.windowId, {}, (tabWindow) => { const e = chrome.runtime.lastError; if (e) { reject(e); } - else { resolve(); } + else { resolve(tabWindow); } }); }); + if (!tabWindow.focused) { + await new Promise((resolve, reject) => { + chrome.windows.update(tab.windowId, {focused: true}, () => { + const e = chrome.runtime.lastError; + if (e) { reject(e); } + else { resolve(); } + }); + }); + } + } catch (e) { + // Edge throws exception for no reason here. } } -- cgit v1.2.3 From d32fd1381b6cd5141a21c22f9ef639b2fe9774fb Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 20 Oct 2019 11:22:50 -0700 Subject: increment version for testing --- ext/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext') diff --git a/ext/manifest.json b/ext/manifest.json index fe2b8689..e913c2b0 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Yomichan", - "version": "1.8.8", + "version": "1.8.9", "description": "Japanese dictionary with Anki integration", "icons": {"16": "mixed/img/icon16.png", "48": "mixed/img/icon48.png", "128": "mixed/img/icon128.png"}, -- cgit v1.2.3