From 8c9389e07d21c3f71184f453c0db11ca7cfb8121 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 10 Apr 2020 01:42:02 +0300 Subject: [PATCH 01/17] listen to optionsUpdated in search-frontend.js --- ext/bg/js/search-frontend.js | 37 +++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-frontend.js index 9cc1436f..48326caf 100644 --- a/ext/bg/js/search-frontend.js +++ b/ext/bg/js/search-frontend.js @@ -19,18 +19,7 @@ * apiOptionsGet */ -async function searchFrontendSetup() { - await yomichan.prepare(); - - const optionsContext = { - depth: 0, - url: window.location.href - }; - const options = await apiOptionsGet(optionsContext); - if (!options.scanning.enableOnSearchPage) { return; } - - window.frontendInitializationData = {depth: 1, proxy: false}; - +function injectSearchFrontend() { const scriptSrcs = [ '/mixed/js/text-scanner.js', '/fg/js/frontend-api-receiver.js', @@ -62,4 +51,26 @@ async function searchFrontendSetup() { } } -searchFrontendSetup(); +async function main() { + await yomichan.prepare(); + + const applyOptions = async () => { + const optionsContext = { + depth: 0, + url: window.location.href + }; + const options = await apiOptionsGet(optionsContext); + if (!options.scanning.enableOnSearchPage) { return; } + + window.frontendInitializationData = {depth: 1, proxy: false}; + injectSearchFrontend(); + + yomichan.off('optionsUpdated', applyOptions); + }; + + yomichan.on('optionsUpdated', applyOptions); + + await applyOptions(); +} + +main(); From 9adbc80a70ea263ae97a853c3d9bb0b6109503bd Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 10 Apr 2020 02:05:34 +0300 Subject: [PATCH 02/17] listen to optionsUpdated in popup-nested.js --- ext/fg/js/popup-nested.js | 47 ++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/ext/fg/js/popup-nested.js b/ext/fg/js/popup-nested.js index 1b24614b..f193aa36 100644 --- a/ext/fg/js/popup-nested.js +++ b/ext/fg/js/popup-nested.js @@ -19,24 +19,7 @@ * apiOptionsGet */ -let popupNestedInitialized = false; - -async function popupNestedInitialize(id, depth, parentFrameId, url) { - if (popupNestedInitialized) { - return; - } - popupNestedInitialized = true; - - const optionsContext = {depth, url}; - const options = await apiOptionsGet(optionsContext); - const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth; - - if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) { - return; - } - - window.frontendInitializationData = {id, depth, parentFrameId, url, proxy: true}; - +function injectPopupNested() { const scriptSrcs = [ '/mixed/js/text-scanner.js', '/fg/js/frontend-api-sender.js', @@ -52,3 +35,31 @@ async function popupNestedInitialize(id, depth, parentFrameId, url) { document.body.appendChild(script); } } + +let popupNestedInitialized = false; + +async function popupNestedInitialize(id, depth, parentFrameId, url) { + if (popupNestedInitialized) { + return; + } + popupNestedInitialized = true; + + const applyOptions = async () => { + const optionsContext = {depth, url}; + const options = await apiOptionsGet(optionsContext); + const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth; + + if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) { + return; + } + + window.frontendInitializationData = {id, depth, parentFrameId, url, proxy: true}; + injectPopupNested(); + + yomichan.off('optionsUpdated', applyOptions); + }; + + yomichan.on('optionsUpdated', applyOptions); + + await applyOptions(); +} From 61a96e327a815bda7fea4c5d2096dead901fdf33 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 10 Apr 2020 02:15:24 +0300 Subject: [PATCH 03/17] prevent injecting frontend multiple times --- ext/bg/js/search-frontend.js | 5 ++++- ext/fg/js/popup-nested.js | 11 ++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-frontend.js index 48326caf..18cb6060 100644 --- a/ext/bg/js/search-frontend.js +++ b/ext/bg/js/search-frontend.js @@ -54,13 +54,16 @@ function injectSearchFrontend() { async function main() { await yomichan.prepare(); + let optionsApplied = false; + const applyOptions = async () => { const optionsContext = { depth: 0, url: window.location.href }; const options = await apiOptionsGet(optionsContext); - if (!options.scanning.enableOnSearchPage) { return; } + if (!options.scanning.enableOnSearchPage || optionsApplied) { return; } + optionsApplied = true; window.frontendInitializationData = {depth: 1, proxy: false}; injectSearchFrontend(); diff --git a/ext/fg/js/popup-nested.js b/ext/fg/js/popup-nested.js index f193aa36..3ecdf50c 100644 --- a/ext/fg/js/popup-nested.js +++ b/ext/fg/js/popup-nested.js @@ -44,15 +44,24 @@ async function popupNestedInitialize(id, depth, parentFrameId, url) { } popupNestedInitialized = true; + let optionsApplied = false; + const applyOptions = async () => { const optionsContext = {depth, url}; const options = await apiOptionsGet(optionsContext); const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth; - if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) { + const maxPopupDepthExceeded = !( + typeof popupNestingMaxDepth === 'number' && + typeof depth === 'number' && + depth < popupNestingMaxDepth + ); + if (maxPopupDepthExceeded || optionsApplied) { return; } + optionsApplied = true; + window.frontendInitializationData = {id, depth, parentFrameId, url, proxy: true}; injectPopupNested(); From 1df59d57b5dcbf9d3efedcb21a4d5e16524a67c1 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 10 Apr 2020 02:48:41 +0300 Subject: [PATCH 04/17] limit popup depth live --- ext/fg/js/frontend.js | 3 ++- ext/mixed/js/text-scanner.js | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 55d699e5..a6b24c76 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -30,7 +30,8 @@ class Frontend extends TextScanner { super( window, popup.isProxy() ? [] : [popup.getContainer()], - [(x, y) => this.popup.containsPoint(x, y)] + [(x, y) => this.popup.containsPoint(x, y)], + () => this.popup.depth <= this.options.scanning.popupNestingMaxDepth ); this.popup = popup; diff --git a/ext/mixed/js/text-scanner.js b/ext/mixed/js/text-scanner.js index a1d96320..1d6e36e0 100644 --- a/ext/mixed/js/text-scanner.js +++ b/ext/mixed/js/text-scanner.js @@ -22,11 +22,13 @@ */ class TextScanner { - constructor(node, ignoreElements, ignorePoints) { + constructor(node, ignoreElements, ignorePoints, canEnable=null) { this.node = node; this.ignoreElements = ignoreElements; this.ignorePoints = ignorePoints; + this.canEnable = canEnable; + this.ignoreNodes = null; this.scanTimerPromise = null; @@ -225,6 +227,10 @@ class TextScanner { } setEnabled(enabled) { + if (this.canEnable !== null && !this.canEnable()) { + enabled = false; + } + if (enabled) { if (!this.enabled) { this.hookEvents(); From 92109bb5d25db99033b0bb9f7f3806883f79218d Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 10 Apr 2020 03:55:25 +0300 Subject: [PATCH 05/17] allow disabling scan on search page live --- ext/bg/js/search-frontend.js | 2 +- ext/fg/js/frontend-initialize.js | 15 +++++++++++++-- ext/fg/js/frontend.js | 18 ++++++++++++++++-- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-frontend.js index 18cb6060..e534e771 100644 --- a/ext/bg/js/search-frontend.js +++ b/ext/bg/js/search-frontend.js @@ -65,7 +65,7 @@ async function main() { if (!options.scanning.enableOnSearchPage || optionsApplied) { return; } optionsApplied = true; - window.frontendInitializationData = {depth: 1, proxy: false}; + window.frontendInitializationData = {depth: 1, proxy: false, isSearchPage: true}; injectSearchFrontend(); yomichan.off('optionsUpdated', applyOptions); diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js index 5af7fdf0..3fe5ac5b 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/frontend-initialize.js @@ -28,7 +28,18 @@ async function main() { await yomichan.prepare(); const data = window.frontendInitializationData || {}; - const {id, depth=0, parentFrameId, url, proxy=false} = data; + const {id, depth=0, parentFrameId, url, proxy=false, isSearchPage=false} = data; + + const initEventDispatcher = new EventDispatcher(); + + yomichan.on('optionsUpdated', async () => { + const optionsContext = {depth: isSearchPage ? 0 : depth, url}; + const options = await apiOptionsGet(optionsContext); + if (isSearchPage) { + const disabled = !options.scanning.enableOnSearchPage; + initEventDispatcher.trigger('setDisabledOverride', {disabled}); + } + }); const optionsContext = {depth, url}; const options = await apiOptionsGet(optionsContext); @@ -65,7 +76,7 @@ async function main() { popup = popupHost.getOrCreatePopup(null, null, depth); } - const frontend = new Frontend(popup); + const frontend = new Frontend(popup, initEventDispatcher); await frontend.prepare(); } diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index a6b24c76..a6df4b4c 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -26,15 +26,19 @@ */ class Frontend extends TextScanner { - constructor(popup) { + constructor(popup, initEventDispatcher) { super( window, popup.isProxy() ? [] : [popup.getContainer()], [(x, y) => this.popup.containsPoint(x, y)], - () => this.popup.depth <= this.options.scanning.popupNestingMaxDepth + () => this.popup.depth <= this.options.scanning.popupNestingMaxDepth && !this._disabledOverride ); this.popup = popup; + this.initEventDispatcher = initEventDispatcher; + + this._disabledOverride = false; + this.options = null; this.optionsContext = { @@ -73,6 +77,8 @@ class Frontend extends TextScanner { window.visualViewport.addEventListener('resize', this.onVisualViewportResize.bind(this)); } + this.initEventDispatcher.on('setDisabledOverride', this.onSetDisabledOverride.bind(this)); + yomichan.on('orphaned', this.onOrphaned.bind(this)); yomichan.on('optionsUpdated', this.updateOptions.bind(this)); yomichan.on('zoomChanged', this.onZoomChanged.bind(this)); @@ -228,6 +234,14 @@ class Frontend extends TextScanner { super.onSearchClear(changeFocus); } + onSetDisabledOverride({disabled}) { + this._disabledOverride = disabled; + // other cases handed by regular options update + if (disabled && this.enabled) { + this.setEnabled(false); + } + } + getOptionsContext() { this.optionsContext.url = this.popup.url; return this.optionsContext; From 7dd2610ce844986feed72720378d7da601c44d27 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 10 Apr 2020 15:00:23 +0300 Subject: [PATCH 06/17] extract different popup creation functions --- ext/fg/js/frontend-initialize.js | 70 ++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js index 3fe5ac5b..db581b93 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/frontend-initialize.js @@ -24,6 +24,47 @@ * apiOptionsGet */ +async function createIframePopupProxy(url) { + const rootPopupInformationPromise = yomichan.getTemporaryListenerResult( + chrome.runtime.onMessage, + ({action, params}, {resolve}) => { + if (action === 'rootPopupInformation') { + resolve(params); + } + } + ); + apiBroadcastTab('rootPopupRequestInformationBroadcast'); + const {popupId, frameId} = await rootPopupInformationPromise; + + const frameOffsetForwarder = new FrameOffsetForwarder(); + frameOffsetForwarder.start(); + const getFrameOffset = frameOffsetForwarder.getOffset.bind(frameOffsetForwarder); + + const popup = new PopupProxy(popupId, 0, null, frameId, url, getFrameOffset); + await popup.prepare(); + + return popup; +} + +async function getOrCreatePopup(depth) { + const frameOffsetForwarder = new FrameOffsetForwarder(); + frameOffsetForwarder.start(); + + const popupHost = new PopupProxyHost(); + await popupHost.prepare(); + + const popup = popupHost.getOrCreatePopup(null, null, depth); + + return popup; +} + +async function createPopupProxy(depth, id, parentFrameId, url) { + const popup = new PopupProxy(null, depth + 1, id, parentFrameId, url); + await popup.prepare(); + + return popup; +} + async function main() { await yomichan.prepare(); @@ -46,34 +87,11 @@ async function main() { let popup; if (!proxy && (window !== window.parent) && options.general.showIframePopupsInRootFrame) { - const rootPopupInformationPromise = yomichan.getTemporaryListenerResult( - chrome.runtime.onMessage, - ({action, params}, {resolve}) => { - if (action === 'rootPopupInformation') { - resolve(params); - } - } - ); - apiBroadcastTab('rootPopupRequestInformationBroadcast'); - const {popupId, frameId} = await rootPopupInformationPromise; - - const frameOffsetForwarder = new FrameOffsetForwarder(); - frameOffsetForwarder.start(); - const getFrameOffset = frameOffsetForwarder.getOffset.bind(frameOffsetForwarder); - - popup = new PopupProxy(popupId, 0, null, frameId, url, getFrameOffset); - await popup.prepare(); + popup = await createIframePopupProxy(url); } else if (proxy) { - popup = new PopupProxy(null, depth + 1, id, parentFrameId, url); - await popup.prepare(); + popup = await createPopupProxy(depth, id, parentFrameId, url); } else { - const frameOffsetForwarder = new FrameOffsetForwarder(); - frameOffsetForwarder.start(); - - const popupHost = new PopupProxyHost(); - await popupHost.prepare(); - - popup = popupHost.getOrCreatePopup(null, null, depth); + popup = await getOrCreatePopup(depth); } const frontend = new Frontend(popup, initEventDispatcher); From b9035854b2b45979c16f8733bb66993f6ce044fe Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 10 Apr 2020 15:21:13 +0300 Subject: [PATCH 07/17] cache created popups --- ext/fg/js/frontend-initialize.js | 44 ++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js index db581b93..2cfb6176 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/frontend-initialize.js @@ -71,31 +71,47 @@ async function main() { const data = window.frontendInitializationData || {}; const {id, depth=0, parentFrameId, url, proxy=false, isSearchPage=false} = data; + const isIframe = !proxy && (window !== window.parent); + const initEventDispatcher = new EventDispatcher(); - yomichan.on('optionsUpdated', async () => { + const popups = { + iframe: null, + proxy: null, + normal: null + }; + + let frontend = null; + + const applyOptions = async () => { const optionsContext = {depth: isSearchPage ? 0 : depth, url}; const options = await apiOptionsGet(optionsContext); if (isSearchPage) { const disabled = !options.scanning.enableOnSearchPage; initEventDispatcher.trigger('setDisabledOverride', {disabled}); } - }); - const optionsContext = {depth, url}; - const options = await apiOptionsGet(optionsContext); + let popup; + if (isIframe && options.general.showIframePopupsInRootFrame) { + popup = popups.iframe || await createIframePopupProxy(url); + popups.iframe = popup; + } else if (proxy) { + popup = popups.proxy || await createPopupProxy(depth, id, parentFrameId, url); + popups.proxy = popup; + } else { + popup = popups.normal || await getOrCreatePopup(depth); + popups.normal = popup; + } - let popup; - if (!proxy && (window !== window.parent) && options.general.showIframePopupsInRootFrame) { - popup = await createIframePopupProxy(url); - } else if (proxy) { - popup = await createPopupProxy(depth, id, parentFrameId, url); - } else { - popup = await getOrCreatePopup(depth); - } + if (frontend === null) { + frontend = new Frontend(popup, initEventDispatcher); + await frontend.prepare(); + } + }; - const frontend = new Frontend(popup, initEventDispatcher); - await frontend.prepare(); + yomichan.on('optionsUpdated', applyOptions); + + await applyOptions(); } main(); From 565e3a8c6ad31afa0ddb171ac5a33b6b84e5c3dc Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 10 Apr 2020 15:49:56 +0300 Subject: [PATCH 08/17] change iframe popup live --- ext/fg/js/frontend-initialize.js | 13 +++++++++---- ext/fg/js/frontend.js | 11 +++++++++-- ext/mixed/js/text-scanner.js | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js index 2cfb6176..ce1a6bf6 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/frontend-initialize.js @@ -86,10 +86,6 @@ async function main() { const applyOptions = async () => { const optionsContext = {depth: isSearchPage ? 0 : depth, url}; const options = await apiOptionsGet(optionsContext); - if (isSearchPage) { - const disabled = !options.scanning.enableOnSearchPage; - initEventDispatcher.trigger('setDisabledOverride', {disabled}); - } let popup; if (isIframe && options.general.showIframePopupsInRootFrame) { @@ -103,6 +99,15 @@ async function main() { popups.normal = popup; } + if (isSearchPage) { + const disabled = !options.scanning.enableOnSearchPage; + initEventDispatcher.trigger('setDisabledOverride', {disabled}); + } + + if (isIframe) { + initEventDispatcher.trigger('popupChange', {popup}); + } + if (frontend === null) { frontend = new Frontend(popup, initEventDispatcher); await frontend.prepare(); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index a6df4b4c..f969df32 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -29,7 +29,7 @@ class Frontend extends TextScanner { constructor(popup, initEventDispatcher) { super( window, - popup.isProxy() ? [] : [popup.getContainer()], + () => this.popup.isProxy() ? [] : [this.popup.getContainer()], [(x, y) => this.popup.containsPoint(x, y)], () => this.popup.depth <= this.options.scanning.popupNestingMaxDepth && !this._disabledOverride ); @@ -78,6 +78,7 @@ class Frontend extends TextScanner { } this.initEventDispatcher.on('setDisabledOverride', this.onSetDisabledOverride.bind(this)); + this.initEventDispatcher.on('popupChange', this.onPopupChange.bind(this)); yomichan.on('orphaned', this.onOrphaned.bind(this)); yomichan.on('optionsUpdated', this.updateOptions.bind(this)); @@ -242,6 +243,12 @@ class Frontend extends TextScanner { } } + async onPopupChange({popup}) { + this.onSearchClear(true); + this.popup = popup; + await popup.setOptions(this.options); + } + getOptionsContext() { this.optionsContext.url = this.popup.url; return this.optionsContext; @@ -274,7 +281,7 @@ class Frontend extends TextScanner { } _broadcastRootPopupInformation() { - if (!this.popup.isProxy() && this.popup.depth === 0) { + if (!this.popup.isProxy() && this.popup.depth === 0 && this.popup.frameId === 0) { apiBroadcastTab('rootPopupInformation', {popupId: this.popup.id, frameId: this.popup.frameId}); } } diff --git a/ext/mixed/js/text-scanner.js b/ext/mixed/js/text-scanner.js index 1d6e36e0..85be7119 100644 --- a/ext/mixed/js/text-scanner.js +++ b/ext/mixed/js/text-scanner.js @@ -48,7 +48,7 @@ class TextScanner { } onMouseOver(e) { - if (this.ignoreElements.includes(e.target)) { + if (this.ignoreElements().includes(e.target)) { this.scanTimerClear(); } } From 275f455e73c48294aeefd6c02959b1ddd3cbf4e8 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 10 Apr 2020 16:06:22 +0300 Subject: [PATCH 09/17] fix preview frame error --- ext/fg/js/frontend.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index f969df32..cd811115 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -26,7 +26,7 @@ */ class Frontend extends TextScanner { - constructor(popup, initEventDispatcher) { + constructor(popup, initEventDispatcher=null) { super( window, () => this.popup.isProxy() ? [] : [this.popup.getContainer()], @@ -77,8 +77,10 @@ class Frontend extends TextScanner { window.visualViewport.addEventListener('resize', this.onVisualViewportResize.bind(this)); } - this.initEventDispatcher.on('setDisabledOverride', this.onSetDisabledOverride.bind(this)); - this.initEventDispatcher.on('popupChange', this.onPopupChange.bind(this)); + if (this.initEventDispatcher !== null) { + this.initEventDispatcher.on('setDisabledOverride', this.onSetDisabledOverride.bind(this)); + this.initEventDispatcher.on('popupChange', this.onPopupChange.bind(this)); + } yomichan.on('orphaned', this.onOrphaned.bind(this)); yomichan.on('optionsUpdated', this.updateOptions.bind(this)); From d93e3e1a6727e23547c44d4fc3b82244f560c459 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 11 Apr 2020 15:32:04 +0300 Subject: [PATCH 10/17] use setters instead of EventDispatcher --- ext/fg/js/frontend-initialize.js | 22 +++++++++---------- ext/fg/js/frontend.js | 36 +++++++++++++------------------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js index ce1a6bf6..34be6bc6 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/frontend-initialize.js @@ -73,8 +73,6 @@ async function main() { const isIframe = !proxy && (window !== window.parent); - const initEventDispatcher = new EventDispatcher(); - const popups = { iframe: null, proxy: null, @@ -99,18 +97,18 @@ async function main() { popups.normal = popup; } - if (isSearchPage) { - const disabled = !options.scanning.enableOnSearchPage; - initEventDispatcher.trigger('setDisabledOverride', {disabled}); - } - - if (isIframe) { - initEventDispatcher.trigger('popupChange', {popup}); - } - if (frontend === null) { - frontend = new Frontend(popup, initEventDispatcher); + frontend = new Frontend(popup); await frontend.prepare(); + } else { + if (isSearchPage) { + const disabled = !options.scanning.enableOnSearchPage; + frontend.setDisabledOverride(disabled); + } + + if (isIframe) { + await frontend.setPopup(popup); + } } }; diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index cd811115..288d3589 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -26,7 +26,7 @@ */ class Frontend extends TextScanner { - constructor(popup, initEventDispatcher=null) { + constructor(popup) { super( window, () => this.popup.isProxy() ? [] : [this.popup.getContainer()], @@ -35,7 +35,6 @@ class Frontend extends TextScanner { ); this.popup = popup; - this.initEventDispatcher = initEventDispatcher; this._disabledOverride = false; @@ -77,11 +76,6 @@ class Frontend extends TextScanner { window.visualViewport.addEventListener('resize', this.onVisualViewportResize.bind(this)); } - if (this.initEventDispatcher !== null) { - this.initEventDispatcher.on('setDisabledOverride', this.onSetDisabledOverride.bind(this)); - this.initEventDispatcher.on('popupChange', this.onPopupChange.bind(this)); - } - yomichan.on('orphaned', this.onOrphaned.bind(this)); yomichan.on('optionsUpdated', this.updateOptions.bind(this)); yomichan.on('zoomChanged', this.onZoomChanged.bind(this)); @@ -142,6 +136,20 @@ class Frontend extends TextScanner { ]; } + setDisabledOverride(disabled) { + this._disabledOverride = disabled; + // other cases handed by regular options update + if (disabled && this.enabled) { + this.setEnabled(false); + } + } + + async setPopup(popup) { + this.onSearchClear(true); + this.popup = popup; + await popup.setOptions(this.options); + } + async updateOptions() { this.setOptions(await apiOptionsGet(this.getOptionsContext())); @@ -237,20 +245,6 @@ class Frontend extends TextScanner { super.onSearchClear(changeFocus); } - onSetDisabledOverride({disabled}) { - this._disabledOverride = disabled; - // other cases handed by regular options update - if (disabled && this.enabled) { - this.setEnabled(false); - } - } - - async onPopupChange({popup}) { - this.onSearchClear(true); - this.popup = popup; - await popup.setOptions(this.options); - } - getOptionsContext() { this.optionsContext.url = this.popup.url; return this.optionsContext; From 5c3641eadb78144b7f6305ebbd574bbc6265a4c4 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 11 Apr 2020 16:20:12 +0300 Subject: [PATCH 11/17] simplify frontend disable override --- ext/fg/js/frontend-initialize.js | 5 ++++- ext/fg/js/frontend.js | 15 ++++++++------- ext/mixed/js/text-scanner.js | 16 +++++----------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js index 34be6bc6..e794c7c0 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/frontend-initialize.js @@ -80,6 +80,7 @@ async function main() { }; let frontend = null; + let frontendPreparePromise = null; const applyOptions = async () => { const optionsContext = {depth: isSearchPage ? 0 : depth, url}; @@ -99,8 +100,10 @@ async function main() { if (frontend === null) { frontend = new Frontend(popup); - await frontend.prepare(); + frontendPreparePromise = frontend.prepare(); + await frontendPreparePromise; } else { + await frontendPreparePromise; if (isSearchPage) { const disabled = !options.scanning.enableOnSearchPage; frontend.setDisabledOverride(disabled); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 288d3589..20bfc638 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -30,8 +30,7 @@ class Frontend extends TextScanner { super( window, () => this.popup.isProxy() ? [] : [this.popup.getContainer()], - [(x, y) => this.popup.containsPoint(x, y)], - () => this.popup.depth <= this.options.scanning.popupNestingMaxDepth && !this._disabledOverride + [(x, y) => this.popup.containsPoint(x, y)] ); this.popup = popup; @@ -138,10 +137,7 @@ class Frontend extends TextScanner { setDisabledOverride(disabled) { this._disabledOverride = disabled; - // other cases handed by regular options update - if (disabled && this.enabled) { - this.setEnabled(false); - } + this.setEnabled(this.options.general.enable, this._canEnable()); } async setPopup(popup) { @@ -151,7 +147,7 @@ class Frontend extends TextScanner { } async updateOptions() { - this.setOptions(await apiOptionsGet(this.getOptionsContext())); + this.setOptions(await apiOptionsGet(this.getOptionsContext()), this._canEnable()); const ignoreNodes = ['.scan-disable', '.scan-disable *']; if (!this.options.scanning.enableOnPopupExpressions) { @@ -290,6 +286,11 @@ class Frontend extends TextScanner { }); } + _canEnable() { + if (this.options === null) { return true; } // called by updateOptions for the first time + return this.popup.depth <= this.options.scanning.popupNestingMaxDepth && !this._disabledOverride; + } + async _updatePopupPosition() { const textSource = this.getCurrentTextSource(); if (textSource !== null && await this.popup.isVisible()) { diff --git a/ext/mixed/js/text-scanner.js b/ext/mixed/js/text-scanner.js index 85be7119..bff9544f 100644 --- a/ext/mixed/js/text-scanner.js +++ b/ext/mixed/js/text-scanner.js @@ -22,13 +22,11 @@ */ class TextScanner { - constructor(node, ignoreElements, ignorePoints, canEnable=null) { + constructor(node, ignoreElements, ignorePoints) { this.node = node; this.ignoreElements = ignoreElements; this.ignorePoints = ignorePoints; - this.canEnable = canEnable; - this.ignoreNodes = null; this.scanTimerPromise = null; @@ -226,12 +224,8 @@ class TextScanner { } } - setEnabled(enabled) { - if (this.canEnable !== null && !this.canEnable()) { - enabled = false; - } - - if (enabled) { + setEnabled(enabled, canEnable) { + if (enabled && canEnable) { if (!this.enabled) { this.hookEvents(); this.enabled = true; @@ -277,9 +271,9 @@ class TextScanner { ]; } - setOptions(options) { + setOptions(options, canEnable=true) { this.options = options; - this.setEnabled(this.options.general.enable); + this.setEnabled(this.options.general.enable, canEnable); } async searchAt(x, y, cause) { From ba97e72c8bde6d7c5467e60645c637a0836b9a31 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 11 Apr 2020 16:41:03 +0300 Subject: [PATCH 12/17] fix ignoreElements in QueryParser --- ext/bg/js/search-query-parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 01a0ace5..f90c68ee 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -27,7 +27,7 @@ class QueryParser extends TextScanner { constructor({getOptionsContext, setContent, setSpinnerVisible}) { - super(document.querySelector('#query-parser-content'), [], []); + super(document.querySelector('#query-parser-content'), () => [], []); this.getOptionsContext = getOptionsContext; this.setContent = setContent; From e6078ce8f6e5d46c50cd1c63d33177939cbb24a0 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 11 Apr 2020 20:58:50 +0300 Subject: [PATCH 13/17] remove redundant call guard --- ext/fg/js/popup-nested.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/ext/fg/js/popup-nested.js b/ext/fg/js/popup-nested.js index 3ecdf50c..c140f9c8 100644 --- a/ext/fg/js/popup-nested.js +++ b/ext/fg/js/popup-nested.js @@ -36,14 +36,7 @@ function injectPopupNested() { } } -let popupNestedInitialized = false; - async function popupNestedInitialize(id, depth, parentFrameId, url) { - if (popupNestedInitialized) { - return; - } - popupNestedInitialized = true; - let optionsApplied = false; const applyOptions = async () => { From e627ab2537d6aee5323cf03e594be472db1a1881 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 12 Apr 2020 16:42:01 +0300 Subject: [PATCH 14/17] fix optionsContext URL for root popup --- ext/fg/js/frontend-initialize.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js index e794c7c0..2c80c541 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/frontend-initialize.js @@ -69,7 +69,7 @@ async function main() { await yomichan.prepare(); const data = window.frontendInitializationData || {}; - const {id, depth=0, parentFrameId, url, proxy=false, isSearchPage=false} = data; + const {id, depth=0, parentFrameId, url=window.location.href, proxy=false, isSearchPage=false} = data; const isIframe = !proxy && (window !== window.parent); From 362ac84e6c6a0a4c2353232a5c44e1b2157de44d Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 12 Apr 2020 19:17:50 +0300 Subject: [PATCH 15/17] prevent spawning multiple FrameOffsetForwarders --- ext/fg/js/frontend-initialize.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js index 2c80c541..2b942258 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/frontend-initialize.js @@ -24,7 +24,7 @@ * apiOptionsGet */ -async function createIframePopupProxy(url) { +async function createIframePopupProxy(url, frameOffsetForwarder) { const rootPopupInformationPromise = yomichan.getTemporaryListenerResult( chrome.runtime.onMessage, ({action, params}, {resolve}) => { @@ -36,8 +36,6 @@ async function createIframePopupProxy(url) { apiBroadcastTab('rootPopupRequestInformationBroadcast'); const {popupId, frameId} = await rootPopupInformationPromise; - const frameOffsetForwarder = new FrameOffsetForwarder(); - frameOffsetForwarder.start(); const getFrameOffset = frameOffsetForwarder.getOffset.bind(frameOffsetForwarder); const popup = new PopupProxy(popupId, 0, null, frameId, url, getFrameOffset); @@ -47,9 +45,6 @@ async function createIframePopupProxy(url) { } async function getOrCreatePopup(depth) { - const frameOffsetForwarder = new FrameOffsetForwarder(); - frameOffsetForwarder.start(); - const popupHost = new PopupProxyHost(); await popupHost.prepare(); @@ -81,14 +76,20 @@ async function main() { let frontend = null; let frontendPreparePromise = null; + let frameOffsetForwarder = null; const applyOptions = async () => { const optionsContext = {depth: isSearchPage ? 0 : depth, url}; const options = await apiOptionsGet(optionsContext); + if (!proxy && frameOffsetForwarder === null) { + frameOffsetForwarder = new FrameOffsetForwarder(); + frameOffsetForwarder.start(); + } + let popup; if (isIframe && options.general.showIframePopupsInRootFrame) { - popup = popups.iframe || await createIframePopupProxy(url); + popup = popups.iframe || await createIframePopupProxy(url, frameOffsetForwarder); popups.iframe = popup; } else if (proxy) { popup = popups.proxy || await createPopupProxy(depth, id, parentFrameId, url); From 950f50ef94931b4dfe19585ba0d7cb80324524ed Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 14 Apr 2020 20:35:52 +0300 Subject: [PATCH 16/17] don't focus tab when changing settings --- ext/fg/js/frontend.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 20bfc638..bf8cb9b4 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -141,7 +141,7 @@ class Frontend extends TextScanner { } async setPopup(popup) { - this.onSearchClear(true); + this.onSearchClear(false); this.popup = popup; await popup.setOptions(this.options); } From b6f7f8c02637cef9cadd4ab2b9f253c42903aee6 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 14 Apr 2020 20:59:42 +0300 Subject: [PATCH 17/17] fix _canEnable being used before setting options --- ext/fg/js/frontend.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index bf8cb9b4..6d16cdd9 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -147,7 +147,8 @@ class Frontend extends TextScanner { } async updateOptions() { - this.setOptions(await apiOptionsGet(this.getOptionsContext()), this._canEnable()); + this.options = await apiOptionsGet(this.getOptionsContext()); + this.setOptions(this.options, this._canEnable()); const ignoreNodes = ['.scan-disable', '.scan-disable *']; if (!this.options.scanning.enableOnPopupExpressions) { @@ -287,7 +288,6 @@ class Frontend extends TextScanner { } _canEnable() { - if (this.options === null) { return true; } // called by updateOptions for the first time return this.popup.depth <= this.options.scanning.popupNestingMaxDepth && !this._disabledOverride; }