From 721248c8210c2fb386b765eb518580765091fa67 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 14 Sep 2019 14:11:06 -0400 Subject: [PATCH 1/6] Remove mouseDownLeft and mouseDownMiddle MouseEvent.buttons can be used instead. --- ext/fg/js/frontend.js | 57 +++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 0b60aa2b..d0bbed69 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -21,8 +21,6 @@ class Frontend { constructor(popup, ignoreNodes) { this.popup = popup; this.popupTimer = null; - this.mouseDownLeft = false; - this.mouseDownMiddle = false; this.textSourceLast = null; this.pendingLookup = false; this.options = null; @@ -61,7 +59,6 @@ class Frontend { window.addEventListener('mousemove', this.onMouseMove.bind(this)); window.addEventListener('mouseover', this.onMouseOver.bind(this)); window.addEventListener('mouseout', this.onMouseOut.bind(this)); - window.addEventListener('mouseup', this.onMouseUp.bind(this)); window.addEventListener('resize', this.onResize.bind(this)); if (this.options.scanning.touchInputEnabled) { @@ -88,26 +85,20 @@ class Frontend { onMouseMove(e) { this.popupTimerClear(); - if (!this.options.general.enable) { + if ( + this.pendingLookup || + !this.options.general.enable || + (e.buttons & 0x1) !== 0x0 // Left mouse button + ) { return; } - if (this.mouseDownLeft) { - return; - } - - if (this.pendingLookup) { - return; - } - - const mouseScan = this.mouseDownMiddle && this.options.scanning.middleMouse; - const keyScan = - this.options.scanning.modifier === 'alt' && e.altKey || - this.options.scanning.modifier === 'ctrl' && e.ctrlKey || - this.options.scanning.modifier === 'shift' && e.shiftKey || - this.options.scanning.modifier === 'none'; - - if (!keyScan && !mouseScan) { + const scanningOptions = this.options.scanning; + const scanningModifier = scanningOptions.modifier; + if (!( + Frontend.isScanningModifierPressed(scanningModifier, e) || + (scanningOptions.middleMouse && (e.buttons & 0x4) !== 0x0) // Middle mouse button + )) { return; } @@ -119,7 +110,7 @@ class Frontend { } }; - if (this.options.scanning.modifier === 'none') { + if (scanningModifier === 'none') { this.popupTimerSet(search); } else { search(); @@ -138,20 +129,6 @@ class Frontend { this.mousePosLast = {x: e.clientX, y: e.clientY}; this.popupTimerClear(); this.searchClear(); - - if (e.which === 1) { - this.mouseDownLeft = true; - } else if (e.which === 2) { - this.mouseDownMiddle = true; - } - } - - onMouseUp(e) { - if (e.which === 1) { - this.mouseDownLeft = false; - } else if (e.which === 2) { - this.mouseDownMiddle = false; - } } onMouseOut(e) { @@ -531,6 +508,16 @@ class Frontend { textSource.setEndOffset(length); } } + + static isScanningModifierPressed(scanningModifier, mouseEvent) { + switch (scanningModifier) { + case 'alt': return mouseEvent.altKey; + case 'ctrl': return mouseEvent.ctrlKey; + case 'shift': return mouseEvent.shiftKey; + case 'none': return true; + default: return false; + } + } } window.yomichan_frontend = Frontend.create(); From 89941d404cdae213ad6c609051de3d206f433beb Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 14 Sep 2019 14:11:25 -0400 Subject: [PATCH 2/6] Remove unused variable --- ext/fg/js/frontend.js | 1 - 1 file changed, 1 deletion(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index d0bbed69..9341e492 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -126,7 +126,6 @@ class Frontend { return false; } - this.mousePosLast = {x: e.clientX, y: e.clientY}; this.popupTimerClear(); this.searchClear(); } From 6d3037f3d6548b742fa73aec7504c4384f327674 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 14 Sep 2019 14:27:25 -0400 Subject: [PATCH 3/6] Remove destructuring from searchAt, containsPoint, docRangeFromPoint --- ext/fg/js/document.js | 2 +- ext/fg/js/frontend.js | 10 +++++----- ext/fg/js/popup-proxy-host.js | 6 +++--- ext/fg/js/popup-proxy.js | 4 ++-- ext/fg/js/popup.js | 2 +- ext/mixed/js/display.js | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 60b1b9bd..f2459197 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -89,7 +89,7 @@ function docImposterCreate(element, isTextarea) { return [imposter, container]; } -function docRangeFromPoint({x, y}, options) { +function docRangeFromPoint(x, y, options) { const elements = document.elementsFromPoint(x, y); let imposter = null; let imposterContainer = null; diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 9341e492..a6919b37 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -104,7 +104,7 @@ class Frontend { const search = async () => { try { - await this.searchAt({x: e.clientX, y: e.clientY}, 'mouse'); + await this.searchAt(e.clientX, e.clientY, 'mouse'); } catch (e) { this.onError(e); } @@ -280,12 +280,12 @@ class Frontend { } } - async searchAt(point, type) { - if (this.pendingLookup || await this.popup.containsPoint(point)) { + async searchAt(x, y, type) { + if (this.pendingLookup || await this.popup.containsPoint(x, y)) { return; } - const textSource = docRangeFromPoint(point, this.options); + const textSource = docRangeFromPoint(x, y, this.options); let hideResults = textSource === null; let searched = false; let success = false; @@ -470,7 +470,7 @@ class Frontend { const search = async () => { try { - await this.searchAt({x, y}, type); + await this.searchAt(x, y, type); } catch (e) { this.onError(e); } diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index 041900ed..47f49b8d 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -42,7 +42,7 @@ class PopupProxyHost { showOrphaned: ({id, elementRect, options}) => this.show(id, elementRect, options), hide: ({id}) => this.hide(id), setVisible: ({id, visible}) => this.setVisible(id, visible), - containsPoint: ({id, point}) => this.containsPoint(id, point), + 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), clearAutoPlayTimer: ({id}) => this.clearAutoPlayTimer(id) @@ -108,9 +108,9 @@ class PopupProxyHost { return popup.setVisible(visible); } - async containsPoint(id, point) { + async containsPoint(id, x, y) { const popup = this.getPopup(id); - return await popup.containsPoint(point); + return await popup.containsPoint(x, y); } async termsShow(id, elementRect, writingMode, definitions, options, context) { diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index c3a7bff0..f04e24e0 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -69,11 +69,11 @@ class PopupProxy { return await this.invokeHostApi('setVisible', {id, visible}); } - async containsPoint(point) { + async containsPoint(x, y) { if (this.id === null) { return false; } - return await this.invokeHostApi('containsPoint', {id: this.id, point}); + return await this.invokeHostApi('containsPoint', {id: this.id, x, y}); } async termsShow(elementRect, writingMode, definitions, options, context) { diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 1b15977b..1d6fa5b3 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -251,7 +251,7 @@ class Popup { } } - async containsPoint({x, y}) { + async containsPoint(x, y) { for (let popup = this; popup !== null && popup.isVisible(); popup = popup.child) { const rect = popup.container.getBoundingClientRect(); if (x >= rect.left && y >= rect.top && x < rect.right && y < rect.bottom) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index eca67b5e..ca1738a6 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -81,7 +81,7 @@ class Display { const {docRangeFromPoint, docSentenceExtract} = this.dependencies; const clickedElement = $(e.target); - const textSource = docRangeFromPoint({x: e.clientX, y: e.clientY}, this.options); + const textSource = docRangeFromPoint(e.clientX, e.clientY, this.options); if (textSource === null) { return false; } From 7dddcb8dca226e79453aa7734c99df1b18af4366 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 14 Sep 2019 14:31:21 -0400 Subject: [PATCH 4/6] Rename type to cause --- ext/fg/js/frontend.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index a6919b37..31b43ad3 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -219,8 +219,8 @@ class Frontend { } } - onAfterSearch(newRange, type, searched, success) { - if (type === 'mouse') { + onAfterSearch(newRange, cause, searched, success) { + if (cause === 'mouse') { return; } @@ -230,7 +230,7 @@ class Frontend { return; } - if (type === 'touchStart' && newRange !== null) { + if (cause === 'touchStart' && newRange !== null) { this.scrollPrevent = true; } @@ -280,7 +280,7 @@ class Frontend { } } - async searchAt(x, y, type) { + async searchAt(x, y, cause) { if (this.pendingLookup || await this.popup.containsPoint(x, y)) { return; } @@ -294,7 +294,7 @@ class Frontend { if (!hideResults && (!this.textSourceLast || !this.textSourceLast.equals(textSource))) { searched = true; this.pendingLookup = true; - const focus = (type === 'mouse'); + const focus = (cause === 'mouse'); hideResults = !await this.searchTerms(textSource, focus) && !await this.searchKanji(textSource, focus); success = true; } @@ -319,7 +319,7 @@ class Frontend { } this.pendingLookup = false; - this.onAfterSearch(this.textSourceLast, type, searched, success); + this.onAfterSearch(this.textSourceLast, cause, searched, success); } } @@ -461,7 +461,7 @@ class Frontend { this.clickPrevent = value; } - searchFromTouch(x, y, type) { + searchFromTouch(x, y, cause) { this.popupTimerClear(); if (!this.options.general.enable || this.pendingLookup) { @@ -470,7 +470,7 @@ class Frontend { const search = async () => { try { - await this.searchAt(x, y, type); + await this.searchAt(x, y, cause); } catch (e) { this.onError(e); } From 8b1e4d1c6fc5f496c05bb69fe9e6b2cd12c9090b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 14 Sep 2019 14:52:03 -0400 Subject: [PATCH 5/6] Return only single element when deepDomScan is not enabled --- ext/fg/js/document.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index f2459197..079a5034 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -89,8 +89,18 @@ function docImposterCreate(element, isTextarea) { return [imposter, container]; } +function docElementsFromPoint(x, y, all) { + if (all) { + return document.elementsFromPoint(x, y); + } + + const e = document.elementFromPoint(x, y); + return e !== null ? [e] : []; +} + function docRangeFromPoint(x, y, options) { - const elements = document.elementsFromPoint(x, y); + const deepDomScan = options.scanning.deepDomScan; + const elements = docElementsFromPoint(x, y, deepDomScan); let imposter = null; let imposterContainer = null; if (elements.length > 0) { @@ -108,7 +118,7 @@ function docRangeFromPoint(x, y, options) { } } - const range = caretRangeFromPointExt(x, y, options.scanning.deepDomScan ? elements : []); + const range = caretRangeFromPointExt(x, y, deepDomScan ? elements : []); if (range !== null) { if (imposter !== null) { docSetImposterStyle(imposterContainer.style, 'z-index', '-2147483646'); From 0067dfeacc3ecaf1215f1b9026c500bff31761e6 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 14 Sep 2019 15:29:43 -0400 Subject: [PATCH 6/6] Remove redundant call of popupTimerClear Also use explicit null checks rather than truthy checks. --- ext/fg/js/frontend.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 31b43ad3..c98a9a33 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -77,7 +77,7 @@ class Frontend { } onMouseOver(e) { - if (e.target === this.popup.container && this.popupTimer) { + if (e.target === this.popup.container && this.popupTimer !== null) { this.popupTimerClear(); } } @@ -269,12 +269,11 @@ class Frontend { } popupTimerSet(callback) { - this.popupTimerClear(); this.popupTimer = window.setTimeout(callback, this.options.scanning.delay); } popupTimerClear() { - if (this.popupTimer) { + if (this.popupTimer !== null) { window.clearTimeout(this.popupTimer); this.popupTimer = null; }