From 548607ea7f45c5f9201f093c56429faf2906b5ad Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 17:07:41 -0400 Subject: Destructure point to {x, y} only once --- ext/fg/js/document.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index dc2a9b87..1aa6f83b 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -87,8 +87,8 @@ function docImposterCreate(element, isTextarea) { return [imposter, container]; } -function docRangeFromPoint(point) { - const element = document.elementFromPoint(point.x, point.y); +function docRangeFromPoint({x, y}) { + const element = document.elementFromPoint(x, y); let imposter = null; let imposterContainer = null; if (element) { @@ -105,7 +105,7 @@ function docRangeFromPoint(point) { } } - const range = document.caretRangeFromPoint(point.x, point.y); + const range = document.caretRangeFromPoint(x, y); if (range !== null && isPointInRange(point, range)) { if (imposter !== null) { docSetImposterStyle(imposterContainer.style, 'z-index', '-2147483646'); @@ -191,7 +191,7 @@ function docSentenceExtract(source, extent) { }; } -function isPointInRange(point, range) { +function isPointInRange(x, y, range) { // Scan forward const nodePre = range.endContainer; const offsetPre = range.endOffset; @@ -199,7 +199,7 @@ function isPointInRange(point, range) { const {node, offset} = TextSourceRange.seekForward(range.endContainer, range.endOffset, 1); range.setEnd(node, offset); - if (isPointInAnyRect(point, range.getClientRects())) { + if (isPointInAnyRect(x, y, range.getClientRects())) { return true; } } finally { @@ -210,7 +210,7 @@ function isPointInRange(point, range) { const {node, offset} = TextSourceRange.seekBackward(range.startContainer, range.startOffset, 1); range.setStart(node, offset); - if (isPointInAnyRect(point, range.getClientRects())) { + if (isPointInAnyRect(x, y, range.getClientRects())) { // This purposefully leaves the starting offset as modified and sets teh range length to 0. range.setEnd(node, offset); return true; @@ -220,19 +220,19 @@ function isPointInRange(point, range) { return false; } -function isPointInAnyRect(point, rects) { +function isPointInAnyRect(x, y, rects) { for (const rect of rects) { - if (isPointInRect(point, rect)) { + if (isPointInRect(x, y, rect)) { return true; } } return false; } -function isPointInRect(point, rect) { +function isPointInRect(x, y, rect) { return ( - point.x >= rect.left && point.x < rect.right && - point.y >= rect.top && point.y < rect.bottom); + x >= rect.left && x < rect.right && + y >= rect.top && y < rect.bottom); } if (typeof document.caretRangeFromPoint !== 'function') { -- cgit v1.2.3 From d296ebd593d125d131b5bf9974e19d13ca3b3b3f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 17:39:13 -0400 Subject: Improve definition of caretRangeFromPoint --- ext/fg/js/document.js | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 1aa6f83b..a64b6c04 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -105,8 +105,8 @@ function docRangeFromPoint({x, y}) { } } - const range = document.caretRangeFromPoint(x, y); - if (range !== null && isPointInRange(point, range)) { + const range = caretRangeFromPoint(x, y); + if (range !== null) { if (imposter !== null) { docSetImposterStyle(imposterContainer.style, 'z-index', '-2147483646'); docSetImposterStyle(imposter.style, 'pointer-events', 'none'); @@ -235,15 +235,29 @@ function isPointInRect(x, y, rect) { y >= rect.top && y < rect.bottom); } -if (typeof document.caretRangeFromPoint !== 'function') { - document.caretRangeFromPoint = (x, y) => { - const position = document.caretPositionFromPoint(x, y); - if (position && position.offsetNode && position.offsetNode.nodeType === Node.TEXT_NODE) { +const caretRangeFromPoint = (() => { + if (typeof document.caretRangeFromPoint === 'function') { + // Chrome, Edge + return (x, y) => document.caretRangeFromPoint(x, y); + } + + if (typeof document.caretPositionFromPoint === 'function') { + // Firefox + return (x, y) => { + const position = document.caretPositionFromPoint(x, y); + const node = position.offsetNode; + if (node === null) { + return null; + } + const range = document.createRange(); - range.setStart(position.offsetNode, position.offset); - range.setEnd(position.offsetNode, position.offset); + const offset = (node.nodeType === Node.TEXT_NODE ? position.offset : 0); + range.setStart(node, offset); + range.setEnd(node, offset); return range; - } - return null; - }; -} + }; + } + + // No support + return () => null; +})(); -- cgit v1.2.3 From 737a5ee8a814bc89ac40f99264e8835c47f77387 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 19:47:00 -0400 Subject: Allow elements behind other transparent elements to be scanned --- ext/fg/js/document.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 3 deletions(-) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index a64b6c04..8bb857e7 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -17,6 +17,8 @@ */ +const REGEX_TRANSPARENT_COLOR = /rgba\s*\([^\)]*,\s*0(?:\.0+)?\s*\)/; + function docSetImposterStyle(style, propertyName, value) { style.setProperty(propertyName, value, 'important'); } @@ -88,10 +90,11 @@ function docImposterCreate(element, isTextarea) { } function docRangeFromPoint({x, y}) { - const element = document.elementFromPoint(x, y); + const elements = document.elementsFromPoint(x, y); let imposter = null; let imposterContainer = null; - if (element) { + if (elements.length > 0) { + const element = elements[0]; switch (element.nodeName) { case 'IMG': case 'BUTTON': @@ -105,7 +108,7 @@ function docRangeFromPoint({x, y}) { } } - const range = caretRangeFromPoint(x, y); + const range = caretRangeFromPointExt(x, y, elements); if (range !== null) { if (imposter !== null) { docSetImposterStyle(imposterContainer.style, 'z-index', '-2147483646'); @@ -261,3 +264,75 @@ const caretRangeFromPoint = (() => { // No support return () => null; })(); + +function caretRangeFromPointExt(x, y, elements) { + const modifications = []; + try { + let i = 0; + while (true) { + const range = caretRangeFromPoint(x, y); + if (range === null) { + return null; + } + + const inRange = isPointInRange(x, y, range); + if (inRange) { + return range; + } + + i = disableTransparentElement(elements, i, modifications); + if (i < 0) { + return null; + } + } + } finally { + if (modifications.length > 0) { + restoreElementStyleModifications(modifications); + } + } +} + +function disableTransparentElement(elements, i, modifications) { + while (true) { + if (i >= elements.length) { + return -1; + } + + const element = elements[i++]; + if (isElementTransparent(element)) { + const style = element.hasAttribute('style') ? element.getAttribute('style') : null; + modifications.push({element, style}); + element.style.pointerEvents = 'none'; + return i; + } + } +} + +function restoreElementStyleModifications(modifications) { + for (const {element, style} of modifications) { + if (style === null) { + element.removeAttribute('style'); + } else { + element.setAttribute('style', style); + } + } +} + +function isElementTransparent(element) { + if ( + element === document.body || + element === document.documentElement + ) { + return false; + } + const style = window.getComputedStyle(element); + return ( + parseFloat(style.opacity) < 0 || + style.visibility === 'hidden' || + (style.backgroundImage === 'none' && isColorTransparent(style.backgroundColor)) + ); +} + +function isColorTransparent(cssColor) { + return REGEX_TRANSPARENT_COLOR.test(cssColor); +} -- cgit v1.2.3 From 171e3f1097a86b993ba1e16c07c4ad6d5bff75ee Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 22:12:21 -0400 Subject: Add option for enabling deep scanning --- ext/bg/js/options.js | 3 ++- ext/bg/js/settings.js | 2 ++ ext/bg/settings.html | 4 ++++ ext/fg/js/document.js | 4 ++-- ext/fg/js/frontend.js | 2 +- ext/mixed/js/display.js | 2 +- 6 files changed, 12 insertions(+), 5 deletions(-) diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index c76525b9..7d993987 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -218,7 +218,8 @@ function optionsSetDefaults(options) { autoHideResults: false, delay: 20, length: 10, - modifier: 'shift' + modifier: 'shift', + deepDomScan: false }, dictionaries: {}, diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index c4eb4842..f5d669b2 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -47,6 +47,7 @@ async function formRead() { optionsNew.scanning.selectText = $('#select-matched-text').prop('checked'); optionsNew.scanning.alphanumeric = $('#search-alphanumeric').prop('checked'); optionsNew.scanning.autoHideResults = $('#auto-hide-results').prop('checked'); + optionsNew.scanning.deepDomScan = $('#deep-dom-scan').prop('checked'); optionsNew.scanning.delay = parseInt($('#scan-delay').val(), 10); optionsNew.scanning.length = parseInt($('#scan-length').val(), 10); optionsNew.scanning.modifier = $('#scan-modifier-key').val(); @@ -187,6 +188,7 @@ async function onReady() { $('#select-matched-text').prop('checked', options.scanning.selectText); $('#search-alphanumeric').prop('checked', options.scanning.alphanumeric); $('#auto-hide-results').prop('checked', options.scanning.autoHideResults); + $('#deep-dom-scan').prop('checked', options.scanning.deepDomScan); $('#scan-delay').val(options.scanning.delay); $('#scan-length').val(options.scanning.length); $('#scan-modifier-key').val(options.scanning.modifier); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 778dcee0..cc140023 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -192,6 +192,10 @@ +
+ +
+
diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 8bb857e7..727bc5d2 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}) { +function docRangeFromPoint({x, y}, options) { const elements = document.elementsFromPoint(x, y); let imposter = null; let imposterContainer = null; @@ -108,7 +108,7 @@ function docRangeFromPoint({x, y}) { } } - const range = caretRangeFromPointExt(x, y, elements); + const range = caretRangeFromPointExt(x, y, options.scanning.deepDomScan ? elements : []); if (range !== null) { if (imposter !== null) { docSetImposterStyle(imposterContainer.style, 'z-index', '-2147483646'); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 5a8d18c1..8a5c48d0 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -285,7 +285,7 @@ class Frontend { return; } - const textSource = docRangeFromPoint(point); + const textSource = docRangeFromPoint(point, this.options); let hideResults = !textSource || !textSource.containsPoint(point); let searched = false; let success = false; diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 4620e198..ebf56897 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -80,7 +80,7 @@ class Display { const {docRangeFromPoint, docSentenceExtract} = this.dependencies; const clickedElement = $(e.target); - const textSource = docRangeFromPoint({x: e.clientX, y: e.clientY}); + const textSource = docRangeFromPoint({x: e.clientX, y: e.clientY}, this.options); if (textSource === null) { return false; } -- cgit v1.2.3 From ee59b3ab8b21d19055302302f28709c6a4e7b918 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 22:38:01 -0400 Subject: Reduce amount of isPointInRange calls for repeated ranges --- ext/fg/js/document.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 727bc5d2..b6e1f83b 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -269,15 +269,19 @@ function caretRangeFromPointExt(x, y, elements) { const modifications = []; try { let i = 0; + let startContinerPre = null; while (true) { const range = caretRangeFromPoint(x, y); if (range === null) { return null; } - const inRange = isPointInRange(x, y, range); - if (inRange) { - return range; + const startContainer = range.startContainer; + if (startContinerPre !== startContainer) { + if (isPointInRange(x, y, range)) { + return range; + } + startContinerPre = startContainer; } i = disableTransparentElement(elements, i, modifications); -- cgit v1.2.3 From a2139213c85a0b44e8241f00bca909dd200068e8 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 23:44:24 -0400 Subject: Fix issue with whitespace ranges The size of the rects for these ranges will sometimes be excessively large on Firefox, leading to false positives. --- ext/fg/js/document.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index b6e1f83b..fc8000dd 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -199,10 +199,10 @@ function isPointInRange(x, y, range) { const nodePre = range.endContainer; const offsetPre = range.endOffset; try { - const {node, offset} = TextSourceRange.seekForward(range.endContainer, range.endOffset, 1); + const {node, offset, content} = TextSourceRange.seekForward(range.endContainer, range.endOffset, 1); range.setEnd(node, offset); - if (isPointInAnyRect(x, y, range.getClientRects())) { + if (!isWhitespace(content) && isPointInAnyRect(x, y, range.getClientRects())) { return true; } } finally { @@ -210,10 +210,10 @@ function isPointInRange(x, y, range) { } // Scan backward - const {node, offset} = TextSourceRange.seekBackward(range.startContainer, range.startOffset, 1); + const {node, offset, content} = TextSourceRange.seekBackward(range.startContainer, range.startOffset, 1); range.setStart(node, offset); - if (isPointInAnyRect(x, y, range.getClientRects())) { + if (!isWhitespace(content) && isPointInAnyRect(x, y, range.getClientRects())) { // This purposefully leaves the starting offset as modified and sets teh range length to 0. range.setEnd(node, offset); return true; @@ -223,6 +223,10 @@ function isPointInRange(x, y, range) { return false; } +function isWhitespace(string) { + return string.trim().length === 0; +} + function isPointInAnyRect(x, y, rects) { for (const rect of rects) { if (isPointInRect(x, y, rect)) { -- cgit v1.2.3 From c0bf6ff0339c3cdbb4976a6df844c67d50f90835 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 23:46:29 -0400 Subject: Fix issues caused by scanning ranges which don't start with a text node The rects returned by range.getClientRects() could include the entire start element's bounding box. --- ext/fg/js/document.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index fc8000dd..71a3d7f2 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -195,6 +195,11 @@ function docSentenceExtract(source, extent) { } function isPointInRange(x, y, range) { + // Require a text node to start + if (range.startContainer.nodeType !== Node.TEXT_NODE) { + return false; + } + // Scan forward const nodePre = range.endContainer; const offsetPre = range.endOffset; -- cgit v1.2.3 From 33076e9db9a4a4d6c33541dcfa6d76252ade95dc Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 23:51:30 -0400 Subject: Fix typo --- ext/fg/js/document.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 71a3d7f2..bd876e5d 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -219,7 +219,7 @@ function isPointInRange(x, y, range) { range.setStart(node, offset); if (!isWhitespace(content) && isPointInAnyRect(x, y, range.getClientRects())) { - // This purposefully leaves the starting offset as modified and sets teh range length to 0. + // This purposefully leaves the starting offset as modified and sets the range length to 0. range.setEnd(node, offset); return true; } -- cgit v1.2.3