feat(assets): bundle runtime assets and vendor dependencies

This commit is contained in:
2026-02-22 21:43:43 -08:00
parent d3fd47f0ec
commit ae95601698
429 changed files with 165389 additions and 0 deletions

94
vendor/yomitan/js/core/api-map.js vendored Normal file
View File

@@ -0,0 +1,94 @@
/*
* Copyright (C) 2023-2025 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 {ExtensionError} from './extension-error.js';
/**
* @template {import('api-map').ApiSurface} [TApiSurface=never]
* @template {unknown[]} [TExtraParams=[]]
* @param {import('api-map').ApiMapInit<TApiSurface, TExtraParams>} init
* @returns {import('api-map').ApiMap<TApiSurface, TExtraParams>}
*/
export function createApiMap(init) {
return new Map(init);
}
/**
* @template {import('api-map').ApiSurface} [TApiSurface=never]
* @template {unknown[]} [TExtraParams=[]]
* @param {import('api-map').ApiMap<TApiSurface, TExtraParams>} map
* @param {import('api-map').ApiMapInit<TApiSurface, TExtraParams>} init
* @throws {Error}
*/
export function extendApiMap(map, init) {
for (const [key, value] of init) {
if (map.has(key)) { throw new Error(`The handler for ${String(key)} has already been registered`); }
map.set(key, value);
}
}
/**
* @template {import('api-map').ApiSurface} [TApiSurface=never]
* @template {unknown[]} [TExtraParams=[]]
* @param {import('api-map').ApiMap<TApiSurface, TExtraParams>} map
* @param {string} name
* @returns {import('api-map').ApiHandlerAny<TApiSurface, TExtraParams>|undefined}
*/
export function getApiMapHandler(map, name) {
return map.get(/** @type {import('api-map').ApiNames<TApiSurface>} */ (name));
}
/**
* @template {import('api-map').ApiSurface} [TApiSurface=never]
* @template {unknown[]} [TExtraParams=[]]
* @param {import('api-map').ApiMap<TApiSurface, TExtraParams>} map
* @param {string} name
* @param {import('api-map').ApiParamsAny<TApiSurface>} params
* @param {TExtraParams} extraParams
* @param {(response: import('core').Response<import('api-map').ApiReturnAny<TApiSurface>>) => void} callback
* @param {() => void} [handlerNotFoundCallback]
* @returns {boolean} `true` if async, `false` otherwise.
*/
export function invokeApiMapHandler(map, name, params, extraParams, callback, handlerNotFoundCallback) {
const handler = getApiMapHandler(map, name);
if (typeof handler === 'undefined') {
if (typeof handlerNotFoundCallback === 'function') {
try {
handlerNotFoundCallback();
} catch (error) {
// NOP
}
}
return false;
}
try {
const promiseOrResult = handler(/** @type {import('core').SafeAny} */ (params), ...extraParams);
if (promiseOrResult instanceof Promise) {
/** @type {Promise<unknown>} */ (promiseOrResult).then(
(result) => { callback({result}); },
(error) => { callback({error: ExtensionError.serialize(error)}); },
);
return true;
} else {
callback({result: promiseOrResult});
return false;
}
} catch (error) {
callback({error: ExtensionError.serialize(error)});
return false;
}
}

View File

@@ -0,0 +1,131 @@
/*
* Copyright (C) 2023-2025 Yomitan Authors
* Copyright (C) 2019-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 {EventDispatcher} from './event-dispatcher.js';
import {generateId} from './utilities.js';
/**
* Class representing a generic value with an override stack.
* Changes can be observed by listening to the 'change' event.
* @template [T=unknown]
* @augments EventDispatcher<import('dynamic-property').Events<T>>
*/
export class DynamicProperty extends EventDispatcher {
/**
* Creates a new instance with the specified value.
* @param {T} value The value to assign.
*/
constructor(value) {
super();
/** @type {T} */
this._value = value;
/** @type {T} */
this._defaultValue = value;
/** @type {{value: T, priority: number, token: string}[]} */
this._overrides = [];
}
/**
* Gets the default value for the property, which is assigned to the
* public value property when no overrides are present.
* @type {T}
*/
get defaultValue() {
return this._defaultValue;
}
/**
* Assigns the default value for the property. If no overrides are present
* and if the value is different than the current default value,
* the 'change' event will be triggered.
* @param {T} value The value to assign.
*/
set defaultValue(value) {
this._defaultValue = value;
if (this._overrides.length === 0) { this._updateValue(); }
}
/**
* Gets the current value for the property, taking any overrides into account.
* @type {T}
*/
get value() {
return this._value;
}
/**
* Gets the number of overrides added to the property.
* @type {number}
*/
get overrideCount() {
return this._overrides.length;
}
/**
* Adds an override value with the specified priority to the override stack.
* Values with higher priority will take precedence over those with lower.
* For tie breaks, the override value added first will take precedence.
* If the newly added override has the highest priority of all overrides
* and if the override value is different from the current value,
* the 'change' event will be fired.
* @param {T} value The override value to assign.
* @param {number} [priority] The priority value to use, as a number.
* @returns {import('core').TokenString} A string token which can be passed to the clearOverride function
* to remove the override.
*/
setOverride(value, priority = 0) {
const overridesCount = this._overrides.length;
let i = 0;
for (; i < overridesCount; ++i) {
if (priority > this._overrides[i].priority) { break; }
}
const token = generateId(16);
this._overrides.splice(i, 0, {value, priority, token});
if (i === 0) { this._updateValue(); }
return token;
}
/**
* Removes a specific override value. If the removed override
* had the highest priority, and the new value is different from
* the previous value, the 'change' event will be fired.
* @param {import('core').TokenString} token The token for the corresponding override which is to be removed.
* @returns {boolean} `true` if an override was returned, `false` otherwise.
*/
clearOverride(token) {
for (let i = 0, ii = this._overrides.length; i < ii; ++i) {
if (this._overrides[i].token === token) {
this._overrides.splice(i, 1);
if (i === 0) { this._updateValue(); }
return true;
}
}
return false;
}
/**
* Updates the current value using the current overrides and default value.
* If the new value differs from the previous value, the 'change' event will be fired.
*/
_updateValue() {
const value = this._overrides.length > 0 ? this._overrides[0].value : this._defaultValue;
if (this._value === value) { return; }
this._value = value;
this.trigger('change', {value});
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2023-2025 Yomitan Authors
* Copyright (C) 2019-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/>.
*/
/**
* The following typedef is required because the JSDoc `implements` tag doesn't work with `import()`.
* https://github.com/microsoft/TypeScript/issues/49905
* @typedef {import('core').EventDispatcherOffGeneric} EventDispatcherOffGeneric
*/
/**
* Base class controls basic event dispatching.
* @template {import('core').EventSurface} TSurface
* @implements {EventDispatcherOffGeneric}
*/
export class EventDispatcher {
/**
* Creates a new instance.
*/
constructor() {
/** @type {Map<import('core').EventNames<TSurface>, import('core').EventHandlerAny[]>} */
this._eventMap = new Map();
}
/**
* Triggers an event with the given name and specified argument.
* @template {import('core').EventNames<TSurface>} TName
* @param {TName} eventName The string representing the event's name.
* @param {import('core').EventArgument<TSurface, TName>} details The argument passed to the callback functions.
* @returns {boolean} `true` if any callbacks were registered, `false` otherwise.
*/
trigger(eventName, details) {
const callbacks = this._eventMap.get(eventName);
if (typeof callbacks === 'undefined') { return false; }
for (const callback of callbacks) {
callback(details);
}
return true;
}
/**
* Adds a single event listener to a specific event.
* @template {import('core').EventNames<TSurface>} TName
* @param {TName} eventName The string representing the event's name.
* @param {import('core').EventHandler<TSurface, TName>} callback The event listener callback to add.
*/
on(eventName, callback) {
let callbacks = this._eventMap.get(eventName);
if (typeof callbacks === 'undefined') {
callbacks = [];
this._eventMap.set(eventName, callbacks);
}
callbacks.push(callback);
}
/**
* Removes a single event listener from a specific event.
* @template {import('core').EventNames<TSurface>} TName
* @param {TName} eventName The string representing the event's name.
* @param {import('core').EventHandler<TSurface, TName>} callback The event listener callback to add.
* @returns {boolean} `true` if the callback was removed, `false` otherwise.
*/
off(eventName, callback) {
const callbacks = this._eventMap.get(eventName);
if (typeof callbacks === 'undefined') { return false; }
const ii = callbacks.length;
for (let i = 0; i < ii; ++i) {
if (callbacks[i] === callback) {
callbacks.splice(i, 1);
if (callbacks.length === 0) {
this._eventMap.delete(eventName);
}
return true;
}
}
return false;
}
/**
* Checks if an event has any listeners.
* @template {import('core').EventNames<TSurface>} TName
* @param {TName} eventName The string representing the event's name.
* @returns {boolean} `true` if the event has listeners, `false` otherwise.
*/
hasListeners(eventName) {
const callbacks = this._eventMap.get(eventName);
return (typeof callbacks !== 'undefined' && callbacks.length > 0);
}
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2023-2025 Yomitan Authors
* Copyright (C) 2019-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/>.
*/
/**
* Class which stores event listeners added to various objects, making it easy to remove them in bulk.
*/
export class EventListenerCollection {
/**
* Creates a new instance.
*/
constructor() {
/** @type {import('event-listener-collection').EventListenerDetails[]} */
this._eventListeners = [];
}
/**
* Returns the number of event listeners that are currently in the object.
* @type {number}
*/
get size() {
return this._eventListeners.length;
}
/**
* Adds an event listener using `object.addEventListener`. The listener will later be removed using `object.removeEventListener`.
* @param {import('event-listener-collection').EventTarget} target The object to add the event listener to.
* @param {string} type The name of the event.
* @param {EventListener | EventListenerObject | import('event-listener-collection').EventListenerFunction} listener The callback listener.
* @param {AddEventListenerOptions | boolean} [options] Options for the event.
*/
addEventListener(target, type, listener, options) {
target.addEventListener(type, listener, options);
this._eventListeners.push({type: 'removeEventListener', target, eventName: type, listener, options});
}
/**
* Adds an event listener using `object.addListener`. The listener will later be removed using `object.removeListener`.
* @template {import('event-listener-collection').EventListenerFunction} TCallback
* @template [TArgs=unknown]
* @param {import('event-listener-collection').ExtensionEvent<TCallback, TArgs>} target The object to add the event listener to.
* @param {TCallback} callback The callback.
* @param {TArgs[]} args The extra argument array passed to the `addListener`/`removeListener` function.
*/
addListener(target, callback, ...args) {
target.addListener(callback, ...args);
this._eventListeners.push({type: 'removeListener', target, callback, args});
}
/**
* Adds an event listener using `object.on`. The listener will later be removed using `object.off`.
* @template {import('core').EventSurface} TSurface
* @template {import('core').EventNames<TSurface>} TName
* @param {import('./event-dispatcher.js').EventDispatcher<TSurface>} target The object to add the event listener to.
* @param {TName} eventName The string representing the event's name.
* @param {import('core').EventHandler<TSurface, TName>} callback The event listener callback to add.
*/
on(target, eventName, callback) {
target.on(eventName, callback);
this._eventListeners.push({type: 'off', eventName, target, callback});
}
/**
* Removes all event listeners added to objects for this instance and clears the internal list of event listeners.
*/
removeAllEventListeners() {
if (this._eventListeners.length === 0) { return; }
for (const item of this._eventListeners) {
switch (item.type) {
case 'removeEventListener':
item.target.removeEventListener(item.eventName, item.listener, item.options);
break;
case 'removeListener':
item.target.removeListener(item.callback, ...item.args);
break;
case 'off':
item.target.off(item.eventName, item.callback);
break;
}
}
this._eventListeners = [];
}
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright (C) 2023-2025 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/>.
*/
/**
* Custom error class for the extension which can contain extra data.
* This works around an issue where assigning the `DOMException.data` field can fail on Firefox.
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=1776555
*/
export class ExtensionError extends Error {
/**
* @param {string} message
*/
constructor(message) {
super(message);
/** @type {string} */
this.name = 'ExtensionError';
/** @type {unknown} */
this._data = void 0;
}
/** @type {unknown} */
get data() { return this._data; }
set data(value) { this._data = value; }
/**
* Converts an `Error` object to a serializable JSON object.
* @param {unknown} error An error object to convert.
* @returns {import('core').SerializedError} A simple object which can be serialized by `JSON.stringify()`.
*/
static serialize(error) {
try {
if (typeof error === 'object' && error !== null) {
const {name, message, stack} = /** @type {import('core').SerializableObject} */ (error);
/** @type {import('core').SerializedError1} */
const result = {
name: typeof name === 'string' ? name : '',
message: typeof message === 'string' ? message : '',
stack: typeof stack === 'string' ? stack : '',
};
if (error instanceof ExtensionError) {
result.data = error.data;
}
return result;
}
} catch (e) {
// NOP
}
return /** @type {import('core').SerializedError2} */ ({
value: error,
hasValue: true,
});
}
/**
* Converts a serialized error into a standard `Error` object.
* @param {import('core').SerializedError} serializedError A simple object which was initially generated by the `serialize` function.
* @returns {ExtensionError} A new `Error` instance.
*/
static deserialize(serializedError) {
if (serializedError.hasValue) {
const {value} = serializedError;
return new ExtensionError(`Error of type ${typeof value}: ${value}`);
}
const {message, name, stack, data} = serializedError;
const error = new ExtensionError(message);
error.name = name;
error.stack = stack;
if (typeof data !== 'undefined') {
error.data = data;
}
return error;
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2024-2025 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 {readResponseJson} from './json.js';
/**
* @param {string} url
* @returns {Promise<Response>}
*/
async function fetchAsset(url) {
const response = await fetch(chrome.runtime.getURL(url), {
method: 'GET',
mode: 'no-cors',
cache: 'default',
credentials: 'omit',
redirect: 'follow',
referrerPolicy: 'no-referrer',
});
if (!response.ok) {
throw new Error(`Failed to fetch ${url}: ${response.status}`);
}
return response;
}
/**
* @param {string} url
* @returns {Promise<string>}
*/
export async function fetchText(url) {
const response = await fetchAsset(url);
return await response.text();
}
/**
* @template [T=unknown]
* @param {string} url
* @returns {Promise<T>}
*/
export async function fetchJson(url) {
const response = await fetchAsset(url);
return await readResponseJson(response);
}

42
vendor/yomitan/js/core/json.js vendored Normal file
View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2023-2025 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/>.
*/
/**
* This function is used to ensure more safe usage of `JSON.parse`.
* By default, `JSON.parse` returns a value with type `any`, which is easy to misuse.
* By changing the default to `unknown` and allowing it to be templatized,
* this improves how the return type is used.
* @template [T=unknown]
* @param {string} value
* @returns {T}
*/
export function parseJson(value) {
// eslint-disable-next-line no-restricted-syntax
return JSON.parse(value);
}
/**
* This function is used to ensure more safe usage of `Response.json`,
* which returns the `any` type.
* @template [T=unknown]
* @param {Response} response
* @returns {Promise<T>}
*/
export async function readResponseJson(response) {
// eslint-disable-next-line no-restricted-syntax
return await response.json();
}

28
vendor/yomitan/js/core/log-utilities.js vendored Normal file
View File

@@ -0,0 +1,28 @@
/*
* Copyright (C) 2024-2025 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/>.
*/
/**
* @param {import('log').LogLevel} errorLevel
* @returns {number}
*/
export function logErrorLevelToNumber(errorLevel) {
switch (errorLevel) {
case 'warn': return 1;
case 'error': return 2;
default: return 0;
}
}

149
vendor/yomitan/js/core/log.js vendored Normal file
View File

@@ -0,0 +1,149 @@
/*
* Copyright (C) 2023-2025 Yomitan Authors
* Copyright (C) 2019-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 {EventDispatcher} from './event-dispatcher.js';
import {ExtensionError} from './extension-error.js';
/**
* This class handles logging of messages to the console and triggering an event for log calls.
* @augments EventDispatcher<import('log').Events>
*/
class Logger extends EventDispatcher {
constructor() {
super();
/** @type {string} */
this._extensionName = 'Extension';
/** @type {?string} */
this._issueUrl = 'https://github.com/yomidevs/yomitan/issues';
}
/**
* @param {string} extensionName
*/
configure(extensionName) {
this._extensionName = extensionName;
}
/**
* @param {unknown} message
* @param {...unknown} optionalParams
*/
log(message, ...optionalParams) {
/* eslint-disable no-console */
console.log(message, ...optionalParams);
/* eslint-enable no-console */
}
/**
* Logs a warning.
* @param {unknown} error The error to log. This is typically an `Error` or `Error`-like object.
*/
warn(error) {
this.logGenericError(error, 'warn');
}
/**
* Logs an error.
* @param {unknown} error The error to log. This is typically an `Error` or `Error`-like object.
*/
error(error) {
this.logGenericError(error, 'error');
}
/**
* Logs a generic error.
* @param {unknown} error The error to log. This is typically an `Error` or `Error`-like object.
* @param {import('log').LogLevel} level
* @param {import('log').LogContext} [context]
*/
logGenericError(error, level, context) {
if (typeof context === 'undefined') {
context = typeof location === 'undefined' ? {url: 'unknown'} : {url: location.href};
}
let errorString;
try {
if (typeof error === 'string') {
errorString = error;
} else {
errorString = (
typeof error === 'object' && error !== null ?
// eslint-disable-next-line @typescript-eslint/no-base-to-string
error.toString() :
`${error}`
);
if (/^\[object \w+\]$/.test(errorString)) {
errorString = JSON.stringify(error);
}
}
} catch (e) {
errorString = `${error}`;
}
let errorStack;
try {
errorStack = (
error instanceof Error ?
(typeof error.stack === 'string' ? error.stack.trimEnd() : '') :
''
);
} catch (e) {
errorStack = '';
}
let errorData;
try {
if (error instanceof ExtensionError) {
errorData = error.data;
}
} catch (e) {
// NOP
}
if (errorStack.startsWith(errorString)) {
errorString = errorStack;
} else if (errorStack.length > 0) {
errorString += `\n${errorStack}`;
}
let message = `${this._extensionName} has encountered a problem.`;
message += `\nOriginating URL: ${context.url}\n`;
message += errorString;
if (typeof errorData !== 'undefined') {
message += `\nData: ${JSON.stringify(errorData, null, 4)}`;
}
if (this._issueUrl !== null) {
message += `\n\nIssues can be reported at ${this._issueUrl}`;
}
/* eslint-disable no-console */
switch (level) {
case 'log': console.log(message); break;
case 'warn': console.warn(message); break;
case 'error': console.error(message); break;
}
/* eslint-enable no-console */
this.trigger('logGenericError', {error, level, context});
}
}
/**
* This object is the default logger used by the runtime.
*/
export const log = new Logger();

View File

@@ -0,0 +1,32 @@
/*
* Copyright (C) 2024-2025 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/>.
*/
/**
* @param {unknown} value
* @returns {value is Record<string, unknown>}
*/
export function isObjectNotArray(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
/**
* @param {unknown} value
* @returns {value is Record<string|number|symbol, unknown>}
*/
export function isObject(value) {
return typeof value === 'object' && value !== null;
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2023-2025 Yomitan Authors
* Copyright (C) 2019-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 {safePerformance} from './safe-performance.js';
/**
* Creates a promise that will resolve after the next animation frame, using `requestAnimationFrame`.
* @param {number} [timeout] A maximum duration (in milliseconds) to wait until the promise resolves. If null or omitted, no timeout is used.
* @returns {Promise<{time: number, timeout: boolean}>} A promise that is resolved with `{time, timeout}`, where `time` is the timestamp from `requestAnimationFrame`,
* and `timeout` is a boolean indicating whether the cause was a timeout or not.
* @throws The promise throws an error if animation is not supported in this context, such as in a service worker.
*/
export function promiseAnimationFrame(timeout) {
return new Promise((resolve, reject) => {
if (typeof cancelAnimationFrame !== 'function' || typeof requestAnimationFrame !== 'function') {
reject(new Error('Animation not supported in this context'));
return;
}
/** @type {?import('core').Timeout} */
let timer = null;
/** @type {?number} */
let frameRequest = null;
/**
* @param {number} time
*/
const onFrame = (time) => {
frameRequest = null;
if (timer !== null) {
clearTimeout(timer);
timer = null;
}
resolve({time, timeout: false});
};
const onTimeout = () => {
timer = null;
if (frameRequest !== null) {
cancelAnimationFrame(frameRequest);
frameRequest = null;
}
resolve({time: safePerformance.now(), timeout: true});
};
frameRequest = requestAnimationFrame(onFrame);
if (typeof timeout === 'number') {
timer = setTimeout(onTimeout, timeout);
}
});
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright (C) 2024-2025 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 {log} from './log.js';
/**
* This class safely handles performance methods.
*/
class SafePerformance {
constructor() {}
/**
* @param {string} markName
* @param {PerformanceMarkOptions} [markOptions]
* @returns {PerformanceMark | undefined}
*/
mark(markName, markOptions) {
try {
// eslint-disable-next-line no-restricted-syntax
return performance.mark(markName, markOptions);
} catch (e) {
log.error(e);
}
}
/**
*
* @param {string} measureName
* @param {string | PerformanceMeasureOptions} [startOrMeasureOptions]
* @param {string} [endMark]
* @returns {PerformanceMeasure | undefined}
*/
measure(measureName, startOrMeasureOptions, endMark) {
try {
// eslint-disable-next-line no-restricted-syntax
return performance.measure(measureName, startOrMeasureOptions, endMark);
} catch (e) {
log.error(e);
}
}
/**
* @returns {DOMHighResTimeStamp}
*/
now() {
// eslint-disable-next-line no-restricted-syntax
return performance.now();
}
}
/**
* This object is the default performance measurer used by the runtime.
*/
export const safePerformance = new SafePerformance();

26
vendor/yomitan/js/core/to-error.js vendored Normal file
View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2023-2025 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/>.
*/
/**
* Utility function to convert an unknown value to an error.
* This is useful for try-catch situations where the catch parameter has the `unknown` type.
* @param {unknown} value
* @returns {Error}
*/
export function toError(value) {
return value instanceof Error ? value : new Error(`${value}`);
}

322
vendor/yomitan/js/core/utilities.js vendored Normal file
View File

@@ -0,0 +1,322 @@
/*
* Copyright (C) 2023-2025 Yomitan Authors
* Copyright (C) 2019-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 {log} from './log.js';
import {toError} from './to-error.js';
/**
* Converts any string into a form that can be passed into the RegExp constructor.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
* @param {string} string The string to convert to a valid regular expression.
* @returns {string} The escaped string.
*/
export function escapeRegExp(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
}
/**
* Reverses a string.
* @param {string} string The string to reverse.
* @returns {string} The returned string, which retains proper UTF-16 surrogate pair order.
*/
export function stringReverse(string) {
return [...string].reverse().join('');
}
/**
* Creates a deep clone of an object or value. This is similar to `parseJson(JSON.stringify(value))`.
* @template [T=unknown]
* @param {T} value The value to clone.
* @returns {T} A new clone of the value.
* @throws An error if the value is circular and cannot be cloned.
*/
export function clone(value) {
if (value === null) { return /** @type {T} */ (null); }
switch (typeof value) {
case 'boolean':
case 'number':
case 'string':
case 'bigint':
case 'symbol':
case 'undefined':
return value;
default:
return cloneInternal(value, new Set());
}
}
/**
* @template [T=unknown]
* @param {T} value
* @param {Set<unknown>} visited
* @returns {T}
* @throws {Error}
*/
function cloneInternal(value, visited) {
if (value === null) { return /** @type {T} */ (null); }
switch (typeof value) {
case 'boolean':
case 'number':
case 'string':
case 'bigint':
case 'symbol':
case 'undefined':
return value;
case 'object':
return /** @type {T} */ (
Array.isArray(value) ?
cloneArray(value, visited) :
cloneObject(/** @type {import('core').SerializableObject} */ (value), visited)
);
default:
throw new Error(`Cannot clone object of type ${typeof value}`);
}
}
/**
* @param {unknown[]} value
* @param {Set<unknown>} visited
* @returns {unknown[]}
* @throws {Error}
*/
function cloneArray(value, visited) {
if (visited.has(value)) { throw new Error('Circular'); }
try {
visited.add(value);
const result = [];
for (const item of value) {
result.push(cloneInternal(item, visited));
}
return result;
} finally {
visited.delete(value);
}
}
/**
* @param {import('core').SerializableObject} value
* @param {Set<unknown>} visited
* @returns {import('core').SerializableObject}
* @throws {Error}
*/
function cloneObject(value, visited) {
if (visited.has(value)) { throw new Error('Circular'); }
try {
visited.add(value);
/** @type {import('core').SerializableObject} */
const result = {};
for (const key in value) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
result[key] = cloneInternal(value[key], visited);
}
}
return result;
} finally {
visited.delete(value);
}
}
/**
* Checks if an object or value is deeply equal to another object or value.
* @param {unknown} value1 The first value to check.
* @param {unknown} value2 The second value to check.
* @returns {boolean} `true` if the values are the same object, or deeply equal without cycles. `false` otherwise.
*/
export function deepEqual(value1, value2) {
if (value1 === value2) { return true; }
const type = typeof value1;
if (typeof value2 !== type) { return false; }
switch (type) {
case 'object':
case 'function':
return deepEqualInternal(value1, value2, new Set());
default:
return false;
}
}
/**
* @param {unknown} value1
* @param {unknown} value2
* @param {Set<unknown>} visited1
* @returns {boolean}
*/
function deepEqualInternal(value1, value2, visited1) {
if (value1 === value2) { return true; }
const type = typeof value1;
if (typeof value2 !== type) { return false; }
switch (type) {
case 'object':
case 'function':
{
if (value1 === null || value2 === null) { return false; }
const array = Array.isArray(value1);
if (array !== Array.isArray(value2)) { return false; }
if (visited1.has(value1)) { return false; }
visited1.add(value1);
return (
array ?
areArraysEqual(/** @type {unknown[]} */ (value1), /** @type {unknown[]} */ (value2), visited1) :
areObjectsEqual(/** @type {import('core').UnknownObject} */ (value1), /** @type {import('core').UnknownObject} */ (value2), visited1)
);
}
default:
return false;
}
}
/**
* @param {import('core').UnknownObject} value1
* @param {import('core').UnknownObject} value2
* @param {Set<unknown>} visited1
* @returns {boolean}
*/
function areObjectsEqual(value1, value2, visited1) {
const keys1 = Object.keys(value1);
const keys2 = Object.keys(value2);
if (keys1.length !== keys2.length) { return false; }
const keys1Set = new Set(keys1);
for (const key of keys2) {
if (!keys1Set.has(key) || !deepEqualInternal(value1[key], value2[key], visited1)) { return false; }
}
return true;
}
/**
* @param {unknown[]} value1
* @param {unknown[]} value2
* @param {Set<unknown>} visited1
* @returns {boolean}
*/
function areArraysEqual(value1, value2, visited1) {
const length = value1.length;
if (length !== value2.length) { return false; }
for (let i = 0; i < length; ++i) {
if (!deepEqualInternal(value1[i], value2[i], visited1)) { return false; }
}
return true;
}
/**
* Creates a new base-16 (lower case) string of a sequence of random bytes of the given length.
* @param {number} length The number of bytes the string represents. The returned string's length will be twice as long.
* @returns {string} A string of random characters.
*/
export function generateId(length) {
const array = new Uint8Array(length);
crypto.getRandomValues(array);
let id = '';
for (const value of array) {
id += value.toString(16).padStart(2, '0');
}
return id;
}
/**
* Creates an unresolved promise that can be resolved later, outside the promise's executor function.
* @template [T=unknown]
* @returns {import('core').DeferredPromiseDetails<T>} An object `{promise, resolve, reject}`, containing the promise and the resolve/reject functions.
*/
export function deferPromise() {
/** @type {((value: T) => void)|undefined} */
let resolve;
/** @type {((reason?: import('core').RejectionReason) => void)|undefined} */
let reject;
/** @type {Promise<T>} */
const promise = new Promise((resolve2, reject2) => {
resolve = resolve2;
reject = reject2;
});
return {
promise,
resolve: /** @type {(value: T) => void} */ (resolve),
reject: /** @type {(reason?: import('core').RejectionReason) => void} */ (reject),
};
}
/**
* Creates a promise that is resolved after a set delay.
* @param {number} delay How many milliseconds until the promise should be resolved. If 0, the promise is immediately resolved.
* @returns {Promise<void>} A promise with two additional properties: `resolve` and `reject`, which can be used to complete the promise early.
*/
export function promiseTimeout(delay) {
return delay <= 0 ? Promise.resolve() : new Promise((resolve) => { setTimeout(resolve, delay); });
}
/**
* @param {string} css
* @returns {string}
*/
export function sanitizeCSS(css) {
const sanitizer = new CSSStyleSheet();
sanitizer.replaceSync(css);
return [...sanitizer.cssRules].map((rule) => rule.cssText || '').join('\n');
}
/**
* @param {string} css
* @param {string} scopeSelector
* @returns {string}
*/
export function addScopeToCss(css, scopeSelector) {
return scopeSelector + ' {' + css + '\n}';
}
/**
* Older browser versions do not support nested css and cannot use the normal `addScopeToCss`.
* All major web browsers should be fine but Anki is still distributing Chromium 112 on some platforms as of Anki version 24.11.
* As of Anki 25.02, nesting is supported. However, many users take issue with changes around this time and refuse to update.
* Chromium 120+ is required for full support.
* @param {string} css
* @param {string} scopeSelector
* @returns {string}
*/
export function addScopeToCssLegacy(css, scopeSelector) {
try {
const stylesheet = new CSSStyleSheet();
stylesheet.replaceSync(css);
const newCSSRules = [];
for (const cssRule of stylesheet.cssRules) {
// ignore non-style rules
if (!(cssRule instanceof CSSStyleRule)) {
continue;
}
const newSelectors = [];
for (const selector of cssRule.selectorText.split(',')) {
newSelectors.push(scopeSelector + ' ' + selector);
}
const newRule = cssRule.cssText.replace(cssRule.selectorText, newSelectors.join(', '));
newCSSRules.push(newRule);
}
stylesheet.replaceSync(newCSSRules.join('\n'));
return [...stylesheet.cssRules].map((rule) => rule.cssText || '').join('\n');
} catch (e) {
log.log('addScopeToCssLegacy failed, falling back on addScopeToCss: ' + toError(e).message);
return addScopeToCss(css, scopeSelector);
}
}