From a50ce724eb70f54f8e62207ec2630997967f36d7 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 23 Aug 2019 15:41:41 -0400 Subject: Suppress messages about unchecked runtime.lastError --- ext/bg/js/backend.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'ext') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index d49286d0..d95cb82d 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -57,9 +57,10 @@ class Backend { this.anki = new AnkiNull(); } + const callback = () => this.checkLastError(chrome.runtime.lastError); chrome.tabs.query({}, tabs => { for (const tab of tabs) { - chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, () => null); + chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, callback); } }); } @@ -147,6 +148,10 @@ class Backend { chrome.browserAction.setBadgeText({text}); } } + + checkLastError(e) { + // NOP + } } window.yomichan_backend = new Backend(); -- cgit v1.2.3 From c49f3c78383350b250c1dd823559bb8231f76e54 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 25 Aug 2019 11:08:13 -0400 Subject: Suppress messages about unchecked runtime.lastError on Firefox Mobile --- ext/fg/js/util.js | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'ext') diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index 954b3988..7518beb5 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -27,6 +27,7 @@ function utilInvoke(action, params={}) { return new Promise((resolve, reject) => { try { chrome.runtime.sendMessage({action, params}, (response) => { + utilCheckLastError(chrome.runtime.lastError); if (response !== null && typeof response === 'object') { if (response.error) { reject(response.error); @@ -43,3 +44,7 @@ function utilInvoke(action, params={}) { } }); } + +function utilCheckLastError(e) { + // NOP +} -- cgit v1.2.3 From a39a1fa9e4700d1189dfc5073b5fcb2557965671 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 22 Aug 2019 19:44:31 -0400 Subject: Add support for Edge browser --- ext/bg/background.html | 2 ++ ext/bg/context.html | 2 ++ ext/bg/js/search.js | 2 +- ext/bg/js/settings.js | 31 ++++++++++++++------------- ext/bg/js/util.js | 16 +++++++++++++- ext/bg/search.html | 2 ++ ext/bg/settings.html | 2 ++ ext/fg/float.html | 2 ++ ext/fg/js/float.js | 2 +- ext/manifest.json | 6 +++++- ext/mixed/js/extension.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 100 insertions(+), 20 deletions(-) create mode 100644 ext/mixed/js/extension.js (limited to 'ext') diff --git a/ext/bg/background.html b/ext/bg/background.html index 3262f2a1..5978f10f 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -10,6 +10,8 @@ + + diff --git a/ext/bg/context.html b/ext/bg/context.html index 01b4fb30..198ccd42 100644 --- a/ext/bg/context.html +++ b/ext/bg/context.html @@ -32,6 +32,8 @@ + + diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index f08f22da..a3382398 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -25,7 +25,7 @@ class DisplaySearch extends Display { this.query = $('#query').on('input', this.onSearchInput.bind(this)); this.intro = $('#intro'); - this.dependencies = {...this.dependencies, ...{docRangeFromPoint, docSentenceExtract}}; + this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); window.wanakana.bind(this.query.get(0)); } diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 75082f3e..e542118c 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -116,7 +116,7 @@ async function formMainDictionaryOptionsPopulate(options) { select.append($('')); let mainDictionary = ''; - for (const dictRow of await utilDatabaseSummarize()) { + for (const dictRow of toIterable(await utilDatabaseSummarize())) { if (dictRow.sequenced) { select.append($(``)); if (dictRow.title === options.general.mainDictionary) { @@ -314,12 +314,12 @@ async function dictionaryGroupsPopulate(options) { const dictGroups = $('#dict-groups').empty(); const dictWarning = $('#dict-warning').hide(); - const dictRows = await utilDatabaseSummarize(); + const dictRows = toIterable(await utilDatabaseSummarize()); if (dictRows.length === 0) { dictWarning.show(); } - for (const dictRow of dictRowsSort(dictRows, options)) { + for (const dictRow of toIterable(dictRowsSort(dictRows, options))) { const dictOptions = options.dictionaries[dictRow.title] || { enabled: false, priority: 0, @@ -581,20 +581,19 @@ async function onAnkiFieldTemplatesReset(e) { */ async function getBrowser() { - if (typeof chrome !== "undefined") { - if (typeof browser !== "undefined") { - try { - const info = await browser.runtime.getBrowserInfo(); - if (info.name === "Fennec") { - return "firefox-mobile"; - } - } catch (e) { } - return "firefox"; - } else { - return "chrome"; - } + if (EXTENSION_IS_BROWSER_EDGE) { + return 'edge'; + } + if (typeof browser !== 'undefined') { + try { + const info = await browser.runtime.getBrowserInfo(); + if (info.name === 'Fennec') { + return 'firefox-mobile'; + } + } catch (e) { } + return 'firefox'; } else { - return "edge"; + return 'chrome'; } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 34b06ddb..3dc7c900 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -87,6 +87,20 @@ function utilDatabasePurge() { return utilBackend().translator.database.purge(); } -function utilDatabaseImport(data, progress, exceptions) { +async function utilDatabaseImport(data, progress, exceptions) { + // Edge cannot read data on the background page due to the File object + // being created from a different window. Read on the same page instead. + if (EXTENSION_IS_BROWSER_EDGE) { + data = await utilReadFile(data); + } return utilBackend().translator.database.importDictionary(data, progress, exceptions); } + +function utilReadFile(file) { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = () => resolve(reader.result); + reader.onerror = () => reject(reader.error); + reader.readAsBinaryString(file); + }); +} diff --git a/ext/bg/search.html b/ext/bg/search.html index ce156578..05c0daab 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -37,6 +37,8 @@ + + diff --git a/ext/bg/settings.html b/ext/bg/settings.html index c6677018..0704140e 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -399,6 +399,8 @@ + + diff --git a/ext/fg/float.html b/ext/fg/float.html index 07f2d58b..0133e653 100644 --- a/ext/fg/float.html +++ b/ext/fg/float.html @@ -34,6 +34,8 @@ + + diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 090839a1..c0ec8a15 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -23,7 +23,7 @@ class DisplayFloat extends Display { this.autoPlayAudioTimer = null; this.styleNode = null; - this.dependencies = {...this.dependencies, ...{docRangeFromPoint, docSentenceExtract}}; + this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); $(window).on('message', utilAsync(this.onMessage.bind(this))); } diff --git a/ext/manifest.json b/ext/manifest.json index e3d7718b..baec58f6 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -11,10 +11,14 @@ }, "author": "Alex Yatskov", - "background": {"page": "bg/background.html"}, + "background": { + "page": "bg/background.html", + "persistent": true + }, "content_scripts": [{ "matches": ["http://*/*", "https://*/*", "file://*/*"], "js": [ + "mixed/js/extension.js", "fg/js/api.js", "fg/js/document.js", "fg/js/popup.js", diff --git a/ext/mixed/js/extension.js b/ext/mixed/js/extension.js new file mode 100644 index 00000000..d7085e5b --- /dev/null +++ b/ext/mixed/js/extension.js @@ -0,0 +1,53 @@ +/* + * 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 . + */ + + +function toIterable(value) { + if (typeof Symbol !== 'undefined' && typeof value[Symbol.iterator] !== 'undefined') { + return value; + } + + const array = JSON.parse(JSON.stringify(value)); + return Array.isArray(array) ? array : []; +} + +function extensionHasChrome() { + try { + return typeof chrome === 'object' && chrome !== null; + } catch (e) { + return false; + } +} + +function extensionHasBrowser() { + try { + return typeof browser === 'object' && browser !== null; + } catch (e) { + return false; + } +} + +const EXTENSION_IS_BROWSER_EDGE = ( + extensionHasBrowser() && + (!extensionHasChrome() || (typeof chrome.runtime === 'undefined' && typeof browser.runtime !== 'undefined')) +); + +if (EXTENSION_IS_BROWSER_EDGE) { + // Edge does not have chrome defined. + chrome = browser; +} -- cgit v1.2.3 From ea9c5ad86794db44eda32e735b8aa22c5eb7726e Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 29 Aug 2019 20:35:23 -0400 Subject: Update isPointInRange to be more accurate --- ext/fg/js/document.js | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) (limited to 'ext') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 86396a8a..8a412f96 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -17,8 +17,6 @@ */ -const IS_FIREFOX = /Firefox/.test(navigator.userAgent); - function docOffsetCalc(element) { const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft; @@ -162,22 +160,49 @@ function docSentenceExtract(source, extent) { } function isPointInRange(point, range) { - if (IS_FIREFOX) { - // Always return true on Firefox due to an issue where range.getClientRects() - // does not return a correct set of rects for characters at the beginning of a line. + // Scan forward + const nodePre = range.endContainer; + const offsetPre = range.endOffset; + try { + const {node, offset} = TextSourceRange.seekForward(range.endContainer, range.endOffset, 1); + range.setEnd(node, offset); + + if (isPointInAnyRect(point, range.getClientRects())) { + return true; + } + } finally { + range.setEnd(nodePre, offsetPre); + } + + // Scan backward + const {node, offset} = TextSourceRange.seekBackward(range.startContainer, range.startOffset, 1); + range.setStart(node, offset); + + if (isPointInAnyRect(point, range.getClientRects())) { + // This purposefully leaves the starting offset as modified and sets teh range length to 0. + range.setEnd(node, offset); return true; } - const y = point.y - 2; - for (const rect of range.getClientRects()) { - if (y <= rect.bottom) { + // No match + return false; +} + +function isPointInAnyRect(point, rects) { + for (const rect of rects) { + if (isPointInRect(point, rect)) { return true; } } - return false; } +function isPointInRect(point, rect) { + return ( + point.x >= rect.left && point.x < rect.right && + point.y >= rect.top && point.y < rect.bottom); +} + if (typeof document.caretRangeFromPoint !== 'function') { document.caretRangeFromPoint = (x, y) => { const position = document.caretPositionFromPoint(x, y); -- cgit v1.2.3 From 7ba71de5e772cf1bbde9510a0cae8b9f9cbc8936 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 29 Aug 2019 20:45:07 -0400 Subject: Replace double quotes with single quotes --- ext/bg/js/settings.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index e542118c..cdcdae77 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -599,7 +599,7 @@ 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; @@ -619,14 +619,14 @@ storageEstimate.mostRecent = null; async function storageInfoInitialize() { const browser = await getBrowser(); - const container = document.querySelector("#storage-info"); - container.setAttribute("data-browser", browser); + const container = document.querySelector('#storage-info'); + container.setAttribute('data-browser', browser); await storageShowInfo(); - container.classList.remove("storage-hidden"); + container.classList.remove('storage-hidden'); - document.querySelector("#storage-refresh").addEventListener('click', () => storageShowInfo(), false); + document.querySelector('#storage-refresh').addEventListener('click', () => storageShowInfo(), false); } async function storageUpdateStats() { @@ -636,8 +636,8 @@ 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); + document.querySelector('#storage-usage').textContent = storageBytesToLabeledString(estimate.usage); + document.querySelector('#storage-quota').textContent = storageBytesToLabeledString(estimate.quota); } storageUpdateStats.isUpdating = false; @@ -649,8 +649,8 @@ async function storageShowInfo() { storageSpinnerShow(true); const valid = await storageUpdateStats(); - document.querySelector("#storage-use").classList.toggle("storage-hidden", !valid); - document.querySelector("#storage-error").classList.toggle("storage-hidden", valid); + document.querySelector('#storage-use').classList.toggle('storage-hidden', !valid); + document.querySelector('#storage-error').classList.toggle('storage-hidden', valid); storageSpinnerShow(false); } -- cgit v1.2.3 From 68af0d86c3a941e185b34926fdbc57c457466e28 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 11:51:31 -0400 Subject: Improve popup position for vertical text --- ext/bg/js/options.js | 3 ++ ext/bg/js/settings.js | 6 +++ ext/bg/settings.html | 19 +++++++ ext/fg/css/client.css | 1 + ext/fg/js/frontend.js | 8 ++- ext/fg/js/popup.js | 147 +++++++++++++++++++++++++++++++++++++------------- ext/fg/js/source.js | 25 +++++++++ 7 files changed, 170 insertions(+), 39 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 29d8a215..c4a7e681 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -199,6 +199,9 @@ function optionsSetDefaults(options) { popupHeight: 250, popupHorizontalOffset: 0, popupVerticalOffset: 10, + popupHorizontalOffset2: 10, + popupVerticalOffset2: 0, + popupVerticalTextPosition: 'before', showGuide: true, compactTags: false, compactGlossaries: false, diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index cdcdae77..83a1c2f6 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -32,10 +32,13 @@ async function formRead() { optionsNew.general.showAdvanced = $('#show-advanced-options').prop('checked'); optionsNew.general.maxResults = parseInt($('#max-displayed-results').val(), 10); optionsNew.general.popupDisplayMode = $('#popup-display-mode').val(); + optionsNew.general.popupVerticalTextPosition = $('#popup-vertical-text-position').val(); optionsNew.general.popupWidth = parseInt($('#popup-width').val(), 10); optionsNew.general.popupHeight = parseInt($('#popup-height').val(), 10); optionsNew.general.popupHorizontalOffset = parseInt($('#popup-horizontal-offset').val(), 0); optionsNew.general.popupVerticalOffset = parseInt($('#popup-vertical-offset').val(), 10); + optionsNew.general.popupHorizontalOffset2 = parseInt($('#popup-horizontal-offset2').val(), 0); + optionsNew.general.popupVerticalOffset2 = parseInt($('#popup-vertical-offset2').val(), 10); optionsNew.general.customPopupCss = $('#custom-popup-css').val(); optionsNew.scanning.middleMouse = $('#middle-mouse-button-scan').prop('checked'); @@ -168,10 +171,13 @@ async function onReady() { $('#show-advanced-options').prop('checked', options.general.showAdvanced); $('#max-displayed-results').val(options.general.maxResults); $('#popup-display-mode').val(options.general.popupDisplayMode); + $('#popup-vertical-text-position').val(options.general.popupVerticalTextPosition); $('#popup-width').val(options.general.popupWidth); $('#popup-height').val(options.general.popupHeight); $('#popup-horizontal-offset').val(options.general.popupHorizontalOffset); $('#popup-vertical-offset').val(options.general.popupVerticalOffset); + $('#popup-horizontal-offset2').val(options.general.popupHorizontalOffset2); + $('#popup-vertical-offset2').val(options.general.popupVerticalOffset2); $('#custom-popup-css').val(options.general.customPopupCss); $('#middle-mouse-button-scan').prop('checked', options.scanning.middleMouse); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 0704140e..c6090c91 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -107,6 +107,17 @@ +
+ + +
+
@@ -133,6 +144,14 @@
+
+ +
+
+
+
+
+
diff --git a/ext/fg/css/client.css b/ext/fg/css/client.css index a9b8e025..a2b06d0f 100644 --- a/ext/fg/css/client.css +++ b/ext/fg/css/client.css @@ -26,6 +26,7 @@ iframe#yomichan-float { resize: both; visibility: hidden; z-index: 2147483647; + box-sizing: border-box; } iframe#yomichan-float.yomichan-float-full-width { diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 3c5f2ac8..72379062 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -301,7 +301,11 @@ class Frontend { } catch (e) { if (window.yomichan_orphaned) { if (textSource && this.options.scanning.modifier !== 'none') { - this.popup.showOrphaned(textSource.getRect(), this.options); + this.popup.showOrphaned( + textSource.getRect(), + textSource.getWritingMode(), + this.options + ); } } else { this.onError(e); @@ -332,6 +336,7 @@ class Frontend { const url = window.location.href; this.popup.termsShow( textSource.getRect(), + textSource.getWritingMode(), definitions, this.options, {sentence, url, focus} @@ -357,6 +362,7 @@ class Frontend { const url = window.location.href; this.popup.kanjiShow( textSource.getRect(), + textSource.getWritingMode(), definitions, this.options, {sentence, url, focus} diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 18dc0386..138dec41 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -48,59 +48,130 @@ class Popup { return this.injected; } - async show(elementRect, options) { + async show(elementRect, writingMode, options) { await this.inject(options); - const containerStyle = window.getComputedStyle(this.container); - const containerHeight = parseInt(containerStyle.height); - const containerWidth = parseInt(containerStyle.width); + const optionsGeneral = options.general; + const container = this.container; + const containerRect = container.getBoundingClientRect(); + const getPosition = ( + writingMode === 'horizontal-tb' || optionsGeneral.popupVerticalTextPosition === 'default' ? + Popup.getPositionForHorizontalText : + Popup.getPositionForVerticalText + ); - const limitX = document.body.clientWidth; - const limitY = window.innerHeight; + const [x, y, width, height, below] = getPosition( + elementRect, + Math.max(containerRect.width, optionsGeneral.popupWidth), + Math.max(containerRect.height, optionsGeneral.popupHeight), + document.body.clientWidth, + window.innerHeight, + optionsGeneral, + writingMode + ); - let x = elementRect.left + options.general.popupHorizontalOffset; - let width = Math.max(containerWidth, options.general.popupWidth); - const overflowX = Math.max(x + width - limitX, 0); + container.classList.toggle('yomichan-float-full-width', optionsGeneral.popupDisplayMode === 'full-width'); + container.classList.toggle('yomichan-float-above', !below); + container.style.left = `${x}px`; + container.style.top = `${y}px`; + container.style.width = `${width}px`; + container.style.height = `${height}px`; + container.style.visibility = 'visible'; + } + + static getPositionForHorizontalText(elementRect, width, height, maxWidth, maxHeight, optionsGeneral) { + let x = elementRect.left + optionsGeneral.popupHorizontalOffset; + const overflowX = Math.max(x + width - maxWidth, 0); if (overflowX > 0) { if (x >= overflowX) { x -= overflowX; } else { - width = limitX; + width = maxWidth; x = 0; } } - let above = false; - let y = 0; - let height = Math.max(containerHeight, options.general.popupHeight); - const yBelow = elementRect.bottom + options.general.popupVerticalOffset; - const yAbove = elementRect.top - options.general.popupVerticalOffset; - const overflowBelow = Math.max(yBelow + height - limitY, 0); - const overflowAbove = Math.max(height - yAbove, 0); - if (overflowBelow > 0 || overflowAbove > 0) { - if (overflowBelow < overflowAbove) { - height = Math.max(height - overflowBelow, 0); - y = yBelow; + const verticalOffset = optionsGeneral.popupVerticalOffset; + const [y, h, below] = Popup.limitGeometry( + elementRect.top - verticalOffset, + elementRect.bottom + verticalOffset, + height, + maxHeight, + true + ); + + return [x, y, width, h, below]; + } + + static getPositionForVerticalText(elementRect, width, height, maxWidth, maxHeight, optionsGeneral, writingMode) { + const preferRight = Popup.isVerticalTextPopupOnRight(optionsGeneral.popupVerticalTextPosition, writingMode); + const horizontalOffset = optionsGeneral.popupHorizontalOffset2; + const verticalOffset = optionsGeneral.popupVerticalOffset2; + + const [x, w] = Popup.limitGeometry( + elementRect.left - horizontalOffset, + elementRect.right + horizontalOffset, + width, + maxWidth, + preferRight + ); + const [y, h, below] = Popup.limitGeometry( + elementRect.bottom - verticalOffset, + elementRect.top + verticalOffset, + height, + maxHeight, + true + ); + return [x, y, w, h, below]; + } + + static isVerticalTextPopupOnRight(positionPreference, writingMode) { + switch (positionPreference) { + case 'before': + return !Popup.isWritingModeLeftToRight(writingMode); + case 'after': + return Popup.isWritingModeLeftToRight(writingMode); + case 'left': + return false; + case 'right': + return true; + } + } + + static isWritingModeLeftToRight(writingMode) { + switch (writingMode) { + case 'vertical-lr': + case 'sideways-lr': + return true; + default: + return false; + } + } + + static limitGeometry(positionBefore, positionAfter, size, limit, preferAfter) { + let after = preferAfter; + let position = 0; + const overflowBefore = Math.max(0, size - positionBefore); + const overflowAfter = Math.max(0, positionAfter + size - limit); + if (overflowAfter > 0 || overflowBefore > 0) { + if (overflowAfter < overflowBefore) { + size = Math.max(0, size - overflowAfter); + position = positionAfter; + after = true; } else { - height = Math.max(height - overflowAbove, 0); - y = Math.max(yAbove - height, 0); - above = true; + size = Math.max(0, size - overflowBefore); + position = Math.max(0, positionBefore - size); + after = false; } } else { - y = yBelow; + position = preferAfter ? positionAfter : positionBefore - size; } - this.container.classList.toggle('yomichan-float-full-width', options.general.popupDisplayMode === 'full-width'); - this.container.classList.toggle('yomichan-float-above', above); - this.container.style.left = `${x}px`; - this.container.style.top = `${y}px`; - this.container.style.width = `${width}px`; - this.container.style.height = `${height}px`; - this.container.style.visibility = 'visible'; + return [position, size, after]; } - async showOrphaned(elementRect, options) { - await this.show(elementRect, options); + async showOrphaned(elementRect, writingMode, options) { + await this.show(elementRect, writingMode, options); this.invokeApi('orphaned'); } @@ -136,13 +207,13 @@ class Popup { return contained; } - async termsShow(elementRect, definitions, options, context) { - await this.show(elementRect, options); + async termsShow(elementRect, writingMode, definitions, options, context) { + await this.show(elementRect, writingMode, options); this.invokeApi('termsShow', {definitions, options, context}); } - async kanjiShow(elementRect, definitions, options, context) { - await this.show(elementRect, options); + async kanjiShow(elementRect, writingMode, definitions, options, context) { + await this.show(elementRect, writingMode, options); this.invokeApi('kanjiShow', {definitions, options, context}); } diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index a360b331..a5421934 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -61,6 +61,10 @@ class TextSourceRange { return this.range.getBoundingClientRect(); } + getWritingMode() { + return TextSourceRange.getElementWritingMode(TextSourceRange.getParentElement(this.range.startContainer)); + } + getPaddedRect() { const range = this.range.cloneRange(); const startOffset = range.startOffset; @@ -204,6 +208,23 @@ class TextSourceRange { return state.remainder > 0; } + + static getParentElement(node) { + while (node !== null && node.nodeType !== Node.ELEMENT_NODE) { + node = node.parentNode; + } + return node; + } + + static getElementWritingMode(element) { + if (element === null) { + return 'horizontal-tb'; + } + + const style = window.getComputedStyle(element); + const writingMode = style.writingMode; + return typeof writingMode === 'string' ? writingMode : 'horizontal-tb'; + } } @@ -267,6 +288,10 @@ class TextSourceElement { return this.element.getBoundingClientRect(); } + getWritingMode() { + return 'horizontal-tb'; + } + select() { // NOP } -- cgit v1.2.3 From 85472d9407861f688e55b06b1767882f71229fc2 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 11:56:12 -0400 Subject: Add option for position of popup for horizontal text --- ext/bg/js/options.js | 1 + ext/bg/js/settings.js | 2 ++ ext/bg/settings.html | 8 ++++++++ ext/fg/js/popup.js | 4 +++- 4 files changed, 14 insertions(+), 1 deletion(-) (limited to 'ext') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index c4a7e681..c76525b9 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -201,6 +201,7 @@ function optionsSetDefaults(options) { popupVerticalOffset: 10, popupHorizontalOffset2: 10, popupVerticalOffset2: 0, + popupHorizontalTextPosition: 'below', popupVerticalTextPosition: 'before', showGuide: true, compactTags: false, diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 83a1c2f6..c4eb4842 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -32,6 +32,7 @@ async function formRead() { optionsNew.general.showAdvanced = $('#show-advanced-options').prop('checked'); optionsNew.general.maxResults = parseInt($('#max-displayed-results').val(), 10); optionsNew.general.popupDisplayMode = $('#popup-display-mode').val(); + optionsNew.general.popupHorizontalTextPosition = $('#popup-horizontal-text-position').val(); optionsNew.general.popupVerticalTextPosition = $('#popup-vertical-text-position').val(); optionsNew.general.popupWidth = parseInt($('#popup-width').val(), 10); optionsNew.general.popupHeight = parseInt($('#popup-height').val(), 10); @@ -171,6 +172,7 @@ async function onReady() { $('#show-advanced-options').prop('checked', options.general.showAdvanced); $('#max-displayed-results').val(options.general.maxResults); $('#popup-display-mode').val(options.general.popupDisplayMode); + $('#popup-horizontal-text-position').val(options.general.popupHorizontalTextPosition); $('#popup-vertical-text-position').val(options.general.popupVerticalTextPosition); $('#popup-width').val(options.general.popupWidth); $('#popup-height').val(options.general.popupHeight); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index c6090c91..4f244602 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -107,6 +107,14 @@
+
+ + +
+
- - - -
- -
- - +
+
+ + +
+
+ + +
+
-- cgit v1.2.3 From 9e9040178d1c498f4c7298a241137d2e7af59114 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 13:58:45 -0400 Subject: Fix some size and positioning issues related to imposter element --- ext/fg/js/document.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'ext') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 8a412f96..962592ec 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -17,16 +17,15 @@ */ -function docOffsetCalc(element) { +function docOffsetCalc(elementRect) { const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft; const clientTop = document.documentElement.clientTop || document.body.clientTop || 0; const clientLeft = document.documentElement.clientLeft || document.body.clientLeft || 0; - const rect = element.getBoundingClientRect(); - const top = Math.round(rect.top + scrollTop - clientTop); - const left = Math.round(rect.left + scrollLeft - clientLeft); + const top = Math.round(elementRect.top + scrollTop - clientTop); + const left = Math.round(elementRect.left + scrollLeft - clientLeft); return {top, left}; } @@ -38,7 +37,8 @@ function docImposterCreate(element) { stylePairs.push(`${key}: ${styleProps[key]};`); } - const offset = docOffsetCalc(element); + const elementRect = element.getBoundingClientRect(); + const offset = docOffsetCalc(elementRect); const imposter = document.createElement('div'); imposter.className = 'yomichan-imposter'; imposter.innerText = element.value; @@ -48,11 +48,22 @@ function docImposterCreate(element) { imposter.style.left = `${offset.left}px`; imposter.style.opacity = 0; imposter.style.zIndex = 2147483646; + imposter.style.margin = '0'; if (element.nodeName === 'TEXTAREA' && styleProps.overflow === 'visible') { imposter.style.overflow = 'auto'; } document.body.appendChild(imposter); + + // Adjust size + const imposterRect = imposter.getBoundingClientRect(); + if (imposterRect.width !== elementRect.width || imposterRect.height !== elementRect.height) { + const width = parseFloat(styleProps.width) + (elementRect.width - imposterRect.width); + const height = parseFloat(styleProps.height) + (elementRect.height - imposterRect.height); + imposter.style.width = `${width}px`; + imposter.style.height = `${height}px`; + } + imposter.scrollTop = element.scrollTop; imposter.scrollLeft = element.scrollLeft; -- cgit v1.2.3 From 62d66d93f7e7f666184aa215c876691c5433043f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 14:08:30 -0400 Subject: Remove rounding --- ext/fg/js/document.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ext') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 962592ec..514f8f2e 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -24,8 +24,8 @@ function docOffsetCalc(elementRect) { const clientTop = document.documentElement.clientTop || document.body.clientTop || 0; const clientLeft = document.documentElement.clientLeft || document.body.clientLeft || 0; - const top = Math.round(elementRect.top + scrollTop - clientTop); - const left = Math.round(elementRect.left + scrollLeft - clientLeft); + const top = elementRect.top + scrollTop - clientTop; + const left = elementRect.left + scrollLeft - clientLeft; return {top, left}; } -- cgit v1.2.3 From e47e041217708c49032dd3d215eacb23276a1f59 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 14:13:47 -0400 Subject: Disable pointer events on hidden imposter --- ext/fg/js/document.js | 1 + 1 file changed, 1 insertion(+) (limited to 'ext') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 514f8f2e..a017a0a6 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -94,6 +94,7 @@ function docRangeFromPoint(point) { const range = document.caretRangeFromPoint(point.x, point.y); if (imposter !== null) { imposter.style.zIndex = -2147483646; + imposter.style.pointerEvents = 'none'; } return range !== null && isPointInRange(point, range) ? new TextSourceRange(range) : null; -- cgit v1.2.3 From ad0dca7bb12d13545b559e9c738fcc0767ba20d5 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 14:57:24 -0400 Subject: Make the imposter element tracked using TextSourceRange --- ext/fg/js/document.js | 22 +++++++++++----------- ext/fg/js/frontend.js | 6 +++--- ext/fg/js/source.js | 15 +++++++++++++-- ext/mixed/js/display.js | 20 +++++++++++++------- 4 files changed, 40 insertions(+), 23 deletions(-) (limited to 'ext') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index a017a0a6..e6a16bd5 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -70,12 +70,6 @@ function docImposterCreate(element) { return imposter; } -function docImposterDestroy() { - for (const element of document.getElementsByClassName('yomichan-imposter')) { - element.parentNode.removeChild(element); - } -} - function docRangeFromPoint(point) { const element = document.elementFromPoint(point.x, point.y); let imposter = null; @@ -92,12 +86,18 @@ function docRangeFromPoint(point) { } const range = document.caretRangeFromPoint(point.x, point.y); - if (imposter !== null) { - imposter.style.zIndex = -2147483646; - imposter.style.pointerEvents = 'none'; + if (range !== null && isPointInRange(point, range)) { + if (imposter !== null) { + imposter.style.zIndex = -2147483646; + imposter.style.pointerEvents = 'none'; + } + return new TextSourceRange(range, '', imposter); + } else { + if (imposter !== null) { + imposter.parentNode.removeChild(imposter); + } + return null; } - - return range !== null && isPointInRange(point, range) ? new TextSourceRange(range) : null; } function docSentenceExtract(source, extent) { diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 3c5f2ac8..ebff768e 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -307,10 +307,11 @@ class Frontend { this.onError(e); } } finally { + if (textSource !== null) { + textSource.cleanup(); + } if (hideResults && this.options.scanning.autoHideResults) { this.searchClear(); - } else { - docImposterDestroy(); } this.pendingLookup = false; @@ -371,7 +372,6 @@ class Frontend { } searchClear() { - docImposterDestroy(); this.popup.hide(); this.popup.clearAutoPlayTimer(); diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index a360b331..409e81aa 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -25,13 +25,20 @@ const IGNORE_TEXT_PATTERN = /\u200c/; */ class TextSourceRange { - constructor(range, content='') { + constructor(range, content, imposter) { this.range = range; this.content = content; + this.imposter = imposter; } clone() { - return new TextSourceRange(this.range.cloneRange(), this.content); + return new TextSourceRange(this.range.cloneRange(), this.content, this.imposter); + } + + cleanup() { + if (this.imposter !== null && this.imposter.parentNode !== null) { + this.imposter.parentNode.removeChild(this.imposter); + } } text() { @@ -221,6 +228,10 @@ class TextSourceElement { return new TextSourceElement(this.element, this.content); } + cleanup() { + // NOP + } + text() { return this.content; } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index a2707bd0..4620e198 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -84,16 +84,22 @@ class Display { if (textSource === null) { return false; } - textSource.setEndOffset(this.options.scanning.length); - const {definitions, length} = await apiTermsFind(textSource.text()); - if (definitions.length === 0) { - return false; - } + let definitions, length, sentence; + try { + textSource.setEndOffset(this.options.scanning.length); - textSource.setEndOffset(length); + ({definitions, length} = await apiTermsFind(textSource.text())); + if (definitions.length === 0) { + return false; + } - const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); + textSource.setEndOffset(length); + + sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); + } finally { + textSource.cleanup(); + } const context = { source: { -- cgit v1.2.3 From 9b46fe70de9d0e7b625649fcdde53f5b91bbb114 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 15:10:46 -0400 Subject: Fix imposter issues with --- ext/fg/js/document.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'ext') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index e6a16bd5..1a49521b 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -30,7 +30,7 @@ function docOffsetCalc(elementRect) { return {top, left}; } -function docImposterCreate(element) { +function docImposterCreate(element, isTextarea) { const styleProps = window.getComputedStyle(element); const stylePairs = []; for (const key of styleProps) { @@ -49,8 +49,14 @@ function docImposterCreate(element) { imposter.style.opacity = 0; imposter.style.zIndex = 2147483646; imposter.style.margin = '0'; - if (element.nodeName === 'TEXTAREA' && styleProps.overflow === 'visible') { - imposter.style.overflow = 'auto'; + if (isTextarea) { + if (styleProps.overflow === 'visible') { + imposter.style.overflow = 'auto'; + } + } else { + imposter.style.overflow = 'hidden'; + imposter.style.whiteSpace = 'nowrap'; + imposter.style.lineHeight = styleProps.height; } document.body.appendChild(imposter); @@ -79,8 +85,10 @@ function docRangeFromPoint(point) { case 'BUTTON': return new TextSourceElement(element); case 'INPUT': + imposter = docImposterCreate(element, false); + break; case 'TEXTAREA': - imposter = docImposterCreate(element); + imposter = docImposterCreate(element, true); break; } } -- cgit v1.2.3 From e3e7dad2cc896a84c5c332f4c95a8984386be844 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 15:30:32 -0400 Subject: Use important CSS priority for imposter element styles --- ext/fg/js/document.js | 55 +++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 24 deletions(-) (limited to 'ext') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 1a49521b..247ff0ee 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -17,6 +17,10 @@ */ +function docSetImposterStyle(style, propertyName, value) { + style.setProperty(propertyName, value, 'important'); +} + function docOffsetCalc(elementRect) { const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft; @@ -31,32 +35,34 @@ function docOffsetCalc(elementRect) { } function docImposterCreate(element, isTextarea) { - const styleProps = window.getComputedStyle(element); - const stylePairs = []; - for (const key of styleProps) { - stylePairs.push(`${key}: ${styleProps[key]};`); - } - + const elementStyle = window.getComputedStyle(element); const elementRect = element.getBoundingClientRect(); const offset = docOffsetCalc(elementRect); const imposter = document.createElement('div'); + const imposterStyle = imposter.style; + imposter.className = 'yomichan-imposter'; imposter.innerText = element.value; - imposter.style.cssText = stylePairs.join('\n'); - imposter.style.position = 'absolute'; - imposter.style.top = `${offset.top}px`; - imposter.style.left = `${offset.left}px`; - imposter.style.opacity = 0; - imposter.style.zIndex = 2147483646; - imposter.style.margin = '0'; + + for (let i = 0, ii = elementStyle.length; i < ii; ++i) { + const property = elementStyle[i]; + docSetImposterStyle(imposterStyle, property, elementStyle.getPropertyValue(property)); + } + docSetImposterStyle(imposterStyle, 'position', 'absolute'); + docSetImposterStyle(imposterStyle, 'top', `${offset.top}px`); + docSetImposterStyle(imposterStyle, 'left', `${offset.left}px`); + docSetImposterStyle(imposterStyle, 'opacity', '0'); + docSetImposterStyle(imposterStyle, 'z-index', '2147483646'); + docSetImposterStyle(imposterStyle, 'margin', '0'); + if (isTextarea) { - if (styleProps.overflow === 'visible') { - imposter.style.overflow = 'auto'; + if (elementStyle.overflow === 'visible') { + docSetImposterStyle(imposterStyle, 'overflow', 'auto'); } } else { - imposter.style.overflow = 'hidden'; - imposter.style.whiteSpace = 'nowrap'; - imposter.style.lineHeight = styleProps.height; + docSetImposterStyle(imposterStyle, 'overflow', 'hidden'); + docSetImposterStyle(imposterStyle, 'white-space', 'nowrap'); + docSetImposterStyle(imposterStyle, 'line-height', elementStyle.height); } document.body.appendChild(imposter); @@ -64,10 +70,10 @@ function docImposterCreate(element, isTextarea) { // Adjust size const imposterRect = imposter.getBoundingClientRect(); if (imposterRect.width !== elementRect.width || imposterRect.height !== elementRect.height) { - const width = parseFloat(styleProps.width) + (elementRect.width - imposterRect.width); - const height = parseFloat(styleProps.height) + (elementRect.height - imposterRect.height); - imposter.style.width = `${width}px`; - imposter.style.height = `${height}px`; + const width = parseFloat(elementStyle.width) + (elementRect.width - imposterRect.width); + const height = parseFloat(elementStyle.height) + (elementRect.height - imposterRect.height); + docSetImposterStyle(imposterStyle, 'width', `${width}px`); + docSetImposterStyle(imposterStyle, 'height', `${height}px`); } imposter.scrollTop = element.scrollTop; @@ -96,8 +102,9 @@ function docRangeFromPoint(point) { const range = document.caretRangeFromPoint(point.x, point.y); if (range !== null && isPointInRange(point, range)) { if (imposter !== null) { - imposter.style.zIndex = -2147483646; - imposter.style.pointerEvents = 'none'; + const imposterStyle = imposter.style; + docSetImposterStyle(imposterStyle, 'z-index', '-2147483646'); + docSetImposterStyle(imposterStyle, 'pointer-events', 'none'); } return new TextSourceRange(range, '', imposter); } else { -- cgit v1.2.3 From e3d7ec8db7a86d475fba00d48f9cbe150feb36ff Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 1 Sep 2019 16:06:22 -0400 Subject: Create container for imposter element The container will prevent the imposter element's size from affecting the document's primary scrollbars. --- ext/fg/js/document.js | 63 +++++++++++++++++++++++++++------------------------ ext/fg/js/source.js | 10 ++++---- 2 files changed, 39 insertions(+), 34 deletions(-) (limited to 'ext') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 247ff0ee..dc2a9b87 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -21,27 +21,32 @@ function docSetImposterStyle(style, propertyName, value) { style.setProperty(propertyName, value, 'important'); } -function docOffsetCalc(elementRect) { - const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; - const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft; - - const clientTop = document.documentElement.clientTop || document.body.clientTop || 0; - const clientLeft = document.documentElement.clientLeft || document.body.clientLeft || 0; - - const top = elementRect.top + scrollTop - clientTop; - const left = elementRect.left + scrollLeft - clientLeft; - - return {top, left}; -} - function docImposterCreate(element, isTextarea) { const elementStyle = window.getComputedStyle(element); const elementRect = element.getBoundingClientRect(); - const offset = docOffsetCalc(elementRect); + const documentRect = document.documentElement.getBoundingClientRect(); + const left = elementRect.left - documentRect.left; + const top = elementRect.top - documentRect.top; + + // Container + const container = document.createElement('div'); + const containerStyle = container.style; + docSetImposterStyle(containerStyle, 'all', 'initial'); + docSetImposterStyle(containerStyle, 'position', 'absolute'); + docSetImposterStyle(containerStyle, 'left', '0'); + docSetImposterStyle(containerStyle, 'top', '0'); + docSetImposterStyle(containerStyle, 'width', `${documentRect.width}px`); + docSetImposterStyle(containerStyle, 'height', `${documentRect.height}px`); + docSetImposterStyle(containerStyle, 'overflow', 'hidden'); + docSetImposterStyle(containerStyle, 'opacity', '0'); + + docSetImposterStyle(containerStyle, 'pointer-events', 'none'); + docSetImposterStyle(containerStyle, 'z-index', '2147483646'); + + // Imposter const imposter = document.createElement('div'); const imposterStyle = imposter.style; - imposter.className = 'yomichan-imposter'; imposter.innerText = element.value; for (let i = 0, ii = elementStyle.length; i < ii; ++i) { @@ -49,11 +54,10 @@ function docImposterCreate(element, isTextarea) { docSetImposterStyle(imposterStyle, property, elementStyle.getPropertyValue(property)); } docSetImposterStyle(imposterStyle, 'position', 'absolute'); - docSetImposterStyle(imposterStyle, 'top', `${offset.top}px`); - docSetImposterStyle(imposterStyle, 'left', `${offset.left}px`); - docSetImposterStyle(imposterStyle, 'opacity', '0'); - docSetImposterStyle(imposterStyle, 'z-index', '2147483646'); + docSetImposterStyle(imposterStyle, 'top', `${top}px`); + docSetImposterStyle(imposterStyle, 'left', `${left}px`); docSetImposterStyle(imposterStyle, 'margin', '0'); + docSetImposterStyle(imposterStyle, 'pointer-events', 'auto'); if (isTextarea) { if (elementStyle.overflow === 'visible') { @@ -65,7 +69,8 @@ function docImposterCreate(element, isTextarea) { docSetImposterStyle(imposterStyle, 'line-height', elementStyle.height); } - document.body.appendChild(imposter); + container.appendChild(imposter); + document.body.appendChild(container); // Adjust size const imposterRect = imposter.getBoundingClientRect(); @@ -79,22 +84,23 @@ function docImposterCreate(element, isTextarea) { imposter.scrollTop = element.scrollTop; imposter.scrollLeft = element.scrollLeft; - return imposter; + return [imposter, container]; } function docRangeFromPoint(point) { const element = document.elementFromPoint(point.x, point.y); let imposter = null; + let imposterContainer = null; if (element) { switch (element.nodeName) { case 'IMG': case 'BUTTON': return new TextSourceElement(element); case 'INPUT': - imposter = docImposterCreate(element, false); + [imposter, imposterContainer] = docImposterCreate(element, false); break; case 'TEXTAREA': - imposter = docImposterCreate(element, true); + [imposter, imposterContainer] = docImposterCreate(element, true); break; } } @@ -102,14 +108,13 @@ function docRangeFromPoint(point) { const range = document.caretRangeFromPoint(point.x, point.y); if (range !== null && isPointInRange(point, range)) { if (imposter !== null) { - const imposterStyle = imposter.style; - docSetImposterStyle(imposterStyle, 'z-index', '-2147483646'); - docSetImposterStyle(imposterStyle, 'pointer-events', 'none'); + docSetImposterStyle(imposterContainer.style, 'z-index', '-2147483646'); + docSetImposterStyle(imposter.style, 'pointer-events', 'none'); } - return new TextSourceRange(range, '', imposter); + return new TextSourceRange(range, '', imposterContainer); } else { - if (imposter !== null) { - imposter.parentNode.removeChild(imposter); + if (imposterContainer !== null) { + imposterContainer.parentNode.removeChild(imposterContainer); } return null; } diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index 409e81aa..69d8197d 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -25,19 +25,19 @@ const IGNORE_TEXT_PATTERN = /\u200c/; */ class TextSourceRange { - constructor(range, content, imposter) { + constructor(range, content, imposterContainer) { this.range = range; this.content = content; - this.imposter = imposter; + this.imposterContainer = imposterContainer; } clone() { - return new TextSourceRange(this.range.cloneRange(), this.content, this.imposter); + return new TextSourceRange(this.range.cloneRange(), this.content, this.imposterContainer); } cleanup() { - if (this.imposter !== null && this.imposter.parentNode !== null) { - this.imposter.parentNode.removeChild(this.imposter); + if (this.imposterContainer !== null && this.imposterContainer.parentNode !== null) { + this.imposterContainer.parentNode.removeChild(this.imposterContainer); } } -- cgit v1.2.3 From e812e76e932fdfbdd1979f5990b66c10882fc80e Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 1 Sep 2019 13:36:41 -0400 Subject: Update deinflect.json --- ext/bg/lang/deinflect.json | 932 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 869 insertions(+), 63 deletions(-) (limited to 'ext') diff --git a/ext/bg/lang/deinflect.json b/ext/bg/lang/deinflect.json index 7ee00b2f..7a68ea71 100644 --- a/ext/bg/lang/deinflect.json +++ b/ext/bg/lang/deinflect.json @@ -784,6 +784,150 @@ "rulesOut": [ "adj-i" ] + }, + { + "kanaIn": "のたもうたら", + "kanaOut": "のたまう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "いったら", + "kanaOut": "いく", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "おうたら", + "kanaOut": "おう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "こうたら", + "kanaOut": "こう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "そうたら", + "kanaOut": "そう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "とうたら", + "kanaOut": "とう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "行ったら", + "kanaOut": "行く", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "逝ったら", + "kanaOut": "逝く", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "往ったら", + "kanaOut": "往く", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "請うたら", + "kanaOut": "請う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "乞うたら", + "kanaOut": "乞う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "恋うたら", + "kanaOut": "恋う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "問うたら", + "kanaOut": "問う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "負うたら", + "kanaOut": "負う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "沿うたら", + "kanaOut": "沿う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "添うたら", + "kanaOut": "添う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "副うたら", + "kanaOut": "副う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "厭うたら", + "kanaOut": "厭う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] } ], "-tari": [ @@ -891,6 +1035,150 @@ "rulesOut": [ "adj-i" ] + }, + { + "kanaIn": "のたもうたり", + "kanaOut": "のたまう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "いったり", + "kanaOut": "いく", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "おうたり", + "kanaOut": "おう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "こうたり", + "kanaOut": "こう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "そうたり", + "kanaOut": "そう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "とうたり", + "kanaOut": "とう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "行ったり", + "kanaOut": "行く", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "逝ったり", + "kanaOut": "逝く", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "往ったり", + "kanaOut": "往く", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "請うたり", + "kanaOut": "請う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "乞うたり", + "kanaOut": "乞う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "恋うたり", + "kanaOut": "恋う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "問うたり", + "kanaOut": "問う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "負うたり", + "kanaOut": "負う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "沿うたり", + "kanaOut": "沿う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "添うたり", + "kanaOut": "添う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "副うたり", + "kanaOut": "副う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "厭うたり", + "kanaOut": "厭う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] } ], "-te": [ @@ -998,6 +1286,150 @@ "rulesOut": [ "v5" ] + }, + { + "kanaIn": "のたもうて", + "kanaOut": "のたまう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "いって", + "kanaOut": "いく", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "おうて", + "kanaOut": "おう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "こうて", + "kanaOut": "こう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "そうて", + "kanaOut": "そう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "とうて", + "kanaOut": "とう", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "行って", + "kanaOut": "行く", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "逝って", + "kanaOut": "逝く", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "往って", + "kanaOut": "往く", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "請うて", + "kanaOut": "請う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "乞うて", + "kanaOut": "乞う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "恋うて", + "kanaOut": "恋う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "問うて", + "kanaOut": "問う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "負うて", + "kanaOut": "負う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "沿うて", + "kanaOut": "沿う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "添うて", + "kanaOut": "添う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "副うて", + "kanaOut": "副う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "厭うて", + "kanaOut": "厭う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] } ], "-zu": [ @@ -1250,6 +1682,16 @@ "vk" ] }, + { + "kanaIn": "させる", + "kanaOut": "す", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, { "kanaIn": "たせる", "kanaOut": "つ", @@ -1497,6 +1939,14 @@ "v5" ] }, + { + "kanaIn": "き", + "kanaOut": "くる", + "rulesIn": [], + "rulesOut": [ + "vk" + ] + }, { "kanaIn": "ぎ", "kanaOut": "ぎる", @@ -1880,6 +2330,16 @@ "vs" ] }, + { + "kanaIn": "される", + "kanaOut": "す", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, { "kanaIn": "たれる", "kanaOut": "つ", @@ -1891,162 +2351,304 @@ ] }, { - "kanaIn": "なれる", - "kanaOut": "ぬ", - "rulesIn": [ - "v1" - ], + "kanaIn": "なれる", + "kanaOut": "ぬ", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "ばれる", + "kanaOut": "ぶ", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "まれる", + "kanaOut": "む", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "われる", + "kanaOut": "う", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "られる", + "kanaOut": "る", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + } + ], + "past": [ + { + "kanaIn": "た", + "kanaOut": "る", + "rulesIn": [], + "rulesOut": [ + "v1", + "vk" + ] + }, + { + "kanaIn": "いた", + "kanaOut": "く", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "いだ", + "kanaOut": "ぐ", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "きた", + "kanaOut": "くる", + "rulesIn": [], + "rulesOut": [ + "vk" + ] + }, + { + "kanaIn": "した", + "kanaOut": "す", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "した", + "kanaOut": "する", + "rulesIn": [], + "rulesOut": [ + "vs" + ] + }, + { + "kanaIn": "った", + "kanaOut": "う", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "った", + "kanaOut": "つ", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "った", + "kanaOut": "る", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "んだ", + "kanaOut": "ぬ", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "んだ", + "kanaOut": "ぶ", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "んだ", + "kanaOut": "む", + "rulesIn": [], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "かった", + "kanaOut": "い", + "rulesIn": [], + "rulesOut": [ + "adj-i" + ] + }, + { + "kanaIn": "のたもうた", + "kanaOut": "のたまう", + "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "ばれる", - "kanaOut": "ぶ", - "rulesIn": [ - "v1" - ], + "kanaIn": "いった", + "kanaOut": "いく", + "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "まれる", - "kanaOut": "む", - "rulesIn": [ - "v1" - ], + "kanaIn": "おうた", + "kanaOut": "おう", + "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "われる", - "kanaOut": "う", - "rulesIn": [ - "v1" - ], + "kanaIn": "こうた", + "kanaOut": "こう", + "rulesIn": [], "rulesOut": [ "v5" ] - } - ], - "passive or causative": [ + }, { - "kanaIn": "される", - "kanaOut": "す", - "rulesIn": [ - "v1" - ], + "kanaIn": "そうた", + "kanaOut": "そう", + "rulesIn": [], "rulesOut": [ "v5" ] - } - ], - "past": [ + }, { - "kanaIn": "た", - "kanaOut": "る", + "kanaIn": "とうた", + "kanaOut": "とう", "rulesIn": [], "rulesOut": [ - "v1", - "vk" + "v5" ] }, { - "kanaIn": "いた", - "kanaOut": "く", + "kanaIn": "行った", + "kanaOut": "行く", "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "いだ", - "kanaOut": "ぐ", + "kanaIn": "逝った", + "kanaOut": "逝く", "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "きた", - "kanaOut": "くる", + "kanaIn": "往った", + "kanaOut": "往く", "rulesIn": [], "rulesOut": [ - "vk" + "v5" ] }, { - "kanaIn": "した", - "kanaOut": "す", + "kanaIn": "請うた", + "kanaOut": "請う", "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "した", - "kanaOut": "する", + "kanaIn": "乞うた", + "kanaOut": "乞う", "rulesIn": [], "rulesOut": [ - "vs" + "v5" ] }, { - "kanaIn": "った", - "kanaOut": "う", + "kanaIn": "恋うた", + "kanaOut": "恋う", "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "った", - "kanaOut": "つ", + "kanaIn": "問うた", + "kanaOut": "問う", "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "った", - "kanaOut": "る", + "kanaIn": "負うた", + "kanaOut": "負う", "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "んだ", - "kanaOut": "ぬ", + "kanaIn": "沿うた", + "kanaOut": "沿う", "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "んだ", - "kanaOut": "ぶ", + "kanaIn": "添うた", + "kanaOut": "添う", "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "んだ", - "kanaOut": "む", + "kanaIn": "副うた", + "kanaOut": "副う", "rulesIn": [], "rulesOut": [ "v5" ] }, { - "kanaIn": "かった", - "kanaOut": "い", + "kanaIn": "厭うた", + "kanaOut": "厭う", "rulesIn": [], "rulesOut": [ - "adj-i" + "v5" ] } ], @@ -2674,7 +3276,6 @@ ], "rulesOut": [ "v1", - "v5", "vk" ] }, @@ -2787,5 +3388,210 @@ "vs" ] } + ], + "causative passive": [ + { + "kanaIn": "かされる", + "kanaOut": "く", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "がされる", + "kanaOut": "ぐ", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "たされる", + "kanaOut": "つ", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "なされる", + "kanaOut": "ぬ", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "ばされる", + "kanaOut": "ぶ", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "まされる", + "kanaOut": "む", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "らされる", + "kanaOut": "る", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "わされる", + "kanaOut": "う", + "rulesIn": [ + "v1" + ], + "rulesOut": [ + "v5" + ] + } + ], + "-toku": [ + { + "kanaIn": "いとく", + "kanaOut": "く", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "いどく", + "kanaOut": "ぐ", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "きとく", + "kanaOut": "くる", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "vk" + ] + }, + { + "kanaIn": "しとく", + "kanaOut": "す", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "しとく", + "kanaOut": "する", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "vs" + ] + }, + { + "kanaIn": "っとく", + "kanaOut": "う", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "っとく", + "kanaOut": "つ", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "っとく", + "kanaOut": "る", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "んどく", + "kanaOut": "ぬ", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "んどく", + "kanaOut": "ぶ", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "んどく", + "kanaOut": "む", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "v5" + ] + }, + { + "kanaIn": "とく", + "kanaOut": "る", + "rulesIn": [ + "v5" + ], + "rulesOut": [ + "v1", + "vk" + ] + } ] } -- cgit v1.2.3 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(-) (limited to 'ext') 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(-) (limited to 'ext') 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(-) (limited to 'ext') 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(-) (limited to 'ext') 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(-) (limited to 'ext') 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(-) (limited to 'ext') 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(+) (limited to 'ext') 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(-) (limited to 'ext') 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 From 4ac55da7dd5354e6c3495f04583352d0d863b7b6 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 2 Sep 2019 11:46:00 -0700 Subject: version bump --- 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 e12f85ad..62eed6ec 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Yomichan", - "version": "1.7.4", + "version": "1.7.5", "description": "Japanese dictionary with Anki integration (testing)", "icons": {"16": "mixed/img/icon16.png", "48": "mixed/img/icon48.png", "128": "mixed/img/icon128.png"}, -- cgit v1.2.3