From 7a3315d75d91e37f69cf23faa0e5b189ad548e52 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 22 Oct 2019 20:23:03 -0400 Subject: Use chrome.runtime.getURL instead of chrome.extension.getURL --- ext/fg/js/popup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index b5eb9fe2..1f9317e0 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -30,7 +30,7 @@ class Popup { this.container.className = 'yomichan-float'; this.container.addEventListener('mousedown', e => e.stopPropagation()); this.container.addEventListener('scroll', e => e.stopPropagation()); - this.container.setAttribute('src', chrome.extension.getURL('/fg/float.html')); + this.container.setAttribute('src', chrome.runtime.getURL('/fg/float.html')); this.container.style.width = '0px'; this.container.style.height = '0px'; this.injectPromise = null; -- cgit v1.2.3 From 48776145d6bdb8aff82e82546583c790353e75b6 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 15:46:27 +0200 Subject: add workaround to Chrome clipboard.readText For some reason this doesn't work on Firefox, so keep using the new API for Firefox --- ext/bg/background.html | 2 ++ ext/bg/js/api.js | 8 ++++++++ ext/bg/js/backend.js | 5 ++++- ext/bg/js/search.js | 14 +++++++++++++- ext/fg/js/api.js | 4 ++++ 5 files changed, 31 insertions(+), 2 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/bg/background.html b/ext/bg/background.html index 194d4a45..30b3db48 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -5,6 +5,8 @@ +
+ diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 999ea337..88eef431 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -401,3 +401,11 @@ async function apiFocusTab(tab) { // Edge throws exception for no reason here. } } + +async function apiClipboardGet() { + const clipboardPasteTarget = utilBackend().clipboardPasteTarget; + clipboardPasteTarget.innerText = ''; + clipboardPasteTarget.focus(); + document.execCommand('paste'); + return clipboardPasteTarget.innerText; +} diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 23d876f6..7192d026 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -30,6 +30,8 @@ class Backend { this.isPreparedResolve = null; this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve)); + this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target'); + this.apiForwarder = new BackendApiForwarder(); } @@ -187,7 +189,8 @@ Backend.messageHandlers = { forward: ({action, params}, sender) => apiForward(action, params, sender), frameInformationGet: (params, sender) => apiFrameInformationGet(sender), injectStylesheet: ({css}, sender) => apiInjectStylesheet(css, sender), - getEnvironmentInfo: () => apiGetEnvironmentInfo() + getEnvironmentInfo: () => apiGetEnvironmentInfo(), + clipboardGet: () => apiClipboardGet() }; window.yomichan_backend = new Backend(); diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index a09ca822..dca4e8fa 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -17,6 +17,12 @@ */ +let IS_FIREFOX = null; +(async () => { + const {browser} = await apiGetEnvironmentInfo(); + IS_FIREFOX = ['firefox', 'firefox-mobile'].includes(browser); +})(); + class DisplaySearch extends Display { constructor() { super(document.querySelector('#spinner'), document.querySelector('#content')); @@ -235,7 +241,13 @@ class DisplaySearch extends Display { startClipboardMonitor() { this.clipboardMonitorIntervalId = setInterval(async () => { - const curText = (await navigator.clipboard.readText()).trim(); + let curText = null; + // TODO get rid of this and figure out why apiClipboardGet doesn't work on Firefox + if (IS_FIREFOX) { + curText = (await navigator.clipboard.readText()).trim(); + } else if (IS_FIREFOX === false) { + curText = (await apiClipboardGet()).trim(); + } if (curText && (curText !== this.clipboardPrevText)) { if (this.isWanakanaEnabled()) { this.query.value = window.wanakana.toKana(curText); diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index b0746b85..bbc9b5fc 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -72,3 +72,7 @@ function apiInjectStylesheet(css) { function apiGetEnvironmentInfo() { return utilInvoke('getEnvironmentInfo'); } + +function apiClipboardGet() { + return utilInvoke('clipboardGet'); +} -- cgit v1.2.3 From 70418202cf2739258a4e95fdc7f0fbe4c7c46821 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 20:11:23 +0200 Subject: make search page checkbox options persist --- ext/bg/js/api.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ ext/bg/js/backend.js | 1 + ext/bg/js/options.js | 4 +++- ext/bg/js/search.js | 15 ++++++++++++++- ext/bg/search.html | 2 +- ext/fg/js/api.js | 4 ++++ 6 files changed, 72 insertions(+), 3 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 88eef431..6c109614 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -21,6 +21,55 @@ function apiOptionsGet(optionsContext) { return utilBackend().getOptions(optionsContext); } +async function apiOptionsSet(changedOptions, optionsContext, source) { + const backend = utilBackend(); + const {depth} = optionsContext; + let options = await apiOptionsGetFull(); + + function getValuePaths(obj) { + let valuePaths = []; + let nodes = [{ + obj: changedOptions, + path: [] + }]; + while (nodes.length > 0) { + let node = nodes.pop(); + Object.keys(node.obj).forEach((key) => { + let path = node.path.concat(key); + let value = node.obj[key]; + if (typeof value === 'object') { + nodes.unshift({ + obj: value, + path: path + }); + } else { + valuePaths.push([value, path]); + } + }); + } + return valuePaths; + } + + function modifyOption(path, value, options) { + let pivot = options; + for (let pathKey of path.slice(0, -1)) { + if (!(pathKey in pivot)) { + return false; + } + pivot = pivot[pathKey]; + } + pivot[path[path.length - 1]] = value; + return true; + } + + for (let [value, path] of getValuePaths(changedOptions)) { + modifyOption(path, value, options.profiles[depth].options); + } + + await optionsSave(options); + backend.onOptionsUpdated(source); +} + function apiOptionsGetFull() { return utilBackend().getFullOptions(); } diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 7192d026..71393467 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -177,6 +177,7 @@ class Backend { Backend.messageHandlers = { optionsGet: ({optionsContext}) => apiOptionsGet(optionsContext), + optionsSet: ({changedOptions, optionsContext, source}) => apiOptionsSet(changedOptions, optionsContext, source), kanjiFind: ({text, optionsContext}) => apiKanjiFind(text, optionsContext), termsFind: ({text, optionsContext}) => apiTermsFind(text, optionsContext), definitionAdd: ({definition, mode, context, optionsContext}) => apiDefinitionAdd(definition, mode, context, optionsContext), diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 4854cd65..be1ccfbb 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -279,7 +279,9 @@ function profileOptionsCreateDefaults() { popupTheme: 'default', popupOuterTheme: 'default', customPopupCss: '', - customPopupOuterCss: '' + customPopupOuterCss: '', + enableWanakana: true, + enableClipboardMonitor: false }, audio: { diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index dca4e8fa..65cca002 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -62,17 +62,22 @@ class DisplaySearch extends Display { this.query.addEventListener('input', () => this.onSearchInput(), false); if (this.wanakanaEnable !== null) { - if (this.wanakanaEnable.checked) { + if (this.options.general.enableWanakana === true) { + this.wanakanaEnable.checked = true; window.wanakana.bind(this.query); + } else { + this.wanakanaEnable.checked = false; } this.wanakanaEnable.addEventListener('change', (e) => { let query = DisplaySearch.getSearchQueryFromLocation(window.location.href); if (e.target.checked) { window.wanakana.bind(this.query); this.query.value = window.wanakana.toKana(query); + apiOptionsSet({general: {enableWanakana: true}}, this.getOptionsContext()); } else { window.wanakana.unbind(this.query); this.query.value = query; + apiOptionsSet({general: {enableWanakana: false}}, this.getOptionsContext()); } this.onSearchQueryUpdated(this.query.value, false); }); @@ -89,6 +94,12 @@ class DisplaySearch extends Display { } } if (this.clipboardMonitorEnable !== null) { + if (this.options.general.enableClipboardMonitor === true) { + this.clipboardMonitorEnable.checked = true; + this.startClipboardMonitor(); + } else { + this.clipboardMonitorEnable.checked = false; + } this.clipboardMonitorEnable.addEventListener('change', (e) => { if (e.target.checked) { chrome.permissions.request( @@ -96,6 +107,7 @@ class DisplaySearch extends Display { (granted) => { if (granted) { this.startClipboardMonitor(); + apiOptionsSet({general: {enableClipboardMonitor: true}}, this.getOptionsContext()); } else { e.target.checked = false; } @@ -103,6 +115,7 @@ class DisplaySearch extends Display { ); } else { this.stopClipboardMonitor(); + apiOptionsSet({general: {enableClipboardMonitor: false}}, this.getOptionsContext()); } }); } diff --git a/ext/bg/search.html b/ext/bg/search.html index 11dca5a2..8b339cc7 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -22,7 +22,7 @@
- + diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index bbc9b5fc..54818702 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -21,6 +21,10 @@ function apiOptionsGet(optionsContext) { return utilInvoke('optionsGet', {optionsContext}); } +function apiOptionsSet(changedOptions, optionsContext, source) { + return utilInvoke('optionsSet', {changedOptions, optionsContext, source}); +} + function apiTermsFind(text, optionsContext) { return utilInvoke('termsFind', {text, optionsContext}); } -- cgit v1.2.3 From d608657495d59469b17fbae9027772c26848a95e Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 19:38:06 -0400 Subject: Move onError catch into searchAt --- ext/fg/js/frontend.js | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index e854f74e..c8a7d254 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -99,11 +99,7 @@ class Frontend { } const search = async () => { - try { - await this.searchAt(e.clientX, e.clientY, 'mouse'); - } catch (e) { - this.onError(e); - } + await this.searchAt(e.clientX, e.clientY, 'mouse'); }; if (scanningModifier === 'none') { @@ -314,12 +310,16 @@ class Frontend { } async searchAt(x, y, cause) { - if (this.pendingLookup || await this.popup.containsPoint(x, y)) { - return; - } + try { + if (this.pendingLookup || await this.popup.containsPoint(x, y)) { + return; + } - const textSource = docRangeFromPoint(x, y, this.options); - return await this.searchSource(textSource, cause); + const textSource = docRangeFromPoint(x, y, this.options); + return await this.searchSource(textSource, cause); + } catch (e) { + this.onError(e); + } } async searchSource(textSource, cause) { @@ -503,15 +503,7 @@ class Frontend { return; } - const search = async () => { - try { - await this.searchAt(x, y, cause); - } catch (e) { - this.onError(e); - } - }; - - search(); + this.searchAt(x, y, cause); } selectionContainsPoint(selection, x, y) { -- cgit v1.2.3 From 185963899b4176b31a14ab141f1335c17a2de9c4 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 19:38:29 -0400 Subject: Use promiseTimeout --- ext/fg/js/frontend.js | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index c8a7d254..897c7b73 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -20,7 +20,7 @@ class Frontend { constructor(popup, ignoreNodes) { this.popup = popup; - this.popupTimer = null; + this.popupTimerPromise = null; this.textSourceLast = null; this.pendingLookup = false; this.options = null; @@ -74,7 +74,7 @@ class Frontend { } onMouseOver(e) { - if (e.target === this.popup.container && this.popupTimer !== null) { + if (e.target === this.popup.container) { this.popupTimerClear(); } } @@ -99,14 +99,17 @@ class Frontend { } const search = async () => { + if (scanningModifier === 'none') { + if (!await this.popupTimerWait()) { + // Aborted + return; + } + } + await this.searchAt(e.clientX, e.clientY, 'mouse'); }; - if (scanningModifier === 'none') { - this.popupTimerSet(search); - } else { - search(); - } + search(); } onMouseDown(e) { @@ -293,19 +296,19 @@ class Frontend { await this.popup.setOptions(this.options); } - popupTimerSet(callback) { + async popupTimerWait() { const delay = this.options.scanning.delay; - if (delay > 0) { - this.popupTimer = window.setTimeout(callback, delay); - } else { - Promise.resolve().then(callback); + this.popupTimerPromise = promiseTimeout(delay, true); + try { + return await this.popupTimerPromise; + } finally { + this.popupTimerPromise = null; } } popupTimerClear() { - if (this.popupTimer !== null) { - window.clearTimeout(this.popupTimer); - this.popupTimer = null; + if (this.popupTimerPromise !== null) { + this.popupTimerPromise.resolve(false); } } -- cgit v1.2.3 From dcb6f68826bd64d6cb41d2d7a0d5da1a58da9a1b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 20:01:04 -0400 Subject: Don't pass null textSource into searchSource --- ext/bg/js/settings-popup-preview.js | 7 ++++++- ext/fg/js/frontend.js | 17 +++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/bg/js/settings-popup-preview.js b/ext/bg/js/settings-popup-preview.js index b12fb726..ce6da4c0 100644 --- a/ext/bg/js/settings-popup-preview.js +++ b/ext/bg/js/settings-popup-preview.js @@ -158,9 +158,14 @@ class SettingsPopupPreview { const range = document.createRange(); range.selectNode(textNode); const source = new TextSourceRange(range, range.toString(), null); + if (source === null) { return; } this.frontend.textSourceLast = null; - await this.frontend.searchSource(source, 'script'); + try { + await this.frontend.searchSource(source, 'script'); + } finally { + source.cleanup(); + } await this.frontend.lastShowPromise; if (this.frontend.popup.isVisible()) { diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 897c7b73..5e2ef529 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -319,19 +319,27 @@ class Frontend { } const textSource = docRangeFromPoint(x, y, this.options); - return await this.searchSource(textSource, cause); + if (textSource === null) { + return; + } + + try { + return await this.searchSource(textSource, cause); + } finally { + textSource.cleanup(); + } } catch (e) { this.onError(e); } } async searchSource(textSource, cause) { - let hideResults = textSource === null; + let hideResults = false; let searched = false; let success = false; try { - if (!hideResults && (!this.textSourceLast || !this.textSourceLast.equals(textSource))) { + if (!this.textSourceLast || !this.textSourceLast.equals(textSource)) { searched = true; this.pendingLookup = true; const focus = (cause === 'mouse'); @@ -351,9 +359,6 @@ class Frontend { this.onError(e); } } finally { - if (textSource !== null) { - textSource.cleanup(); - } if (hideResults && this.options.scanning.autoHideResults) { this.searchClear(true); } -- cgit v1.2.3 From 1f0a434e965aabad6b10bff4970a8c44a61961be Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 20:02:15 -0400 Subject: Remove unused vars --- ext/fg/js/frontend.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 5e2ef529..957f57c9 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -212,7 +212,7 @@ class Frontend { } } - onAfterSearch(newRange, cause, searched, success) { + onAfterSearch(newRange, cause) { if (cause === 'mouse') { return; } @@ -335,16 +335,12 @@ class Frontend { async searchSource(textSource, cause) { let hideResults = false; - let searched = false; - let success = false; try { if (!this.textSourceLast || !this.textSourceLast.equals(textSource)) { - searched = true; this.pendingLookup = true; const focus = (cause === 'mouse'); hideResults = !await this.searchTerms(textSource, focus) && !await this.searchKanji(textSource, focus); - success = true; } } catch (e) { if (window.yomichan_orphaned) { @@ -364,7 +360,7 @@ class Frontend { } this.pendingLookup = false; - this.onAfterSearch(this.textSourceLast, cause, searched, success); + this.onAfterSearch(this.textSourceLast, cause); } } -- cgit v1.2.3 From f927f806ba602867948654947dce4d27dfc07ebf Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 20:07:17 -0400 Subject: Move check --- ext/bg/js/settings-popup-preview.js | 1 - ext/fg/js/frontend.js | 13 +++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/bg/js/settings-popup-preview.js b/ext/bg/js/settings-popup-preview.js index ce6da4c0..b7b3acc5 100644 --- a/ext/bg/js/settings-popup-preview.js +++ b/ext/bg/js/settings-popup-preview.js @@ -160,7 +160,6 @@ class SettingsPopupPreview { const source = new TextSourceRange(range, range.toString(), null); if (source === null) { return; } - this.frontend.textSourceLast = null; try { await this.frontend.searchSource(source, 'script'); } finally { diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 957f57c9..1ab3c1a1 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -319,7 +319,10 @@ class Frontend { } const textSource = docRangeFromPoint(x, y, this.options); - if (textSource === null) { + if ( + textSource === null || + (this.textSourceLast !== null && this.textSourceLast.equals(textSource)) + ) { return; } @@ -337,11 +340,9 @@ class Frontend { let hideResults = false; try { - if (!this.textSourceLast || !this.textSourceLast.equals(textSource)) { - this.pendingLookup = true; - const focus = (cause === 'mouse'); - hideResults = !await this.searchTerms(textSource, focus) && !await this.searchKanji(textSource, focus); - } + this.pendingLookup = true; + const focus = (cause === 'mouse'); + hideResults = !await this.searchTerms(textSource, focus) && !await this.searchKanji(textSource, focus); } catch (e) { if (window.yomichan_orphaned) { if (textSource && this.options.scanning.modifier !== 'none') { -- cgit v1.2.3 From be27781c150bc081cc65c06b0a08d7b49044cbf3 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 20:21:21 -0400 Subject: Update how definitions are searched for --- ext/fg/js/frontend.js | 72 +++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 43 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 1ab3c1a1..52fdaacf 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -337,12 +337,18 @@ class Frontend { } async searchSource(textSource, cause) { - let hideResults = false; + let results = null; try { this.pendingLookup = true; - const focus = (cause === 'mouse'); - hideResults = !await this.searchTerms(textSource, focus) && !await this.searchKanji(textSource, focus); + results = ( + await this.findTerms(textSource) || + await this.findKanji(textSource) + ); + if (results !== null) { + const focus = (cause === 'mouse'); + this.showContent(textSource, focus, results.definitions, results.type); + } } catch (e) { if (window.yomichan_orphaned) { if (textSource && this.options.scanning.modifier !== 'none') { @@ -356,7 +362,7 @@ class Frontend { this.onError(e); } } finally { - if (hideResults && this.options.scanning.autoHideResults) { + if (results === null && this.options.scanning.autoHideResults) { this.searchClear(true); } @@ -365,27 +371,13 @@ class Frontend { } } - async searchTerms(textSource, focus) { - this.setTextSourceScanLength(textSource, this.options.scanning.length); - - const searchText = textSource.text(); - if (searchText.length === 0) { - return false; - } - - const {definitions, length} = await apiTermsFind(searchText, this.getOptionsContext()); - if (definitions.length === 0) { - return false; - } - - textSource.setEndOffset(length); - + showContent(textSource, focus, definitions, type) { const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); const url = window.location.href; this.lastShowPromise = this.popup.showContent( textSource.getRect(), textSource.getWritingMode(), - 'terms', + type, {definitions, context: {sentence, url, focus}} ); @@ -393,38 +385,32 @@ class Frontend { if (this.options.scanning.selectText) { textSource.select(); } + } + + async findTerms(textSource) { + this.setTextSourceScanLength(textSource, this.options.scanning.length); + + const searchText = textSource.text(); + if (searchText.length === 0) { return null; } + + const {definitions, length} = await apiTermsFind(searchText, this.getOptionsContext()); + if (definitions.length === 0) { return null; } + + textSource.setEndOffset(length); - return true; + return {definitions, type: 'terms'}; } - async searchKanji(textSource, focus) { + async findKanji(textSource) { this.setTextSourceScanLength(textSource, 1); const searchText = textSource.text(); - if (searchText.length === 0) { - return false; - } + if (searchText.length === 0) { return null; } const definitions = await apiKanjiFind(searchText, this.getOptionsContext()); - if (definitions.length === 0) { - return false; - } - - const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); - const url = window.location.href; - this.lastShowPromise = this.popup.showContent( - textSource.getRect(), - textSource.getWritingMode(), - 'kanji', - {definitions, context: {sentence, url, focus}} - ); - - this.textSourceLast = textSource; - if (this.options.scanning.selectText) { - textSource.select(); - } + if (definitions.length === 0) { return null; } - return true; + return {definitions, type: 'kanji'}; } searchClear(changeFocus) { -- cgit v1.2.3 From e91bcf5f553eb4199fff067044413064c46d1844 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 20:45:30 -0400 Subject: Rename textSourceLast to textSourceCurrent --- ext/fg/js/frontend.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 52fdaacf..5ab69b9a 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -21,7 +21,7 @@ class Frontend { constructor(popup, ignoreNodes) { this.popup = popup; this.popupTimerPromise = null; - this.textSourceLast = null; + this.textSourceCurrent = null; this.pendingLookup = false; this.options = null; this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null); @@ -139,8 +139,8 @@ class Frontend { } async onResize() { - if (this.textSourceLast !== null && await this.popup.isVisibleAsync()) { - const textSource = this.textSourceLast; + if (this.textSourceCurrent !== null && await this.popup.isVisibleAsync()) { + const textSource = this.textSourceCurrent; this.lastShowPromise = this.popup.showContent( textSource.getRect(), textSource.getWritingMode() @@ -321,7 +321,7 @@ class Frontend { const textSource = docRangeFromPoint(x, y, this.options); if ( textSource === null || - (this.textSourceLast !== null && this.textSourceLast.equals(textSource)) + (this.textSourceCurrent !== null && this.textSourceCurrent.equals(textSource)) ) { return; } @@ -367,8 +367,10 @@ class Frontend { } this.pendingLookup = false; - this.onAfterSearch(this.textSourceLast, cause); + this.onAfterSearch(this.textSourceCurrent, cause); } + + return results !== null; } showContent(textSource, focus, definitions, type) { @@ -381,7 +383,7 @@ class Frontend { {definitions, context: {sentence, url, focus}} ); - this.textSourceLast = textSource; + this.textSourceCurrent = textSource; if (this.options.scanning.selectText) { textSource.select(); } @@ -417,11 +419,13 @@ class Frontend { this.popup.hide(changeFocus); this.popup.clearAutoPlayTimer(); - if (this.options.scanning.selectText && this.textSourceLast) { - this.textSourceLast.deselect(); - } + if (this.textSourceCurrent !== null) { + if (this.options.scanning.selectText) { + this.textSourceCurrent.deselect(); + } - this.textSourceLast = null; + this.textSourceCurrent = null; + } } getPrimaryTouch(touchList) { @@ -460,7 +464,7 @@ class Frontend { } else { this.primaryTouchIdentifier = touch.identifier; - this.contextMenuPreviousRange = this.textSourceLast ? this.textSourceLast.clone() : null; + this.contextMenuPreviousRange = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; this.contextMenuChecking = true; this.scrollPrevent = false; this.setContextMenuPrevent(false, false); -- cgit v1.2.3 From 8c5240d7a6cc325a628b32cf9e73ac6221517049 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:03:50 -0400 Subject: Simplify how touch events use onAfterSearch --- ext/fg/js/frontend.js | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 5ab69b9a..07c1f0dc 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -32,7 +32,6 @@ class Frontend { }; this.primaryTouchIdentifier = null; - this.contextMenuChecking = false; this.contextMenuPrevent = false; this.contextMenuPreviousRange = null; this.mouseDownPrevent = false; @@ -212,26 +211,6 @@ class Frontend { } } - onAfterSearch(newRange, cause) { - if (cause === 'mouse') { - return; - } - - if ( - !this.contextMenuChecking || - (this.contextMenuPreviousRange === null ? newRange === null : this.contextMenuPreviousRange.equals(newRange))) { - return; - } - - if (cause === 'touchStart' && newRange !== null) { - this.scrollPrevent = true; - } - - this.setContextMenuPrevent(true, false); - this.setMouseDownPrevent(true, false); - this.contextMenuChecking = false; - } - onRuntimeMessage({action, params}, sender, callback) { const handlers = Frontend.runtimeMessageHandlers; if (handlers.hasOwnProperty(action)) { @@ -367,7 +346,6 @@ class Frontend { } this.pendingLookup = false; - this.onAfterSearch(this.textSourceCurrent, cause); } return results !== null; @@ -456,7 +434,6 @@ class Frontend { if (touch === null) { this.primaryTouchIdentifier = null; this.contextMenuPreviousRange = null; - this.contextMenuChecking = false; this.scrollPrevent = false; this.setContextMenuPrevent(false, true); this.setMouseDownPrevent(false, true); @@ -465,13 +442,27 @@ class Frontend { else { this.primaryTouchIdentifier = touch.identifier; this.contextMenuPreviousRange = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; - this.contextMenuChecking = true; this.scrollPrevent = false; this.setContextMenuPrevent(false, false); this.setMouseDownPrevent(false, false); this.setClickPrevent(false); - this.searchFromTouch(touch.clientX, touch.clientY, 'touchStart'); + const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; + + this.searchFromTouch(touch.clientX, touch.clientY, 'touchStart') + .then(() => { + if ( + this.pendingLookup || + this.textSourceCurrent === null || + this.textSourceCurrent.equals(textSourceCurrentPrevious) + ) { + return; + } + + this.scrollPrevent = true; + this.setContextMenuPrevent(true, false); + this.setMouseDownPrevent(true, false); + }); } } @@ -495,10 +486,10 @@ class Frontend { this.popupTimerClear(); if (this.pendingLookup) { - return; + return Promise.resolve(); } - this.searchAt(x, y, cause); + return this.searchAt(x, y, cause); } selectionContainsPoint(selection, x, y) { -- cgit v1.2.3 From f2dec4ca20c1eddeda86ceb5c13795d4bacf19d6 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:05:29 -0400 Subject: Remove searchFromTouch --- ext/fg/js/frontend.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 07c1f0dc..bc4e8cc3 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -197,7 +197,7 @@ class Frontend { } const touch = touches[index]; - this.searchFromTouch(touch.clientX, touch.clientY, 'touchMove'); + this.searchAt(touch.clientX, touch.clientY, 'touchMove'); e.preventDefault(); // Disable scroll } @@ -293,6 +293,8 @@ class Frontend { async searchAt(x, y, cause) { try { + this.popupTimerClear(); + if (this.pendingLookup || await this.popup.containsPoint(x, y)) { return; } @@ -449,7 +451,7 @@ class Frontend { const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; - this.searchFromTouch(touch.clientX, touch.clientY, 'touchStart') + this.searchAt(touch.clientX, touch.clientY, 'touchStart') .then(() => { if ( this.pendingLookup || @@ -482,16 +484,6 @@ class Frontend { this.clickPrevent = value; } - searchFromTouch(x, y, cause) { - this.popupTimerClear(); - - if (this.pendingLookup) { - return Promise.resolve(); - } - - return this.searchAt(x, y, cause); - } - selectionContainsPoint(selection, x, y) { for (let i = 0; i < selection.rangeCount; ++i) { const range = selection.getRangeAt(i); -- cgit v1.2.3 From c74c466c36cc3183e4044e8a7583a0a95609f0e1 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:06:54 -0400 Subject: Remove unused variable --- ext/fg/js/frontend.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index bc4e8cc3..e7f0bbf7 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -33,7 +33,6 @@ class Frontend { this.primaryTouchIdentifier = null; this.contextMenuPrevent = false; - this.contextMenuPreviousRange = null; this.mouseDownPrevent = false; this.clickPrevent = false; this.scrollPrevent = false; @@ -435,7 +434,6 @@ class Frontend { setPrimaryTouch(touch) { if (touch === null) { this.primaryTouchIdentifier = null; - this.contextMenuPreviousRange = null; this.scrollPrevent = false; this.setContextMenuPrevent(false, true); this.setMouseDownPrevent(false, true); @@ -443,7 +441,6 @@ class Frontend { } else { this.primaryTouchIdentifier = touch.identifier; - this.contextMenuPreviousRange = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; this.scrollPrevent = false; this.setContextMenuPrevent(false, false); this.setMouseDownPrevent(false, false); -- cgit v1.2.3 From 9178636613461c1e333abbb9c67e7c82948fdfeb Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:15:02 -0400 Subject: Remove setters --- ext/fg/js/frontend.js | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index e7f0bbf7..6d4ba4f8 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -112,8 +112,8 @@ class Frontend { onMouseDown(e) { if (this.mouseDownPrevent) { - this.setMouseDownPrevent(false, false); - this.setClickPrevent(true); + this.mouseDownPrevent = false; + this.clickPrevent = true; e.preventDefault(); e.stopPropagation(); return false; @@ -148,7 +148,7 @@ class Frontend { onClick(e) { if (this.clickPrevent) { - this.setClickPrevent(false); + this.clickPrevent = false; e.preventDefault(); e.stopPropagation(); return false; @@ -203,7 +203,7 @@ class Frontend { onContextMenu(e) { if (this.contextMenuPrevent) { - this.setContextMenuPrevent(false, false); + this.contextMenuPrevent = false; e.preventDefault(); e.stopPropagation(); return false; @@ -435,16 +435,18 @@ class Frontend { if (touch === null) { this.primaryTouchIdentifier = null; this.scrollPrevent = false; - this.setContextMenuPrevent(false, true); - this.setMouseDownPrevent(false, true); - this.setClickPrevent(false); + this.clickPrevent = false; + // Don't revert context menu and mouse down prevention, + // since these events can occur after the touch has ended. + // this.contextMenuPrevent = false; + // this.mouseDownPrevent = false; } else { this.primaryTouchIdentifier = touch.identifier; this.scrollPrevent = false; - this.setContextMenuPrevent(false, false); - this.setMouseDownPrevent(false, false); - this.setClickPrevent(false); + this.contextMenuPrevent = false; + this.mouseDownPrevent = false; + this.clickPrevent = false; const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; @@ -459,28 +461,12 @@ class Frontend { } this.scrollPrevent = true; - this.setContextMenuPrevent(true, false); - this.setMouseDownPrevent(true, false); + this.contextMenuPrevent = true; + this.mouseDownPrevent = true; }); } } - setContextMenuPrevent(value, delay) { - if (!delay) { - this.contextMenuPrevent = value; - } - } - - setMouseDownPrevent(value, delay) { - if (!delay) { - this.mouseDownPrevent = value; - } - } - - setClickPrevent(value) { - this.clickPrevent = value; - } - selectionContainsPoint(selection, x, y) { for (let i = 0; i < selection.rangeCount; ++i) { const range = selection.getRangeAt(i); -- cgit v1.2.3 From a648e0509120d401611d8f3f1a7f518d200930d2 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:16:23 -0400 Subject: Improve naming --- ext/fg/js/frontend.js | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 6d4ba4f8..0743628c 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -32,10 +32,10 @@ class Frontend { }; this.primaryTouchIdentifier = null; - this.contextMenuPrevent = false; - this.mouseDownPrevent = false; - this.clickPrevent = false; - this.scrollPrevent = false; + this.preventNextContextMenu = false; + this.preventNextMouseDown = false; + this.preventNextClick = false; + this.preventScroll = false; this.enabled = false; this.eventListeners = []; @@ -111,9 +111,9 @@ class Frontend { } onMouseDown(e) { - if (this.mouseDownPrevent) { - this.mouseDownPrevent = false; - this.clickPrevent = true; + if (this.preventNextMouseDown) { + this.preventNextMouseDown = false; + this.preventNextClick = true; e.preventDefault(); e.stopPropagation(); return false; @@ -147,8 +147,8 @@ class Frontend { } onClick(e) { - if (this.clickPrevent) { - this.clickPrevent = false; + if (this.preventNextClick) { + this.preventNextClick = false; e.preventDefault(); e.stopPropagation(); return false; @@ -185,7 +185,7 @@ class Frontend { } onTouchMove(e) { - if (!this.scrollPrevent || !e.cancelable || this.primaryTouchIdentifier === null) { + if (!this.preventScroll || !e.cancelable || this.primaryTouchIdentifier === null) { return; } @@ -202,8 +202,8 @@ class Frontend { } onContextMenu(e) { - if (this.contextMenuPrevent) { - this.contextMenuPrevent = false; + if (this.preventNextContextMenu) { + this.preventNextContextMenu = false; e.preventDefault(); e.stopPropagation(); return false; @@ -434,19 +434,19 @@ class Frontend { setPrimaryTouch(touch) { if (touch === null) { this.primaryTouchIdentifier = null; - this.scrollPrevent = false; - this.clickPrevent = false; + this.preventScroll = false; + this.preventNextClick = false; // Don't revert context menu and mouse down prevention, // since these events can occur after the touch has ended. - // this.contextMenuPrevent = false; - // this.mouseDownPrevent = false; + // this.preventNextContextMenu = false; + // this.preventNextMouseDown = false; } else { this.primaryTouchIdentifier = touch.identifier; - this.scrollPrevent = false; - this.contextMenuPrevent = false; - this.mouseDownPrevent = false; - this.clickPrevent = false; + this.preventScroll = false; + this.preventNextContextMenu = false; + this.preventNextMouseDown = false; + this.preventNextClick = false; const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; @@ -460,9 +460,9 @@ class Frontend { return; } - this.scrollPrevent = true; - this.contextMenuPrevent = true; - this.mouseDownPrevent = true; + this.preventScroll = true; + this.preventNextContextMenu = true; + this.preventNextMouseDown = true; }); } } -- cgit v1.2.3 From 131dc8397dd453839843dbdc493dc721e51496fb Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:27:35 -0400 Subject: Make selectionContainsPoint static --- ext/fg/js/frontend.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 0743628c..8a08f105 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -161,7 +161,7 @@ class Frontend { } let touch = this.getPrimaryTouch(e.changedTouches); - if (this.selectionContainsPoint(window.getSelection(), touch.clientX, touch.clientY)) { + if (Frontend.selectionContainsPoint(window.getSelection(), touch.clientX, touch.clientY)) { touch = null; } @@ -467,7 +467,7 @@ class Frontend { } } - selectionContainsPoint(selection, x, y) { + static selectionContainsPoint(selection, x, y) { for (let i = 0; i < selection.rangeCount; ++i) { const range = selection.getRangeAt(i); for (const rect of range.getClientRects()) { -- cgit v1.2.3 From 80eb3575276e6499824b4ac71782f1a6c87c43ac Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:36:18 -0400 Subject: Simplify touch event implementation --- ext/fg/js/frontend.js | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 8a08f105..223088a1 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -156,28 +156,30 @@ class Frontend { } onTouchStart(e) { - if (this.primaryTouchIdentifier !== null && this.getIndexOfTouch(e.touches, this.primaryTouchIdentifier) >= 0) { + if ( + this.primaryTouchIdentifier !== null || + e.changedTouches.length === 0 + ) { return; } - let touch = this.getPrimaryTouch(e.changedTouches); - if (Frontend.selectionContainsPoint(window.getSelection(), touch.clientX, touch.clientY)) { - touch = null; + const primaryTouch = e.changedTouches[0]; + if (Frontend.selectionContainsPoint(window.getSelection(), primaryTouch.clientX, primaryTouch.clientY)) { + return; } - this.setPrimaryTouch(touch); + this.setPrimaryTouch(primaryTouch); } onTouchEnd(e) { - if (this.primaryTouchIdentifier === null) { - return; - } - - if (this.getIndexOfTouch(e.changedTouches, this.primaryTouchIdentifier) < 0) { + if ( + this.primaryTouchIdentifier === null || + this.getIndexOfTouch(e.changedTouches, this.primaryTouchIdentifier) < 0 + ) { return; } - this.setPrimaryTouch(this.getPrimaryTouch(this.excludeTouches(e.touches, e.changedTouches))); + this.setPrimaryTouch(null); } onTouchCancel(e) { @@ -195,8 +197,8 @@ class Frontend { return; } - const touch = touches[index]; - this.searchAt(touch.clientX, touch.clientY, 'touchMove'); + const primaryTouch = touches[index]; + this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchMove'); e.preventDefault(); // Disable scroll } @@ -407,10 +409,6 @@ class Frontend { } } - getPrimaryTouch(touchList) { - return touchList.length > 0 ? touchList[0] : null; - } - getIndexOfTouch(touchList, identifier) { for (let i in touchList) { let t = touchList[i]; @@ -421,16 +419,6 @@ class Frontend { return -1; } - excludeTouches(touchList, excludeTouchList) { - const result = []; - for (let r of touchList) { - if (this.getIndexOfTouch(excludeTouchList, r.identifier) < 0) { - result.push(r); - } - } - return result; - } - setPrimaryTouch(touch) { if (touch === null) { this.primaryTouchIdentifier = null; -- cgit v1.2.3 From 9703e123bf7467fc990e6d38e59bcc5fd46645b8 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:38:19 -0400 Subject: Remove setPrimaryTouch --- ext/fg/js/frontend.js | 67 ++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 38 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 223088a1..8d51c1df 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -168,7 +168,28 @@ class Frontend { return; } - this.setPrimaryTouch(primaryTouch); + this.primaryTouchIdentifier = primaryTouch.identifier; + this.preventScroll = false; + this.preventNextContextMenu = false; + this.preventNextMouseDown = false; + this.preventNextClick = false; + + const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; + + this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchStart') + .then(() => { + if ( + this.pendingLookup || + this.textSourceCurrent === null || + this.textSourceCurrent.equals(textSourceCurrentPrevious) + ) { + return; + } + + this.preventScroll = true; + this.preventNextContextMenu = true; + this.preventNextMouseDown = true; + }); } onTouchEnd(e) { @@ -179,7 +200,13 @@ class Frontend { return; } - this.setPrimaryTouch(null); + this.primaryTouchIdentifier = null; + this.preventScroll = false; + this.preventNextClick = false; + // Don't revert context menu and mouse down prevention, + // since these events can occur after the touch has ended. + // this.preventNextContextMenu = false; + // this.preventNextMouseDown = false; } onTouchCancel(e) { @@ -419,42 +446,6 @@ class Frontend { return -1; } - setPrimaryTouch(touch) { - if (touch === null) { - this.primaryTouchIdentifier = null; - this.preventScroll = false; - this.preventNextClick = false; - // Don't revert context menu and mouse down prevention, - // since these events can occur after the touch has ended. - // this.preventNextContextMenu = false; - // this.preventNextMouseDown = false; - } - else { - this.primaryTouchIdentifier = touch.identifier; - this.preventScroll = false; - this.preventNextContextMenu = false; - this.preventNextMouseDown = false; - this.preventNextClick = false; - - const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; - - this.searchAt(touch.clientX, touch.clientY, 'touchStart') - .then(() => { - if ( - this.pendingLookup || - this.textSourceCurrent === null || - this.textSourceCurrent.equals(textSourceCurrentPrevious) - ) { - return; - } - - this.preventScroll = true; - this.preventNextContextMenu = true; - this.preventNextMouseDown = true; - }); - } - } - static selectionContainsPoint(selection, x, y) { for (let i = 0; i < selection.rangeCount; ++i) { const range = selection.getRangeAt(i); -- cgit v1.2.3 From e542f381102785aa564f13162e58481d153aa1fc Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:39:23 -0400 Subject: Check pendingLookup earlier --- ext/fg/js/frontend.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 8d51c1df..cfeee64f 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -174,12 +174,13 @@ class Frontend { this.preventNextMouseDown = false; this.preventNextClick = false; + if (this.pendingLookup) { return; } + const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchStart') .then(() => { if ( - this.pendingLookup || this.textSourceCurrent === null || this.textSourceCurrent.equals(textSourceCurrentPrevious) ) { -- cgit v1.2.3 From c365101ec28be0aa2638325ecfda4abe1474eb0f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:41:13 -0400 Subject: Reset preventions earlier --- ext/fg/js/frontend.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index cfeee64f..f788f431 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -163,16 +163,17 @@ class Frontend { return; } + this.preventScroll = false; + this.preventNextContextMenu = false; + this.preventNextMouseDown = false; + this.preventNextClick = false; + const primaryTouch = e.changedTouches[0]; if (Frontend.selectionContainsPoint(window.getSelection(), primaryTouch.clientX, primaryTouch.clientY)) { return; } this.primaryTouchIdentifier = primaryTouch.identifier; - this.preventScroll = false; - this.preventNextContextMenu = false; - this.preventNextMouseDown = false; - this.preventNextClick = false; if (this.pendingLookup) { return; } -- cgit v1.2.3 From d2644c0776c4058ec8b029650ecf911150e35993 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 21:41:58 -0400 Subject: Style update --- ext/fg/js/frontend.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index f788f431..9fdd9671 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -156,10 +156,7 @@ class Frontend { } onTouchStart(e) { - if ( - this.primaryTouchIdentifier !== null || - e.changedTouches.length === 0 - ) { + if (this.primaryTouchIdentifier !== null || e.changedTouches.length === 0) { return; } @@ -175,7 +172,9 @@ class Frontend { this.primaryTouchIdentifier = primaryTouch.identifier; - if (this.pendingLookup) { return; } + if (this.pendingLookup) { + return; + } const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; -- cgit v1.2.3 From 6d85dae68d9820265887405666163eb19219c477 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 25 Oct 2019 20:01:42 -0400 Subject: Fix some issues with the context menu --- ext/fg/js/frontend.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 9fdd9671..a963bd92 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -119,8 +119,10 @@ class Frontend { return false; } - this.popupTimerClear(); - this.searchClear(true); + if (e.button === 0) { + this.popupTimerClear(); + this.searchClear(true); + } } onMouseOut(e) { @@ -231,6 +233,10 @@ class Frontend { e.preventDefault(); // Disable scroll } + onAuxClick(e) { + this.preventNextContextMenu = false; + } + onContextMenu(e) { if (this.preventNextContextMenu) { this.preventNextContextMenu = false; @@ -278,6 +284,7 @@ class Frontend { if (this.options.scanning.touchInputEnabled) { this.addEventListener(window, 'click', this.onClick.bind(this)); + this.addEventListener(window, 'auxclick', this.onAuxClick.bind(this)); this.addEventListener(window, 'touchstart', this.onTouchStart.bind(this)); this.addEventListener(window, 'touchend', this.onTouchEnd.bind(this)); this.addEventListener(window, 'touchcancel', this.onTouchCancel.bind(this)); -- cgit v1.2.3 From bcffe80a1e8342da1bdcca3ee08f0d005a01a2c3 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 25 Oct 2019 20:11:44 -0400 Subject: Group similar functions together --- ext/fg/js/frontend.js | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index a963bd92..94c318d7 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -129,28 +129,22 @@ class Frontend { this.popupTimerClear(); } - onWindowMessage(e) { - const action = e.data; - const handlers = Frontend.windowMessageHandlers; - if (handlers.hasOwnProperty(action)) { - const handler = handlers[action]; - handler(this); + onClick(e) { + if (this.preventNextClick) { + this.preventNextClick = false; + e.preventDefault(); + e.stopPropagation(); + return false; } } - async onResize() { - if (this.textSourceCurrent !== null && await this.popup.isVisibleAsync()) { - const textSource = this.textSourceCurrent; - this.lastShowPromise = this.popup.showContent( - textSource.getRect(), - textSource.getWritingMode() - ); - } + onAuxClick(e) { + this.preventNextContextMenu = false; } - onClick(e) { - if (this.preventNextClick) { - this.preventNextClick = false; + onContextMenu(e) { + if (this.preventNextContextMenu) { + this.preventNextContextMenu = false; e.preventDefault(); e.stopPropagation(); return false; @@ -233,16 +227,22 @@ class Frontend { e.preventDefault(); // Disable scroll } - onAuxClick(e) { - this.preventNextContextMenu = false; + async onResize() { + if (this.textSourceCurrent !== null && await this.popup.isVisibleAsync()) { + const textSource = this.textSourceCurrent; + this.lastShowPromise = this.popup.showContent( + textSource.getRect(), + textSource.getWritingMode() + ); + } } - onContextMenu(e) { - if (this.preventNextContextMenu) { - this.preventNextContextMenu = false; - e.preventDefault(); - e.stopPropagation(); - return false; + onWindowMessage(e) { + const action = e.data; + const handlers = Frontend.windowMessageHandlers; + if (handlers.hasOwnProperty(action)) { + const handler = handlers[action]; + handler(this); } } -- cgit v1.2.3 From 75ff05148d5a07c397d11b8bfbc92833be8726ea Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 3 Nov 2019 11:06:31 -0500 Subject: Fix autoHideResults not working --- ext/bg/js/settings-popup-preview.js | 1 - ext/fg/js/frontend.js | 31 ++++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/bg/js/settings-popup-preview.js b/ext/bg/js/settings-popup-preview.js index b7b3acc5..7d641c46 100644 --- a/ext/bg/js/settings-popup-preview.js +++ b/ext/bg/js/settings-popup-preview.js @@ -158,7 +158,6 @@ class SettingsPopupPreview { const range = document.createRange(); range.selectNode(textNode); const source = new TextSourceRange(range, range.toString(), null); - if (source === null) { return; } try { await this.frontend.searchSource(source, 'script'); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 94c318d7..802221be 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -336,17 +336,16 @@ class Frontend { } const textSource = docRangeFromPoint(x, y, this.options); - if ( - textSource === null || - (this.textSourceCurrent !== null && this.textSourceCurrent.equals(textSource)) - ) { + if (this.textSourceCurrent !== null && this.textSourceCurrent.equals(textSource)) { return; } try { - return await this.searchSource(textSource, cause); + await this.searchSource(textSource, cause); } finally { - textSource.cleanup(); + if (textSource !== null) { + textSource.cleanup(); + } } } catch (e) { this.onError(e); @@ -358,17 +357,19 @@ class Frontend { try { this.pendingLookup = true; - results = ( - await this.findTerms(textSource) || - await this.findKanji(textSource) - ); - if (results !== null) { - const focus = (cause === 'mouse'); - this.showContent(textSource, focus, results.definitions, results.type); + if (textSource !== null) { + results = ( + await this.findTerms(textSource) || + await this.findKanji(textSource) + ); + if (results !== null) { + const focus = (cause === 'mouse'); + this.showContent(textSource, focus, results.definitions, results.type); + } } } catch (e) { if (window.yomichan_orphaned) { - if (textSource && this.options.scanning.modifier !== 'none') { + if (textSource !== null && this.options.scanning.modifier !== 'none') { this.lastShowPromise = this.popup.showContent( textSource.getRect(), textSource.getWritingMode(), @@ -386,7 +387,7 @@ class Frontend { this.pendingLookup = false; } - return results !== null; + return results; } showContent(textSource, focus, definitions, type) { -- cgit v1.2.3 From f4a987912a3bc7012cbfeaaa35ea94e5f97655e0 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 3 Nov 2019 18:56:22 +0200 Subject: prevent accidental lookup on glossary text select --- ext/fg/js/frontend.js | 24 +++++++++++++++++++----- ext/mixed/js/display.js | 21 ++++++++++++++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 802221be..84d6af28 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -80,10 +80,7 @@ class Frontend { onMouseMove(e) { this.popupTimerClear(); - if ( - this.pendingLookup || - (e.buttons & 0x1) !== 0x0 // Left mouse button - ) { + if (this.pendingLookup || Frontend.isMouseButton('primary', e)) { return; } @@ -91,7 +88,7 @@ class Frontend { const scanningModifier = scanningOptions.modifier; if (!( Frontend.isScanningModifierPressed(scanningModifier, e) || - (scanningOptions.middleMouse && (e.buttons & 0x4) !== 0x0) // Middle mouse button + (scanningOptions.middleMouse && Frontend.isMouseButton('auxiliary', e)) )) { return; } @@ -498,6 +495,23 @@ class Frontend { default: return false; } } + + static isMouseButton(button, mouseEvent) { + if (['mouseup', 'mousedown', 'click'].includes(mouseEvent.type)) { + switch (button) { + case 'primary': return mouseEvent.button === 0; + case 'secondary': return mouseEvent.button === 2; + case 'auxiliary': return mouseEvent.button === 1; + default: return false; + } + } + switch (button) { + case 'primary': return (mouseEvent.buttons & 0x1) !== 0x0; + case 'secondary': return (mouseEvent.buttons & 0x2) !== 0x0; + case 'auxiliary': return (mouseEvent.buttons & 0x4) !== 0x0; + default: return false; + } + } } Frontend.windowMessageHandlers = { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index bdc5e962..81072957 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -35,6 +35,7 @@ class Display { this.persistentEventListeners = []; this.interactive = false; this.eventListenersActive = false; + this.clickScanPrevent = false; this.windowScroll = new WindowScroll(); @@ -81,6 +82,22 @@ class Display { } } + onGlossaryMousedown(e) { + if (Frontend.isMouseButton('primary', e)) { + this.clickScanPrevent = false; + } + } + + onGlossaryMouseMove(e) { + this.clickScanPrevent = true; + } + + onGlossaryMouseup(e) { + if (!this.clickScanPrevent && Frontend.isMouseButton('primary', e)) { + this.onTermLookup(e); + } + } + async onTermLookup(e) { try { e.preventDefault(); @@ -252,7 +269,9 @@ class Display { 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.addEventListeners('.glossary-item', 'mouseup', this.onGlossaryMouseup.bind(this)); + this.addEventListeners('.glossary-item', 'mousedown', this.onGlossaryMousedown.bind(this)); + this.addEventListeners('.glossary-item', 'mousemove', this.onGlossaryMouseMove.bind(this)); } } else { Display.clearEventListeners(this.eventListeners); -- cgit v1.2.3 From 83460bcdade28fe77908d3008444a23555f09487 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Mon, 4 Nov 2019 02:15:33 +0200 Subject: refactoring and optimization --- ext/fg/js/frontend.js | 18 ++++++++++-------- ext/mixed/js/display.js | 8 ++++---- 2 files changed, 14 insertions(+), 12 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 84d6af28..b9410f2c 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -497,19 +497,21 @@ class Frontend { } static isMouseButton(button, mouseEvent) { - if (['mouseup', 'mousedown', 'click'].includes(mouseEvent.type)) { - switch (button) { + switch (mouseEvent.type) { + case 'mouseup': + case 'mousedown': + case 'click': switch (button) { case 'primary': return mouseEvent.button === 0; case 'secondary': return mouseEvent.button === 2; case 'auxiliary': return mouseEvent.button === 1; default: return false; } - } - switch (button) { - case 'primary': return (mouseEvent.buttons & 0x1) !== 0x0; - case 'secondary': return (mouseEvent.buttons & 0x2) !== 0x0; - case 'auxiliary': return (mouseEvent.buttons & 0x4) !== 0x0; - default: return false; + default: switch (button) { + case 'primary': return (mouseEvent.buttons & 0x1) !== 0x0; + case 'secondary': return (mouseEvent.buttons & 0x2) !== 0x0; + case 'auxiliary': return (mouseEvent.buttons & 0x4) !== 0x0; + default: return false; + } } } } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 81072957..6d992897 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -82,7 +82,7 @@ class Display { } } - onGlossaryMousedown(e) { + onGlossaryMouseDown(e) { if (Frontend.isMouseButton('primary', e)) { this.clickScanPrevent = false; } @@ -92,7 +92,7 @@ class Display { this.clickScanPrevent = true; } - onGlossaryMouseup(e) { + onGlossaryMouseUp(e) { if (!this.clickScanPrevent && Frontend.isMouseButton('primary', e)) { this.onTermLookup(e); } @@ -269,8 +269,8 @@ class Display { 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', 'mouseup', this.onGlossaryMouseup.bind(this)); - this.addEventListeners('.glossary-item', 'mousedown', this.onGlossaryMousedown.bind(this)); + this.addEventListeners('.glossary-item', 'mouseup', this.onGlossaryMouseUp.bind(this)); + this.addEventListeners('.glossary-item', 'mousedown', this.onGlossaryMouseDown.bind(this)); this.addEventListeners('.glossary-item', 'mousemove', this.onGlossaryMouseMove.bind(this)); } } else { -- cgit v1.2.3 From a85ec440d6c04cc1633db37fdaff00a2d450d51e Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 5 Nov 2019 19:47:02 -0500 Subject: Fix overlapping popup timers --- ext/fg/js/frontend.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index b9410f2c..e67008df 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -310,17 +310,21 @@ class Frontend { async popupTimerWait() { const delay = this.options.scanning.delay; - this.popupTimerPromise = promiseTimeout(delay, true); + const promise = promiseTimeout(delay, true); + this.popupTimerPromise = promise; try { - return await this.popupTimerPromise; + return await promise; } finally { - this.popupTimerPromise = null; + if (this.popupTimerPromise === promise) { + this.popupTimerPromise = null; + } } } popupTimerClear() { if (this.popupTimerPromise !== null) { this.popupTimerPromise.resolve(false); + this.popupTimerPromise = null; } } -- cgit v1.2.3