aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/js/app/popup.js6
-rw-r--r--ext/js/display/display.js20
-rw-r--r--ext/js/dom/style-util.js133
-rw-r--r--ext/js/script/dynamic-loader-sentinel.js21
-rw-r--r--ext/js/script/dynamic-loader.js208
-rw-r--r--types/ext/dynamic-loader.d.ts20
6 files changed, 141 insertions, 267 deletions
diff --git a/ext/js/app/popup.js b/ext/js/app/popup.js
index a2aa204a..45604a55 100644
--- a/ext/js/app/popup.js
+++ b/ext/js/app/popup.js
@@ -20,7 +20,7 @@ import {FrameClient} from '../comm/frame-client.js';
import {DynamicProperty, EventDispatcher, EventListenerCollection, deepEqual} from '../core.js';
import {ExtensionError} from '../core/extension-error.js';
import {DocumentUtil} from '../dom/document-util.js';
-import {dynamicLoader} from '../script/dynamic-loader.js';
+import {loadStyle} from '../dom/style-util.js';
import {yomitan} from '../yomitan.js';
import {ThemeController} from './theme-controller.js';
@@ -359,7 +359,7 @@ export class Popup extends EventDispatcher {
useWebExtensionApi = false;
parentNode = this._shadow;
}
- const node = await dynamicLoader.loadStyle('yomitan-popup-outer-user-stylesheet', 'code', css, useWebExtensionApi, parentNode);
+ const node = await loadStyle('yomitan-popup-outer-user-stylesheet', 'code', css, useWebExtensionApi, parentNode);
/** @type {import('popup').CustomOuterCssChangedEvent} */
const event = {node, useWebExtensionApi, inShadow};
this.trigger('customOuterCssChanged', event);
@@ -574,7 +574,7 @@ export class Popup extends EventDispatcher {
useWebExtensionApi = false;
parentNode = this._shadow;
}
- await dynamicLoader.loadStyle('yomitan-popup-outer-stylesheet', fileType, '/css/popup-outer.css', useWebExtensionApi, parentNode);
+ await loadStyle('yomitan-popup-outer-stylesheet', fileType, '/css/popup-outer.css', useWebExtensionApi, parentNode);
}
/**
diff --git a/ext/js/display/display.js b/ext/js/display/display.js
index 945ec0b9..d7b8f898 100644
--- a/ext/js/display/display.js
+++ b/ext/js/display/display.js
@@ -16,8 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-import {Frontend} from '../app/frontend.js';
-import {PopupFactory} from '../app/popup-factory.js';
import {ThemeController} from '../app/theme-controller.js';
import {FrameEndpoint} from '../comm/frame-endpoint.js';
import {DynamicProperty, EventDispatcher, EventListenerCollection, clone, deepEqual, invokeMessageHandler, log, promiseTimeout} from '../core.js';
@@ -26,7 +24,6 @@ import {querySelectorNotNull} from '../dom/query-selector.js';
import {ScrollElement} from '../dom/scroll-element.js';
import {HotkeyHelpController} from '../input/hotkey-help-controller.js';
import {TextScanner} from '../language/text-scanner.js';
-import {dynamicLoader} from '../script/dynamic-loader.js';
import {yomitan} from '../yomitan.js';
import {DisplayContentManager} from './display-content-manager.js';
import {DisplayGenerator} from './display-generator.js';
@@ -145,7 +142,7 @@ export class Display extends EventDispatcher {
this._navigationPreviousButton = document.querySelector('#navigate-previous-button');
/** @type {?HTMLButtonElement} */
this._navigationNextButton = document.querySelector('#navigate-next-button');
- /** @type {?Frontend} */
+ /** @type {?import('../app/frontend.js').Frontend} */
this._frontend = null;
/** @type {?Promise<void>} */
this._frontendSetupPromise = null;
@@ -1707,7 +1704,7 @@ export class Display extends EventDispatcher {
}
}
- /** @type {Frontend} */ (this._frontend).setDisabledOverride(!isEnabled);
+ /** @type {import('../app/frontend.js').Frontend} */ (this._frontend).setDisabledOverride(!isEnabled);
}
/** */
@@ -1720,16 +1717,9 @@ export class Display extends EventDispatcher {
const parentPopupId = this._parentPopupId;
const parentFrameId = this._parentFrameId;
- await dynamicLoader.loadScripts([
- '/js/language/text-scanner.js',
- '/js/comm/frame-client.js',
- '/js/app/popup.js',
- '/js/app/popup-proxy.js',
- '/js/app/popup-window.js',
- '/js/app/popup-factory.js',
- '/js/comm/frame-ancestry-handler.js',
- '/js/comm/frame-offset-forwarder.js',
- '/js/app/frontend.js'
+ const [{PopupFactory}, {Frontend}] = await Promise.all([
+ import('../app/popup-factory.js'),
+ import('../app/frontend.js')
]);
const popupFactory = new PopupFactory(this._frameId);
diff --git a/ext/js/dom/style-util.js b/ext/js/dom/style-util.js
new file mode 100644
index 00000000..90dddd9e
--- /dev/null
+++ b/ext/js/dom/style-util.js
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2023 Yomitan Authors
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+import {yomitan} from '../yomitan.js';
+
+/** @type {Map<string, ?HTMLStyleElement|HTMLLinkElement>} */
+const injectedStylesheets = new Map();
+/** @type {WeakMap<Node, Map<string, ?HTMLStyleElement|HTMLLinkElement>>} */
+const injectedStylesheetsWithParent = new WeakMap();
+
+/**
+ * @param {string} id
+ * @param {?Node} parentNode
+ * @returns {?HTMLStyleElement|HTMLLinkElement|undefined}
+ */
+function getInjectedStylesheet(id, parentNode) {
+ if (parentNode === null) {
+ return injectedStylesheets.get(id);
+ }
+ const map = injectedStylesheetsWithParent.get(parentNode);
+ return typeof map !== 'undefined' ? map.get(id) : void 0;
+}
+
+/**
+ * @param {string} id
+ * @param {?Node} parentNode
+ * @param {?HTMLStyleElement|HTMLLinkElement} value
+ */
+function setInjectedStylesheet(id, parentNode, value) {
+ if (parentNode === null) {
+ injectedStylesheets.set(id, value);
+ return;
+ }
+ let map = injectedStylesheetsWithParent.get(parentNode);
+ if (typeof map === 'undefined') {
+ map = new Map();
+ injectedStylesheetsWithParent.set(parentNode, map);
+ }
+ map.set(id, value);
+}
+
+/**
+ * @param {string} id
+ * @param {'code'|'file'|'file-content'} type
+ * @param {string} value
+ * @param {boolean} [useWebExtensionApi]
+ * @param {?Node} [parentNode]
+ * @returns {Promise<?HTMLStyleElement|HTMLLinkElement>}
+ * @throws {Error}
+ */
+export async function loadStyle(id, type, value, useWebExtensionApi = false, parentNode = null) {
+ if (useWebExtensionApi && yomitan.isExtensionUrl(window.location.href)) {
+ // Permissions error will occur if trying to use the WebExtension API to inject into an extension page
+ useWebExtensionApi = false;
+ }
+
+ let styleNode = getInjectedStylesheet(id, parentNode);
+ if (typeof styleNode !== 'undefined') {
+ if (styleNode === null) {
+ // Previously injected via WebExtension API
+ throw new Error(`Stylesheet with id ${id} has already been injected using the WebExtension API`);
+ }
+ } else {
+ styleNode = null;
+ }
+
+ if (type === 'file-content') {
+ value = await yomitan.api.getStylesheetContent(value);
+ type = 'code';
+ useWebExtensionApi = false;
+ }
+
+ if (useWebExtensionApi) {
+ // Inject via WebExtension API
+ if (styleNode !== null && styleNode.parentNode !== null) {
+ styleNode.parentNode.removeChild(styleNode);
+ }
+
+ setInjectedStylesheet(id, parentNode, null);
+ await yomitan.api.injectStylesheet(type, value);
+ return null;
+ }
+
+ // Create node in document
+ let parentNode2 = parentNode;
+ if (parentNode2 === null) {
+ parentNode2 = document.head;
+ if (parentNode2 === null) {
+ throw new Error('No parent node');
+ }
+ }
+
+ // Create or reuse node
+ const isFile = (type === 'file');
+ const tagName = isFile ? 'link' : 'style';
+ if (styleNode === null || styleNode.nodeName.toLowerCase() !== tagName) {
+ if (styleNode !== null && styleNode.parentNode !== null) {
+ styleNode.parentNode.removeChild(styleNode);
+ }
+ styleNode = document.createElement(tagName);
+ }
+
+ // Update node style
+ if (isFile) {
+ /** @type {HTMLLinkElement} */ (styleNode).rel = 'stylesheet';
+ /** @type {HTMLLinkElement} */ (styleNode).href = value;
+ } else {
+ styleNode.textContent = value;
+ }
+
+ // Update parent
+ if (styleNode.parentNode !== parentNode2) {
+ parentNode2.appendChild(styleNode);
+ }
+
+ // Add to map
+ setInjectedStylesheet(id, parentNode, styleNode);
+ return styleNode;
+}
diff --git a/ext/js/script/dynamic-loader-sentinel.js b/ext/js/script/dynamic-loader-sentinel.js
deleted file mode 100644
index d77f3cb0..00000000
--- a/ext/js/script/dynamic-loader-sentinel.js
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (C) 2023 Yomitan Authors
- * Copyright (C) 2020-2022 Yomichan Authors
- *
- * 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 <https://www.gnu.org/licenses/>.
- */
-
-import {yomitan} from '../yomitan.js';
-
-yomitan.trigger('dynamicLoaderSentinel', /** @type {import('dynamic-loader').DynamicLoaderSentinelDetails} */ ({scriptUrl: import.meta.url}));
diff --git a/ext/js/script/dynamic-loader.js b/ext/js/script/dynamic-loader.js
deleted file mode 100644
index 8fdb77e9..00000000
--- a/ext/js/script/dynamic-loader.js
+++ /dev/null
@@ -1,208 +0,0 @@
-/*
- * Copyright (C) 2023 Yomitan Authors
- * Copyright (C) 2020-2022 Yomichan Authors
- *
- * 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 <https://www.gnu.org/licenses/>.
- */
-
-import {yomitan} from '../yomitan.js';
-
-export const dynamicLoader = (() => {
- /** @type {Map<string, ?HTMLStyleElement|HTMLLinkElement>} */
- const injectedStylesheets = new Map();
- /** @type {WeakMap<Node, Map<string, ?HTMLStyleElement|HTMLLinkElement>>} */
- const injectedStylesheetsWithParent = new WeakMap();
-
- /**
- * @param {string} id
- * @param {?Node} parentNode
- * @returns {?HTMLStyleElement|HTMLLinkElement|undefined}
- */
- function getInjectedStylesheet(id, parentNode) {
- if (parentNode === null) {
- return injectedStylesheets.get(id);
- }
- const map = injectedStylesheetsWithParent.get(parentNode);
- return typeof map !== 'undefined' ? map.get(id) : void 0;
- }
-
- /**
- * @param {string} id
- * @param {?Node} parentNode
- * @param {?HTMLStyleElement|HTMLLinkElement} value
- */
- function setInjectedStylesheet(id, parentNode, value) {
- if (parentNode === null) {
- injectedStylesheets.set(id, value);
- return;
- }
- let map = injectedStylesheetsWithParent.get(parentNode);
- if (typeof map === 'undefined') {
- map = new Map();
- injectedStylesheetsWithParent.set(parentNode, map);
- }
- map.set(id, value);
- }
-
- /**
- * @param {string} id
- * @param {'code'|'file'|'file-content'} type
- * @param {string} value
- * @param {boolean} [useWebExtensionApi]
- * @param {?Node} [parentNode]
- * @returns {Promise<?HTMLStyleElement|HTMLLinkElement>}
- * @throws {Error}
- */
- async function loadStyle(id, type, value, useWebExtensionApi = false, parentNode = null) {
- if (useWebExtensionApi && yomitan.isExtensionUrl(window.location.href)) {
- // Permissions error will occur if trying to use the WebExtension API to inject into an extension page
- useWebExtensionApi = false;
- }
-
- let styleNode = getInjectedStylesheet(id, parentNode);
- if (typeof styleNode !== 'undefined') {
- if (styleNode === null) {
- // Previously injected via WebExtension API
- throw new Error(`Stylesheet with id ${id} has already been injected using the WebExtension API`);
- }
- } else {
- styleNode = null;
- }
-
- if (type === 'file-content') {
- value = await yomitan.api.getStylesheetContent(value);
- type = 'code';
- useWebExtensionApi = false;
- }
-
- if (useWebExtensionApi) {
- // Inject via WebExtension API
- if (styleNode !== null && styleNode.parentNode !== null) {
- styleNode.parentNode.removeChild(styleNode);
- }
-
- setInjectedStylesheet(id, parentNode, null);
- await yomitan.api.injectStylesheet(type, value);
- return null;
- }
-
- // Create node in document
- let parentNode2 = parentNode;
- if (parentNode2 === null) {
- parentNode2 = document.head;
- if (parentNode2 === null) {
- throw new Error('No parent node');
- }
- }
-
- // Create or reuse node
- const isFile = (type === 'file');
- const tagName = isFile ? 'link' : 'style';
- if (styleNode === null || styleNode.nodeName.toLowerCase() !== tagName) {
- if (styleNode !== null && styleNode.parentNode !== null) {
- styleNode.parentNode.removeChild(styleNode);
- }
- styleNode = document.createElement(tagName);
- }
-
- // Update node style
- if (isFile) {
- /** @type {HTMLLinkElement} */ (styleNode).rel = 'stylesheet';
- /** @type {HTMLLinkElement} */ (styleNode).href = value;
- } else {
- styleNode.textContent = value;
- }
-
- // Update parent
- if (styleNode.parentNode !== parentNode2) {
- parentNode2.appendChild(styleNode);
- }
-
- // Add to map
- setInjectedStylesheet(id, parentNode, styleNode);
- return styleNode;
- }
-
- /**
- * @param {string[]} urls
- * @returns {Promise<void>}
- */
- function loadScripts(urls) {
- return new Promise((resolve, reject) => {
- const parent = document.body;
- if (parent === null) {
- reject(new Error('Missing body'));
- return;
- }
-
- for (const url of urls) {
- const node = parent.querySelector(`script[src='${escapeCSSAttribute(url)}']`);
- if (node !== null) { continue; }
-
- const script = document.createElement('script');
- script.type = 'module';
- script.async = false;
- script.src = url;
- parent.appendChild(script);
- }
-
- loadScriptSentinel(parent, resolve, reject);
- });
- }
-
- /**
- * @param {HTMLElement} parent
- * @param {() => void} resolve
- * @param {(reason?: unknown) => void} reject
- */
- function loadScriptSentinel(parent, resolve, reject) {
- const script = document.createElement('script');
-
- const sentinelEventName = 'dynamicLoaderSentinel';
- /**
- * @param {import('dynamic-loader').DynamicLoaderSentinelDetails} e
- */
- const sentinelEventCallback = (e) => {
- if (e.scriptUrl !== script.src) { return; }
- yomitan.off(sentinelEventName, sentinelEventCallback);
- parent.removeChild(script);
- resolve();
- };
- yomitan.on(sentinelEventName, sentinelEventCallback);
-
- try {
- script.type = 'module';
- script.async = false;
- script.src = '/js/script/dynamic-loader-sentinel.js';
- parent.appendChild(script);
- } catch (e) {
- yomitan.off(sentinelEventName, sentinelEventCallback);
- reject(e);
- }
- }
-
- /**
- * @param {string} value
- * @returns {string}
- */
- function escapeCSSAttribute(value) {
- return value.replace(/['\\]/g, (character) => `\\${character}`);
- }
-
-
- return {
- loadStyle,
- loadScripts
- };
-})();
diff --git a/types/ext/dynamic-loader.d.ts b/types/ext/dynamic-loader.d.ts
deleted file mode 100644
index 49011db0..00000000
--- a/types/ext/dynamic-loader.d.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (C) 2023 Yomitan Authors
- *
- * 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 <https://www.gnu.org/licenses/>.
- */
-
-export type DynamicLoaderSentinelDetails = {
- scriptUrl: string;
-};