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/bg/js/backend-api-forwarder.js | 45 ++++++++++++++++++++++++++++++++++++++ ext/bg/js/backend.js | 2 ++ 2 files changed, 47 insertions(+) create mode 100644 ext/bg/js/backend-api-forwarder.js (limited to 'ext/bg/js') diff --git a/ext/bg/js/backend-api-forwarder.js b/ext/bg/js/backend-api-forwarder.js new file mode 100644 index 00000000..979afd16 --- /dev/null +++ b/ext/bg/js/backend-api-forwarder.js @@ -0,0 +1,45 @@ +/* + * 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 . + */ + + +class BackendApiForwarder { + constructor() { + chrome.runtime.onConnect.addListener(this.onConnect.bind(this)); + } + + onConnect(port) { + if (port.name !== 'backend-api-forwarder') { return; } + + let tabId; + if (!( + port.sender && + port.sender.tab && + (typeof (tabId = port.sender.tab.id)) === 'number' + )) { + port.disconnect(); + return; + } + + const forwardPort = chrome.tabs.connect(tabId, {name: 'frontend-api-receiver'}); + + port.onMessage.addListener(message => forwardPort.postMessage(message)); + forwardPort.onMessage.addListener(message => port.postMessage(message)); + port.onDisconnect.addListener(() => forwardPort.disconnect()); + forwardPort.onDisconnect.addListener(() => port.disconnect()); + } +} diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index d95cb82d..8f99c13a 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -22,6 +22,8 @@ class Backend { this.translator = new Translator(); this.anki = new AnkiNull(); this.options = null; + + this.apiForwarder = new BackendApiForwarder(); } async prepare() { -- cgit v1.2.3 From 1a9348ec27b903af47511da11306f884a82cf353 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 17 Aug 2019 19:32:58 -0400 Subject: Add option for maximum nested popup depth --- ext/bg/js/options.js | 3 ++- ext/bg/js/settings.js | 2 ++ ext/bg/settings.html | 5 +++++ ext/fg/float.html | 6 +----- ext/fg/js/popup-nested.js | 48 +++++++++++++++++++++++++++++++++++++++++++ ext/fg/js/popup-proxy-host.js | 3 ++- ext/fg/js/popup.js | 5 +++-- 7 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 ext/fg/js/popup-nested.js (limited to 'ext/bg/js') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 7d993987..2197c72c 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -219,7 +219,8 @@ function optionsSetDefaults(options) { delay: 20, length: 10, modifier: 'shift', - deepDomScan: false + deepDomScan: false, + popupNestingMaxDepth: 0 }, dictionaries: {}, diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index f5d669b2..de1616f3 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -51,6 +51,7 @@ async function formRead() { optionsNew.scanning.delay = parseInt($('#scan-delay').val(), 10); optionsNew.scanning.length = parseInt($('#scan-length').val(), 10); optionsNew.scanning.modifier = $('#scan-modifier-key').val(); + optionsNew.scanning.popupNestingMaxDepth = parseInt($('#popup-nesting-max-depth').val(), 10); optionsNew.anki.enable = $('#anki-enable').prop('checked'); optionsNew.anki.tags = $('#card-tags').val().split(/[,; ]+/); @@ -192,6 +193,7 @@ async function onReady() { $('#scan-delay').val(options.scanning.delay); $('#scan-length').val(options.scanning.length); $('#scan-modifier-key').val(options.scanning.modifier); + $('#popup-nesting-max-depth').val(options.scanning.popupNestingMaxDepth); $('#dict-purge-link').click(utilAsync(onDictionaryPurge)); $('#dict-file').change(utilAsync(onDictionaryImport)); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index cc140023..a0d08ec5 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -215,6 +215,11 @@ + +
+ + +
diff --git a/ext/fg/float.html b/ext/fg/float.html index bd08296a..465db589 100644 --- a/ext/fg/float.html +++ b/ext/fg/float.html @@ -44,10 +44,6 @@ - - - - - + diff --git a/ext/fg/js/popup-nested.js b/ext/fg/js/popup-nested.js new file mode 100644 index 00000000..7df4e4e2 --- /dev/null +++ b/ext/fg/js/popup-nested.js @@ -0,0 +1,48 @@ +/* + * 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 . + */ + + +async function popupNestedSetup() { + const options = await apiOptionsGet(); + const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth; + + let depth = null; + const match = /[&?]depth=([^&]*?)(?:&|$)/.exec(location.href); + if (match !== null) { + depth = parseInt(match[1], 10); + } + + if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) { + return; + } + + const scriptSrcs = [ + '/fg/js/frontend-api-sender.js', + '/fg/js/popup.js', + '/fg/js/popup-proxy.js', + '/fg/js/frontend.js' + ]; + for (const src of scriptSrcs) { + const script = document.createElement('script'); + script.async = false; + script.src = src; + document.body.appendChild(script); + } +} + +popupNestedSetup(); diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index 189481bc..ba3db832 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -36,9 +36,10 @@ class PopupProxyHost { createPopup(parentId) { const parent = (typeof parentId === 'string' && this.popups.hasOwnProperty(parentId) ? this.popups[parentId] : null); + const depth = (parent !== null ? parent.depth + 1 : 0); const id = `${this.nextId}`; ++this.nextId; - const popup = new Popup(id); + const popup = new Popup(id, depth); if (parent !== null) { popup.parent = parent; parent.children.push(popup); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index f6b4f6d9..00dfeb89 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -18,15 +18,16 @@ class Popup { - constructor(id) { + constructor(id, depth) { this.id = id; + this.depth = depth; 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}`)); + 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; -- cgit v1.2.3 From 42ec3e2a43dfd9ac0748ca7c364cef2b44f625a2 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 18 Aug 2019 16:48:18 -0400 Subject: Add support for popup on the search page --- ext/bg/js/options.js | 3 ++- ext/bg/js/search-frontend.js | 51 ++++++++++++++++++++++++++++++++++++++++++++ ext/bg/js/settings.js | 2 ++ ext/bg/search.html | 1 + ext/bg/settings.html | 4 ++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 ext/bg/js/search-frontend.js (limited to 'ext/bg/js') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 2197c72c..1b2d5e1a 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -220,7 +220,8 @@ function optionsSetDefaults(options) { length: 10, modifier: 'shift', deepDomScan: false, - popupNestingMaxDepth: 0 + popupNestingMaxDepth: 0, + enableOnSearchPage: true }, dictionaries: {}, diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-frontend.js new file mode 100644 index 00000000..77aa4052 --- /dev/null +++ b/ext/bg/js/search-frontend.js @@ -0,0 +1,51 @@ +/* + * 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 . + */ + + +async function searchFrontendSetup() { + const options = await apiOptionsGet(); + if (!options.scanning.enableOnSearchPage) { return; } + + const scriptSrcs = [ + '/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/frontend.js' + ]; + for (const src of scriptSrcs) { + const script = document.createElement('script'); + script.async = false; + script.src = src; + document.body.appendChild(script); + } + + const styleSrcs = [ + '/fg/css/client.css' + ]; + for (const src of styleSrcs) { + const style = document.createElement('link'); + style.rel = 'stylesheet'; + style.type = 'text/css'; + style.href = src; + document.head.appendChild(style); + } +} + +searchFrontendSetup(); diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index de1616f3..5472c7bd 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.enableOnSearchPage = $('#enable-scanning-on-search-page').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(); @@ -190,6 +191,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-on-search-page').prop('checked', options.scanning.enableOnSearchPage); $('#scan-delay').val(options.scanning.delay); $('#scan-length').val(options.scanning.length); $('#scan-modifier-key').val(options.scanning.modifier); diff --git a/ext/bg/search.html b/ext/bg/search.html index 05c0daab..38c5a4e9 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -51,5 +51,6 @@ + diff --git a/ext/bg/settings.html b/ext/bg/settings.html index a0d08ec5..0005efa6 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -192,6 +192,10 @@
+
+ +
+
-- 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/bg/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 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/bg/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