From 5c4614f585648c2b835efc1d369e78918bc4f5ff Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 17 Aug 2019 18:50:48 -0400 Subject: Add support for showing recursive popups --- ext/fg/js/frontend.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 8a5c48d0..d378dd61 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -18,8 +18,8 @@ class Frontend { - constructor() { - this.popup = new Popup(); + constructor(popup) { + this.popup = popup; this.popupTimer = null; this.mouseDownLeft = false; this.mouseDownMiddle = false; @@ -36,6 +36,25 @@ class Frontend { this.scrollPrevent = false; } + static create() { + const floatUrl = chrome.extension.getURL('/fg/float.html'); + const currentUrl = location.href.replace(/[\?#][\w\W]*$/, ""); + const isNested = (currentUrl === floatUrl); + + let id = null; + if (isNested) { + const match = /[&?]id=([^&]*?)(?:&|$)/.exec(location.href); + if (match !== null) { + id = match[1]; + } + } + + const popup = isNested ? new PopupProxy(id) : PopupProxyHost.instance.createPopup(); + const frontend = new Frontend(popup); + frontend.prepare(); + return frontend; + } + async prepare() { try { this.options = await apiOptionsGet(); @@ -259,9 +278,8 @@ class Frontend { const handler = handlers[action]; if (handler) { handler(params); + callback(); } - - callback(); } onError(error) { @@ -281,7 +299,10 @@ class Frontend { } async searchAt(point, type) { - if (this.pendingLookup || this.popup.containsPoint(point)) { + if ( + this.pendingLookup || + (this.popup.containsPointIsAsync() ? await this.popup.containsPointAsync(point) : this.popup.containsPoint(point)) + ) { return; } @@ -482,5 +503,4 @@ class Frontend { } } -window.yomichan_frontend = new Frontend(); -window.yomichan_frontend.prepare(); +window.yomichan_frontend = Frontend.create(); -- cgit v1.2.3 From 53aad0bef68bf6930b684fda4a25e1a045cd800e Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 18 Aug 2019 20:51:19 -0400 Subject: Fix messaging issues when iframes are present in the document --- ext/bg/js/api.js | 5 ++++ ext/bg/js/backend.js | 4 +++ ext/bg/js/search-frontend.js | 2 +- ext/fg/js/api.js | 4 +++ ext/fg/js/frontend-api-receiver.js | 16 +++++------ ext/fg/js/frontend-api-sender.js | 8 +++--- ext/fg/js/frontend.js | 10 +++++-- ext/fg/js/popup-proxy-host.js | 21 ++++++++++++--- ext/fg/js/popup-proxy.js | 9 +++++-- ext/fg/js/popup.js | 55 +++++++++++++++++++++++++------------- ext/manifest.json | 2 +- 11 files changed, 97 insertions(+), 39 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 4b2bacd7..b8ef4362 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -205,3 +205,8 @@ function apiForward(action, params, sender) { chrome.tabs.sendMessage(tabId, {action, params}, (response) => resolve(response)); }); } + +function apiFrameInformationGet(sender) { + const frameId = sender.frameId; + return Promise.resolve({frameId}); +} diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 8f99c13a..39fd4288 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -127,6 +127,10 @@ class Backend { forward: ({action, params}) => { forward(apiForward(action, params, sender), callback); + }, + + frameInformationGet: () => { + forward(apiFrameInformationGet(sender), callback); } }; diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-frontend.js index 77aa4052..840a1ea8 100644 --- a/ext/bg/js/search-frontend.js +++ b/ext/bg/js/search-frontend.js @@ -25,8 +25,8 @@ async function searchFrontendSetup() { '/fg/js/api.js', '/fg/js/frontend-api-receiver.js', '/fg/js/popup.js', - '/fg/js/popup-proxy-host.js', '/fg/js/util.js', + '/fg/js/popup-proxy-host.js', '/fg/js/frontend.js' ]; for (const src of scriptSrcs) { diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index 99ad307c..6bcb0dbb 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -64,3 +64,7 @@ function apiScreenshotGet(options) { function apiForward(action, params) { return utilInvoke('forward', {action, params}); } + +function apiFrameInformationGet() { + return utilInvoke('frameInformationGet'); +} diff --git a/ext/fg/js/frontend-api-receiver.js b/ext/fg/js/frontend-api-receiver.js index f5d29f67..687e5c3c 100644 --- a/ext/fg/js/frontend-api-receiver.js +++ b/ext/fg/js/frontend-api-receiver.js @@ -31,7 +31,7 @@ class FrontendApiReceiver { port.onMessage.addListener(this.onMessage.bind(this, port)); } - onMessage(port, {id, action, params, target}) { + onMessage(port, {id, action, params, target, senderId}) { if ( target !== this.source || !this.handlers.hasOwnProperty(action) @@ -39,24 +39,24 @@ class FrontendApiReceiver { return; } - this.sendAck(port, id); + this.sendAck(port, id, senderId); const handler = this.handlers[action]; handler(params).then( result => { - this.sendResult(port, id, {result}); + this.sendResult(port, id, senderId, {result}); }, e => { const error = typeof e.toString === 'function' ? e.toString() : e; - this.sendResult(port, id, {error}); + this.sendResult(port, id, senderId, {error}); }); } - sendAck(port, id) { - port.postMessage({type: 'ack', id}); + sendAck(port, id, senderId) { + port.postMessage({type: 'ack', id, senderId}); } - sendResult(port, id, data) { - port.postMessage({type: 'result', id, data}); + sendResult(port, id, senderId, data) { + port.postMessage({type: 'result', id, senderId, data}); } } diff --git a/ext/fg/js/frontend-api-sender.js b/ext/fg/js/frontend-api-sender.js index e2becb90..a1cb02c4 100644 --- a/ext/fg/js/frontend-api-sender.js +++ b/ext/fg/js/frontend-api-sender.js @@ -19,6 +19,7 @@ class FrontendApiSender { constructor() { + this.senderId = FrontendApiSender.generateId(16); this.ackTimeout = 3000; // 3 seconds this.responseTimeout = 10000; // 10 seconds this.callbacks = {}; @@ -43,11 +44,12 @@ class FrontendApiSender { this.callbacks[id] = info; info.timer = setTimeout(() => this.onError(id, 'Timeout (ack)'), this.ackTimeout); - this.port.postMessage({id, action, params, target}); + this.port.postMessage({id, action, params, target, senderId: this.senderId}); }); } - onMessage({type, id, data}) { + onMessage({type, id, data, senderId}) { + if (senderId !== this.senderId) { return; } switch (type) { case 'ack': this.onAck(id); @@ -69,7 +71,7 @@ class FrontendApiSender { onAck(id) { if (!this.callbacks.hasOwnProperty(id)) { - console.warn(`ID ${id} not found`); + console.warn(`ID ${id} not found for ack`); return; } diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index d378dd61..9c511d8a 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -42,14 +42,20 @@ class Frontend { const isNested = (currentUrl === floatUrl); let id = null; + let parentFrameId = null; if (isNested) { - const match = /[&?]id=([^&]*?)(?:&|$)/.exec(location.href); + let match = /[&?]id=([^&]*?)(?:&|$)/.exec(location.href); if (match !== null) { id = match[1]; } + + match = /[&?]parent=(\d+)(?:&|$)/.exec(location.href); + if (match !== null) { + parentFrameId = parseInt(match[1], 10); + } } - const popup = isNested ? new PopupProxy(id) : PopupProxyHost.instance.createPopup(); + const popup = isNested ? new PopupProxy(id, parentFrameId) : PopupProxyHost.instance.createPopup(null); const frontend = new Frontend(popup); frontend.prepare(); return frontend; diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index ba3db832..cdd1d02c 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -21,7 +21,22 @@ class PopupProxyHost { constructor() { this.popups = {}; this.nextId = 0; - this.apiReceiver = new FrontendApiReceiver('popup-proxy-host', { + this.apiReceiver = null; + this.frameIdPromise = null; + } + + static create() { + const popupProxyHost = new PopupProxyHost(); + popupProxyHost.prepare(); + return popupProxyHost; + } + + async prepare() { + this.frameIdPromise = apiFrameInformationGet(); + const {frameId} = await this.frameIdPromise; + if (typeof frameId !== 'number') { return; } + + this.apiReceiver = new FrontendApiReceiver(`popup-proxy-host#${frameId}`, { createNestedPopup: ({parentId}) => this.createNestedPopup(parentId), show: ({id, elementRect, options}) => this.show(id, elementRect, options), showOrphaned: ({id, elementRect, options}) => this.show(id, elementRect, options), @@ -39,7 +54,7 @@ class PopupProxyHost { const depth = (parent !== null ? parent.depth + 1 : 0); const id = `${this.nextId}`; ++this.nextId; - const popup = new Popup(id, depth); + const popup = new Popup(id, depth, this.frameIdPromise); if (parent !== null) { popup.parent = parent; parent.children.push(popup); @@ -116,4 +131,4 @@ class PopupProxyHost { } } -PopupProxyHost.instance = new PopupProxyHost(); +PopupProxyHost.instance = PopupProxyHost.create(); diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index 3a15be7d..bbf6a2cf 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -18,12 +18,14 @@ class PopupProxy { - constructor(parentId) { + constructor(parentId, parentFrameId) { this.parentId = parentId; + this.parentFrameId = parentFrameId; this.id = null; this.idPromise = null; this.parent = null; this.children = []; + this.depth = 0; this.container = null; @@ -102,7 +104,10 @@ class PopupProxy { } invokeHostApi(action, params={}) { - return this.apiSender.invoke(action, params, 'popup-proxy-host'); + if (typeof this.parentFrameId !== 'number') { + return Promise.reject('Invalid frame'); + } + return this.apiSender.invoke(action, params, `popup-proxy-host#${this.parentFrameId}`); } static DOMRectToJson(domRect) { diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 00dfeb89..24b5684d 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -18,38 +18,55 @@ class Popup { - constructor(id, depth) { + constructor(id, depth, frameIdPromise) { this.id = id; this.depth = depth; + this.frameIdPromise = frameIdPromise; + this.frameId = null; this.parent = null; this.children = []; this.container = document.createElement('iframe'); this.container.id = 'yomichan-float'; this.container.addEventListener('mousedown', e => e.stopPropagation()); this.container.addEventListener('scroll', e => e.stopPropagation()); - this.container.setAttribute('src', chrome.extension.getURL(`/fg/float.html?id=${id}&depth=${depth}`)); this.container.style.width = '0px'; this.container.style.height = '0px'; - this.injected = null; + this.injectPromise = null; + this.isInjected = false; } inject(options) { - if (!this.injected) { - this.injected = new Promise((resolve, reject) => { - this.container.addEventListener('load', () => { - this.invokeApi('setOptions', { - general: { - customPopupCss: options.general.customPopupCss - } - }); - resolve(); - }); - this.observeFullscreen(); - this.onFullscreenChanged(); - }); + if (this.injectPromise === null) { + this.injectPromise = this.createInjectPromise(options); + } + return this.injectPromise; + } + + async createInjectPromise(options) { + try { + const {frameId} = await this.frameIdPromise; + if (typeof frameId === 'number') { + this.frameId = frameId; + } + } catch (e) { + // NOP } - return this.injected; + return new Promise((resolve) => { + const parent = (typeof this.frameId === 'number' ? this.frameId : ''); + this.container.setAttribute('src', chrome.extension.getURL(`/fg/float.html?id=${this.id}&depth=${this.depth}&parent=${parent}`)); + this.container.addEventListener('load', () => { + this.invokeApi('setOptions', { + general: { + customPopupCss: options.general.customPopupCss + } + }); + resolve(); + }); + this.observeFullscreen(); + this.onFullscreenChanged(); + this.isInjected = true; + }); } async show(elementRect, writingMode, options) { @@ -215,7 +232,7 @@ class Popup { } isVisible() { - return this.injected && this.container.style.visibility !== 'hidden'; + return this.isInjected && this.container.style.visibility !== 'hidden'; } setVisible(visible) { @@ -260,7 +277,7 @@ class Popup { } clearAutoPlayTimer() { - if (this.injected) { + if (this.isInjected) { this.invokeApi('clearAutoPlayTimer'); } } diff --git a/ext/manifest.json b/ext/manifest.json index eed6e40a..06d13dd1 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -23,9 +23,9 @@ "fg/js/document.js", "fg/js/frontend-api-receiver.js", "fg/js/popup.js", - "fg/js/popup-proxy-host.js", "fg/js/source.js", "fg/js/util.js", + "fg/js/popup-proxy-host.js", "fg/js/frontend.js" ], "css": ["fg/css/client.css"], -- cgit v1.2.3 From 3491affcf13ef10b78714d4955da6ee9aeb11457 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 18 Aug 2019 21:49:55 -0400 Subject: Update nested initialization parameters passed via message rather than using the URL query string --- ext/fg/js/float.js | 4 ++++ ext/fg/js/frontend.js | 20 +++----------------- ext/fg/js/popup-nested.js | 19 ++++++++++--------- ext/fg/js/popup.js | 9 +++++++-- 4 files changed, 24 insertions(+), 28 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index c0ec8a15..3c521714 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -72,6 +72,10 @@ class DisplayFloat extends Display { if (css) { this.setStyle(css); } + }, + + popupNestedInitialize: ({id, depth, parentFrameId}) => { + popupNestedInitialize(id, depth, parentFrameId); } }; diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 9c511d8a..3605dffd 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -37,23 +37,9 @@ class Frontend { } static create() { - const floatUrl = chrome.extension.getURL('/fg/float.html'); - const currentUrl = location.href.replace(/[\?#][\w\W]*$/, ""); - const isNested = (currentUrl === floatUrl); - - let id = null; - let parentFrameId = null; - if (isNested) { - let match = /[&?]id=([^&]*?)(?:&|$)/.exec(location.href); - if (match !== null) { - id = match[1]; - } - - match = /[&?]parent=(\d+)(?:&|$)/.exec(location.href); - if (match !== null) { - parentFrameId = parseInt(match[1], 10); - } - } + const initializationData = window.frontendInitializationData; + const isNested = (initializationData !== null && typeof initializationData === 'object'); + const {id, parentFrameId} = initializationData || {}; const popup = isNested ? new PopupProxy(id, parentFrameId) : PopupProxyHost.instance.createPopup(null); const frontend = new Frontend(popup); diff --git a/ext/fg/js/popup-nested.js b/ext/fg/js/popup-nested.js index 7df4e4e2..ad235cc6 100644 --- a/ext/fg/js/popup-nested.js +++ b/ext/fg/js/popup-nested.js @@ -17,20 +17,23 @@ */ -async function popupNestedSetup() { - const options = await apiOptionsGet(); - const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth; +let popupNestedInitialized = false; - let depth = null; - const match = /[&?]depth=([^&]*?)(?:&|$)/.exec(location.href); - if (match !== null) { - depth = parseInt(match[1], 10); +async function popupNestedInitialize(id, depth, parentFrameId) { + if (popupNestedInitialized) { + return; } + popupNestedInitialized = true; + + const options = await apiOptionsGet(); + const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth; if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) { return; } + window.frontendInitializationData = {id, depth, parentFrameId}; + const scriptSrcs = [ '/fg/js/frontend-api-sender.js', '/fg/js/popup.js', @@ -44,5 +47,3 @@ async function popupNestedSetup() { document.body.appendChild(script); } } - -popupNestedSetup(); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 24b5684d..ab1dbbed 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -29,6 +29,7 @@ class Popup { this.container.id = 'yomichan-float'; this.container.addEventListener('mousedown', e => e.stopPropagation()); this.container.addEventListener('scroll', e => e.stopPropagation()); + this.container.setAttribute('src', chrome.extension.getURL('/fg/float.html')); this.container.style.width = '0px'; this.container.style.height = '0px'; this.injectPromise = null; @@ -53,9 +54,13 @@ class Popup { } return new Promise((resolve) => { - const parent = (typeof this.frameId === 'number' ? this.frameId : ''); - this.container.setAttribute('src', chrome.extension.getURL(`/fg/float.html?id=${this.id}&depth=${this.depth}&parent=${parent}`)); + const parentFrameId = (typeof this.frameId === 'number' ? this.frameId : null); this.container.addEventListener('load', () => { + this.invokeApi('popupNestedInitialize', { + id: this.id, + depth: this.depth, + parentFrameId + }); this.invokeApi('setOptions', { general: { customPopupCss: options.general.customPopupCss -- cgit v1.2.3 From 71471d08e56f1a2431e67d0439724cc52a1ea73e Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 25 Aug 2019 21:13:17 -0400 Subject: Add option to enable/disable scanning of source expressions in popups --- ext/bg/js/options.js | 1 + ext/bg/js/settings.js | 2 ++ ext/bg/settings.html | 4 +++ ext/fg/js/frontend.js | 86 +++++++++++++++++++++++++++++++++++++++++++---- ext/fg/js/popup-nested.js | 4 ++- 5 files changed, 89 insertions(+), 8 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 1b2d5e1a..df95aae9 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -221,6 +221,7 @@ function optionsSetDefaults(options) { modifier: 'shift', deepDomScan: false, popupNestingMaxDepth: 0, + enableOnPopupExpressions: false, enableOnSearchPage: true }, diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 5472c7bd..83f4528c 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -48,6 +48,7 @@ async function formRead() { 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.enableOnPopupExpressions = $('#enable-scanning-of-popup-expressions').prop('checked'); optionsNew.scanning.enableOnSearchPage = $('#enable-scanning-on-search-page').prop('checked'); optionsNew.scanning.delay = parseInt($('#scan-delay').val(), 10); optionsNew.scanning.length = parseInt($('#scan-length').val(), 10); @@ -191,6 +192,7 @@ async function onReady() { $('#search-alphanumeric').prop('checked', options.scanning.alphanumeric); $('#auto-hide-results').prop('checked', options.scanning.autoHideResults); $('#deep-dom-scan').prop('checked', options.scanning.deepDomScan); + $('#enable-scanning-of-popup-expressions').prop('checked', options.scanning.enableOnPopupExpressions); $('#enable-scanning-on-search-page').prop('checked', options.scanning.enableOnSearchPage); $('#scan-delay').val(options.scanning.delay); $('#scan-length').val(options.scanning.length); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 0005efa6..85b7ee5f 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -192,6 +192,10 @@ +
+ +
+
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 3605dffd..f59ea318 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -18,7 +18,7 @@ class Frontend { - constructor(popup) { + constructor(popup, ignoreNodes) { this.popup = popup; this.popupTimer = null; this.mouseDownLeft = false; @@ -26,6 +26,7 @@ class Frontend { this.textSourceLast = null; this.pendingLookup = false; this.options = null; + this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null); this.primaryTouchIdentifier = null; this.contextMenuChecking = false; @@ -39,10 +40,10 @@ class Frontend { static create() { const initializationData = window.frontendInitializationData; const isNested = (initializationData !== null && typeof initializationData === 'object'); - const {id, parentFrameId} = initializationData || {}; + const {id, parentFrameId, ignoreNodes} = isNested ? initializationData : {}; const popup = isNested ? new PopupProxy(id, parentFrameId) : PopupProxyHost.instance.createPopup(null); - const frontend = new Frontend(popup); + const frontend = new Frontend(popup, ignoreNodes); frontend.prepare(); return frontend; } @@ -337,9 +338,14 @@ class Frontend { } async searchTerms(textSource, focus) { - textSource.setEndOffset(this.options.scanning.length); + this.setTextSourceScanLength(textSource, this.options.scanning.length); - const {definitions, length} = await apiTermsFind(textSource.text()); + const searchText = textSource.text(); + if (searchText.length === 0) { + return; + } + + const {definitions, length} = await apiTermsFind(searchText); if (definitions.length === 0) { return false; } @@ -365,9 +371,14 @@ class Frontend { } async searchKanji(textSource, focus) { - textSource.setEndOffset(1); + this.setTextSourceScanLength(textSource, 1); - const definitions = await apiKanjiFind(textSource.text()); + const searchText = textSource.text(); + if (searchText.length === 0) { + return; + } + + const definitions = await apiKanjiFind(searchText); if (definitions.length === 0) { return false; } @@ -493,6 +504,67 @@ class Frontend { } return false; } + + setTextSourceScanLength(textSource, length) { + textSource.setEndOffset(length); + if (this.ignoreNodes === null || !textSource.range) { + return; + } + + length = textSource.text().length; + while (textSource.range && length > 0) { + const nodes = Frontend.getNodesInRange(textSource.range); + if (Frontend.isValidScanningNodeList(nodes, this.ignoreNodes)) { + break; + } + --length; + textSource.setEndOffset(length); + } + } + + static getNodesInRange(range) { + const end = range.endContainer; + const nodes = []; + for (let node = range.startContainer; node !== null; node = Frontend.getNextNode(node)) { + nodes.push(node); + if (node === end) { break; } + } + return nodes; + } + + static getNextNode(node) { + let next = node.firstChild; + if (next === null) { + while (true) { + next = node.nextSibling; + if (next !== null) { break; } + + next = node.parentNode; + if (node === null) { break; } + + node = next; + } + } + return next; + } + + static isValidScanningNodeList(nodeList, selector) { + for (const node of nodeList) { + if (!Frontend.isValidScanningNode(node, selector)) { + return false; + } + } + return true; + } + + static isValidScanningNode(node, selector) { + for (; node !== null; node = node.parentNode) { + if (node.nodeType === Node.ELEMENT_NODE) { + return !node.matches(selector); + } + } + return true; + } } window.yomichan_frontend = Frontend.create(); diff --git a/ext/fg/js/popup-nested.js b/ext/fg/js/popup-nested.js index ad235cc6..e0376bb2 100644 --- a/ext/fg/js/popup-nested.js +++ b/ext/fg/js/popup-nested.js @@ -32,7 +32,9 @@ async function popupNestedInitialize(id, depth, parentFrameId) { return; } - window.frontendInitializationData = {id, depth, parentFrameId}; + const ignoreNodes = options.scanning.enableOnPopupExpressions ? [] : [ '.expression', '.expression *' ]; + + window.frontendInitializationData = {id, depth, parentFrameId, ignoreNodes}; const scriptSrcs = [ '/fg/js/frontend-api-sender.js', -- cgit v1.2.3 From 87ff5cb19bc78244faafcfcb54d8428b2ed978f6 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Wed, 28 Aug 2019 01:08:45 +0300 Subject: make containsPoint async everywhere --- ext/fg/js/frontend.js | 2 +- ext/fg/js/popup-proxy-host.js | 2 +- ext/fg/js/popup-proxy.js | 10 +--------- ext/fg/js/popup.js | 2 +- 4 files changed, 4 insertions(+), 12 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index f59ea318..1dff6fb9 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -294,7 +294,7 @@ class Frontend { async searchAt(point, type) { if ( this.pendingLookup || - (this.popup.containsPointIsAsync() ? await this.popup.containsPointAsync(point) : this.popup.containsPoint(point)) + await this.popup.containsPoint(point) ) { return; } diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index cdd1d02c..1048f410 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -110,7 +110,7 @@ class PopupProxyHost { async containsPoint(id, point) { const popup = this.getPopup(id); - return popup.containsPointIsAsync() ? await popup.containsPointAsync(point) : popup.containsPoint(point); + return await popup.containsPoint(point); } async termsShow(id, elementRect, definitions, options, context) { diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index bbf6a2cf..112b998e 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -69,21 +69,13 @@ class PopupProxy { return await this.invokeHostApi('setVisible', {id, visible}); } - containsPoint() { - throw 'Non-async function not supported'; - } - - async containsPointAsync(point) { + async containsPoint(point) { if (this.id === null) { return false; } return await this.invokeHostApi('containsPoint', {id: this.id, point}); } - containsPointIsAsync() { - return true; - } - async termsShow(elementRect, definitions, options, context) { const id = await this.getPopupId(); elementRect = PopupProxy.DOMRectToJson(elementRect); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index ab1dbbed..6b757472 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -248,7 +248,7 @@ class Popup { } } - containsPoint(point) { + async containsPoint(point) { if (!this.isVisible()) { return false; } -- cgit v1.2.3 From 3d28a3a405273d8416ddc8976aaaff0c90ac057d Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 31 Aug 2019 16:35:25 +0300 Subject: prevent closing new child popup on parent mouseout --- ext/fg/js/frontend.js | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 1dff6fb9..a31bdf79 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -56,6 +56,7 @@ class Frontend { window.addEventListener('mousedown', this.onMouseDown.bind(this)); window.addEventListener('mousemove', this.onMouseMove.bind(this)); window.addEventListener('mouseover', this.onMouseOver.bind(this)); + window.addEventListener('mouseout', this.onMouseOut.bind(this)); window.addEventListener('mouseup', this.onMouseUp.bind(this)); window.addEventListener('resize', this.onResize.bind(this)); @@ -149,6 +150,10 @@ class Frontend { } } + onMouseOut(e) { + this.popupTimerClear(); + } + onFrameMessage(e) { const handlers = { popupClose: () => { -- cgit v1.2.3 From 834f14e808526da958b26accd0d18056a7f4415c Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 31 Aug 2019 16:37:17 +0300 Subject: simplified if statement back to single line --- ext/fg/js/frontend.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index a31bdf79..eea77d2a 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -297,10 +297,7 @@ class Frontend { } async searchAt(point, type) { - if ( - this.pendingLookup || - await this.popup.containsPoint(point) - ) { + if (this.pendingLookup || await this.popup.containsPoint(point)) { return; } -- cgit v1.2.3 From fad53324885c971a1ab1aeee3da5dd60dc5058df Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 1 Sep 2019 10:28:50 -0400 Subject: Move static DOM scanning functions into TextSourceRange --- ext/fg/js/frontend.js | 48 ++---------------------------------------------- ext/fg/js/source.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 46 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index eea77d2a..b70bf036 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -515,58 +515,14 @@ class Frontend { length = textSource.text().length; while (textSource.range && length > 0) { - const nodes = Frontend.getNodesInRange(textSource.range); - if (Frontend.isValidScanningNodeList(nodes, this.ignoreNodes)) { + const nodes = TextSourceRange.getNodesInRange(textSource.range); + if (!TextSourceRange.anyNodeMatchesSelector(nodes, this.ignoreNodes)) { break; } --length; textSource.setEndOffset(length); } } - - static getNodesInRange(range) { - const end = range.endContainer; - const nodes = []; - for (let node = range.startContainer; node !== null; node = Frontend.getNextNode(node)) { - nodes.push(node); - if (node === end) { break; } - } - return nodes; - } - - static getNextNode(node) { - let next = node.firstChild; - if (next === null) { - while (true) { - next = node.nextSibling; - if (next !== null) { break; } - - next = node.parentNode; - if (node === null) { break; } - - node = next; - } - } - return next; - } - - static isValidScanningNodeList(nodeList, selector) { - for (const node of nodeList) { - if (!Frontend.isValidScanningNode(node, selector)) { - return false; - } - } - return true; - } - - static isValidScanningNode(node, selector) { - for (; node !== null; node = node.parentNode) { - if (node.nodeType === Node.ELEMENT_NODE) { - return !node.matches(selector); - } - } - return true; - } } window.yomichan_frontend = Frontend.create(); diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index e724488d..385b5001 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -232,6 +232,50 @@ class TextSourceRange { const writingMode = style.writingMode; return typeof writingMode === 'string' ? writingMode : 'horizontal-tb'; } + + static getNodesInRange(range) { + const end = range.endContainer; + const nodes = []; + for (let node = range.startContainer; node !== null; node = TextSourceRange.getNextNode(node)) { + nodes.push(node); + if (node === end) { break; } + } + return nodes; + } + + static getNextNode(node) { + let next = node.firstChild; + if (next === null) { + while (true) { + next = node.nextSibling; + if (next !== null) { break; } + + next = node.parentNode; + if (node === null) { break; } + + node = next; + } + } + return next; + } + + static anyNodeMatchesSelector(nodeList, selector) { + for (const node of nodeList) { + if (TextSourceRange.nodeMatchesSelector(node, selector)) { + return true; + } + } + return false; + } + + static nodeMatchesSelector(node, selector) { + for (; node !== null; node = node.parentNode) { + if (node.nodeType === Node.ELEMENT_NODE) { + return node.matches(selector); + } + } + return false; + } } -- cgit v1.2.3 From 5c793180d04e6b5eafaf01c823f5f5e07639cfa9 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 3 Sep 2019 19:15:34 -0400 Subject: Omit containsPoint check on searchAt The range returned by docRangeFromPoint is now guaranteed to contain the point due to the addition of isPointInRange checks. --- ext/fg/js/frontend.js | 2 +- ext/fg/js/source.js | 22 ---------------------- 2 files changed, 1 insertion(+), 23 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 8a5c48d0..39403bbd 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -286,7 +286,7 @@ class Frontend { } const textSource = docRangeFromPoint(point, this.options); - let hideResults = !textSource || !textSource.containsPoint(point); + let hideResults = textSource === null; let searched = false; let success = false; diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index e724488d..1febab7e 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -59,11 +59,6 @@ class TextSourceRange { return length - state.remainder; } - containsPoint(point) { - const rect = this.getPaddedRect(); - return point.x >= rect.left && point.x <= rect.right; - } - getRect() { return this.range.getBoundingClientRect(); } @@ -72,18 +67,6 @@ class TextSourceRange { return TextSourceRange.getElementWritingMode(TextSourceRange.getParentElement(this.range.startContainer)); } - getPaddedRect() { - const range = this.range.cloneRange(); - const startOffset = range.startOffset; - const endOffset = range.endOffset; - const node = range.startContainer; - - range.setStart(node, Math.max(0, startOffset - 1)); - range.setEnd(node, Math.min(node.length, endOffset + 1)); - - return range.getBoundingClientRect(); - } - select() { const selection = window.getSelection(); selection.removeAllRanges(); @@ -290,11 +273,6 @@ class TextSourceElement { return 0; } - containsPoint(point) { - const rect = this.getRect(); - return point.x >= rect.left && point.x <= rect.right; - } - getRect() { return this.element.getBoundingClientRect(); } -- cgit v1.2.3