diff options
-rw-r--r-- | ext/bg/js/backend.js | 1 | ||||
-rw-r--r-- | ext/bg/js/database.js | 3 | ||||
-rw-r--r-- | ext/bg/js/search-frontend.js | 40 | ||||
-rw-r--r-- | ext/bg/js/search-query-parser.js | 2 | ||||
-rw-r--r-- | ext/fg/js/frontend-initialize.js | 128 | ||||
-rw-r--r-- | ext/fg/js/frontend.js | 25 | ||||
-rw-r--r-- | ext/fg/js/popup-nested.js | 49 | ||||
-rw-r--r-- | ext/mixed/js/text-scanner.js | 10 | ||||
-rw-r--r-- | package-lock.json | 44 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | test/lint/global-declarations.js | 27 |
11 files changed, 233 insertions, 98 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index f8b0fc96..2265c1a9 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -30,7 +30,6 @@ * Translator * conditionsTestValue * dictConfigured - * dictEnabledSet * dictTermsSort * handlebarsRenderDynamic * jp diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index ad4e3bad..260c815a 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -16,10 +16,7 @@ */ /* global - * JSZip - * JsonSchema * dictFieldSplit - * requestJson */ class Database { diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-frontend.js index 9cc1436f..e534e771 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,29 @@ async function searchFrontendSetup() { } } -searchFrontendSetup(); +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 || optionsApplied) { return; } + optionsApplied = true; + + window.frontendInitializationData = {depth: 1, proxy: false, isSearchPage: true}; + injectSearchFrontend(); + + yomichan.off('optionsUpdated', applyOptions); + }; + + yomichan.on('optionsUpdated', applyOptions); + + await applyOptions(); +} + +main(); diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 692fb1a8..eb3b681c 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -26,7 +26,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; diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js index 5af7fdf0..2b942258 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/frontend-initialize.js @@ -24,49 +24,101 @@ * apiOptionsGet */ +async function createIframePopupProxy(url, frameOffsetForwarder) { + const rootPopupInformationPromise = yomichan.getTemporaryListenerResult( + chrome.runtime.onMessage, + ({action, params}, {resolve}) => { + if (action === 'rootPopupInformation') { + resolve(params); + } + } + ); + apiBroadcastTab('rootPopupRequestInformationBroadcast'); + const {popupId, frameId} = await rootPopupInformationPromise; + + 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 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(); const data = window.frontendInitializationData || {}; - const {id, depth=0, parentFrameId, url, proxy=false} = data; - - const optionsContext = {depth, url}; - const options = await apiOptionsGet(optionsContext); - - 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); - } + const {id, depth=0, parentFrameId, url=window.location.href, proxy=false, isSearchPage=false} = data; + + const isIframe = !proxy && (window !== window.parent); + + const popups = { + iframe: null, + proxy: null, + normal: null + }; + + 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, frameOffsetForwarder); + 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; + } + + if (frontend === null) { + frontend = new Frontend(popup); + frontendPreparePromise = frontend.prepare(); + await frontendPreparePromise; + } else { + await frontendPreparePromise; + if (isSearchPage) { + const disabled = !options.scanning.enableOnSearchPage; + frontend.setDisabledOverride(disabled); + } + + if (isIframe) { + await frontend.setPopup(popup); } - ); - 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(); - } else if (proxy) { - popup = new PopupProxy(null, depth + 1, id, parentFrameId, url); - await popup.prepare(); - } else { - const frameOffsetForwarder = new FrameOffsetForwarder(); - frameOffsetForwarder.start(); - - const popupHost = new PopupProxyHost(); - await popupHost.prepare(); - - popup = popupHost.getOrCreatePopup(null, null, depth); - } - - const frontend = new Frontend(popup); - await frontend.prepare(); + } + }; + + yomichan.on('optionsUpdated', applyOptions); + + await applyOptions(); } main(); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 95427c4c..eecfe2e1 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -29,11 +29,14 @@ class Frontend extends TextScanner { constructor(popup) { super( window, - popup.isProxy() ? [] : [popup.getContainer()], + () => this.popup.isProxy() ? [] : [this.popup.getContainer()], [(x, y) => this.popup.containsPoint(x, y)] ); this.popup = popup; + + this._disabledOverride = false; + this.options = null; this.optionsContext = { @@ -132,8 +135,20 @@ class Frontend extends TextScanner { ]; } + setDisabledOverride(disabled) { + this._disabledOverride = disabled; + this.setEnabled(this.options.general.enable, this._canEnable()); + } + + async setPopup(popup) { + this.onSearchClear(false); + this.popup = popup; + await popup.setOptions(this.options); + } + async updateOptions() { - this.setOptions(await apiOptionsGet(this.getOptionsContext())); + this.options = await apiOptionsGet(this.getOptionsContext()); + this.setOptions(this.options, this._canEnable()); const ignoreNodes = ['.scan-disable', '.scan-disable *']; if (!this.options.scanning.enableOnPopupExpressions) { @@ -259,7 +274,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}); } } @@ -272,6 +287,10 @@ class Frontend extends TextScanner { }); } + _canEnable() { + 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/fg/js/popup-nested.js b/ext/fg/js/popup-nested.js index 1b24614b..c140f9c8 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,33 @@ async function popupNestedInitialize(id, depth, parentFrameId, url) { document.body.appendChild(script); } } + +async function popupNestedInitialize(id, depth, parentFrameId, url) { + let optionsApplied = false; + + const applyOptions = async () => { + const optionsContext = {depth, url}; + const options = await apiOptionsGet(optionsContext); + const popupNestingMaxDepth = options.scanning.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(); + + yomichan.off('optionsUpdated', applyOptions); + }; + + yomichan.on('optionsUpdated', applyOptions); + + await applyOptions(); +} diff --git a/ext/mixed/js/text-scanner.js b/ext/mixed/js/text-scanner.js index e6b9a2e6..0cd12cd7 100644 --- a/ext/mixed/js/text-scanner.js +++ b/ext/mixed/js/text-scanner.js @@ -46,7 +46,7 @@ class TextScanner { } onMouseOver(e) { - if (this.ignoreElements.includes(e.target)) { + if (this.ignoreElements().includes(e.target)) { this.scanTimerClear(); } } @@ -224,8 +224,8 @@ class TextScanner { } } - setEnabled(enabled) { - if (enabled) { + setEnabled(enabled, canEnable) { + if (enabled && canEnable) { if (!this.enabled) { this.hookEvents(); this.enabled = true; @@ -271,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) { diff --git a/package-lock.json b/package-lock.json index 920263d2..8f421a68 100644 --- a/package-lock.json +++ b/package-lock.json @@ -906,9 +906,9 @@ "dev": true }, "jsdom": { - "version": "16.2.1", - "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-16.2.1.tgz", - "integrity": "sha512-3p0gHs5EfT7PxW9v8Phz3mrq//4Dy8MQenU/PoKxhdT+c45S7NjIjKbGT3Ph0nkICweE1r36+yaknXA5WfVNAg==", + "version": "16.2.2", + "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-16.2.2.tgz", + "integrity": "sha512-pDFQbcYtKBHxRaP55zGXCJWgFHkDAYbKcsXEK/3Icu9nKYZkutUXfLBwbD+09XDutkYSHcgfQLZ0qvpAAm9mvg==", "dev": true, "requires": { "abab": "^2.0.3", @@ -931,11 +931,11 @@ "tough-cookie": "^3.0.1", "w3c-hr-time": "^1.0.2", "w3c-xmlserializer": "^2.0.0", - "webidl-conversions": "^5.0.0", + "webidl-conversions": "^6.0.0", "whatwg-encoding": "^1.0.5", "whatwg-mimetype": "^2.3.0", "whatwg-url": "^8.0.0", - "ws": "^7.2.1", + "ws": "^7.2.3", "xml-name-validator": "^3.0.0" }, "dependencies": { @@ -946,6 +946,14 @@ "dev": true, "requires": { "webidl-conversions": "^5.0.0" + }, + "dependencies": { + "webidl-conversions": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-5.0.0.tgz", + "integrity": "sha512-VlZwKPCkYKxQgeSbH5EyngOmRp7Ww7I9rQLERETtf5ofd9pGeswWiOtogpEO850jziPRarreGxn5QIiTqpb2wA==", + "dev": true + } } }, "tr46": { @@ -958,9 +966,9 @@ } }, "webidl-conversions": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-5.0.0.tgz", - "integrity": "sha512-VlZwKPCkYKxQgeSbH5EyngOmRp7Ww7I9rQLERETtf5ofd9pGeswWiOtogpEO850jziPRarreGxn5QIiTqpb2wA==", + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-6.0.0.tgz", + "integrity": "sha512-jTZAeJnc6D+yAOjygbJOs33kVQIk5H6fj9SFDOhIKjsf9HiAzL/c+tAJsc8ASWafvhNkH+wJZms47pmajkhatA==", "dev": true }, "whatwg-url": { @@ -972,6 +980,14 @@ "lodash.sortby": "^4.7.0", "tr46": "^2.0.0", "webidl-conversions": "^5.0.0" + }, + "dependencies": { + "webidl-conversions": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-5.0.0.tgz", + "integrity": "sha512-VlZwKPCkYKxQgeSbH5EyngOmRp7Ww7I9rQLERETtf5ofd9pGeswWiOtogpEO850jziPRarreGxn5QIiTqpb2wA==", + "dev": true + } } } } @@ -1199,9 +1215,9 @@ "dev": true }, "psl": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/psl/-/psl-1.7.0.tgz", - "integrity": "sha512-5NsSEDv8zY70ScRnOTn7bK7eanl2MvFrOrS/R6x+dBt5g1ghnj9Zv90kO8GwT8gxcu2ANyFprnFYB85IogIJOQ==", + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", + "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==", "dev": true }, "punycode": { @@ -1362,9 +1378,9 @@ "dev": true }, "saxes": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/saxes/-/saxes-5.0.0.tgz", - "integrity": "sha512-LXTZygxhf8lfwKaTP/8N9CsVdjTlea3teze4lL6u37ivbgGbV0GGMuNtS/I9rnD/HC2/txUM7Df4S2LVl1qhiA==", + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/saxes/-/saxes-5.0.1.tgz", + "integrity": "sha512-5LBh1Tls8c9xgGjw3QrMwETmTMVk0oFgvrFSvWx62llR2hcEInrKNZ2GZCCuuy2lvWrdl5jhbpeqc5hRYKFOcw==", "dev": true, "requires": { "xmlchars": "^2.2.0" diff --git a/package.json b/package.json index b02ec179..0729cda1 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,6 @@ "eslint": "^6.8.0", "eslint-plugin-no-unsanitized": "^3.0.2", "fake-indexeddb": "^3.0.0", - "jsdom": "^16.2.1" + "jsdom": "^16.2.2" } } diff --git a/test/lint/global-declarations.js b/test/lint/global-declarations.js index 07ba5570..2fc9a5e2 100644 --- a/test/lint/global-declarations.js +++ b/test/lint/global-declarations.js @@ -37,6 +37,18 @@ function getNewline(string) { } } +function getSubstringCount(string, substring) { + let start = 0; + let count = 0; + while (true) { + const pos = string.indexOf(substring, start); + if (pos < 0) { break; } + ++count; + start = pos + substring.length; + } + return count; +} + function validateGlobals(fileName, fix) { const pattern = /\/\*\s*global\s+([\w\W]*?)\*\//g; @@ -47,6 +59,7 @@ function validateGlobals(fileName, fix) { let first = true; let endIndex = 0; let newSource = ''; + const allGlobals = []; const newline = getNewline(source); while ((match = pattern.exec(source)) !== null) { if (!first) { @@ -74,15 +87,27 @@ function validateGlobals(fileName, fix) { newSource += source.substring(0, match.index); newSource += expected; endIndex = match.index + match[0].length; + + allGlobals.push(...parts); } newSource += source.substring(endIndex); + // This is an approximate check to see if a global variable is unused. + // If the global appears in a comment, string, or similar, the check will pass. + let errorCount = 0; + for (const global of allGlobals) { + if (getSubstringCount(newSource, global) <= 1) { + console.error(`Global variable ${global} appears to be unused in ${fileName}`); + ++errorCount; + } + } + if (fix) { fs.writeFileSync(fileName, newSource, {encoding: 'utf8'}); } - return true; + return errorCount === 0; } |