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(-) (limited to 'ext/fg') 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