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

View File

@@ -0,0 +1,126 @@
/*
* Copyright (C) 2023-2025 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/>.
*/
export class Environment {
constructor() {
/** @type {?import('environment').Info} */
this._cachedEnvironmentInfo = null;
}
/**
* @returns {Promise<void>}
*/
async prepare() {
this._cachedEnvironmentInfo = await this._loadEnvironmentInfo();
}
/**
* @returns {import('environment').Info}
* @throws {Error}
*/
getInfo() {
if (this._cachedEnvironmentInfo === null) { throw new Error('Not prepared'); }
return this._cachedEnvironmentInfo;
}
/**
* @returns {Promise<import('environment').Info>}
*/
async _loadEnvironmentInfo() {
const os = await this._getOperatingSystem();
const browser = await this._getBrowser(os);
return {
browser,
platform: {os},
};
}
/**
* @returns {Promise<import('environment').OperatingSystem>}
*/
async _getOperatingSystem() {
try {
const {os} = await this._getPlatformInfo();
if (typeof os === 'string') {
return os;
}
} catch (e) {
// NOP
}
return 'unknown';
}
/**
* @returns {Promise<chrome.runtime.PlatformInfo>}
*/
_getPlatformInfo() {
return new Promise((resolve, reject) => {
chrome.runtime.getPlatformInfo((result) => {
const error = chrome.runtime.lastError;
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
/**
* @param {import('environment').OperatingSystem} os
* @returns {Promise<import('environment').Browser>}
*/
async _getBrowser(os) {
try {
if (chrome.runtime.getURL('/').startsWith('ms-browser-extension://')) {
return 'edge-legacy';
}
if (/\bEdge?\//.test(navigator.userAgent)) {
return 'edge';
}
} catch (e) {
// NOP
}
if (typeof browser !== 'undefined') {
if (this._isSafari()) {
return 'safari';
}
if (os === 'android') {
return 'firefox-mobile';
}
return 'firefox';
} else {
return 'chrome';
}
}
/**
* @returns {boolean};
*/
_isSafari() {
const {vendor, userAgent} = navigator;
return (
typeof vendor === 'string' &&
typeof userAgent === 'string' &&
vendor.includes('Apple') &&
!userAgent.includes('CriOS') &&
!userAgent.includes('FxiOS')
);
}
}

View File

@@ -0,0 +1,137 @@
/*
* 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 {EventDispatcher} from '../core/event-dispatcher.js';
import {toError} from '../core/to-error.js';
/**
* @augments EventDispatcher<import('web-extension').Events>
*/
export class WebExtension extends EventDispatcher {
constructor() {
super();
/** @type {boolean} */
this._unloaded = false;
/** @type {?string} */
this._extensionBaseUrl = null;
try {
this._extensionBaseUrl = this.getUrl('/');
} catch (e) {
// NOP
}
/** @type {string} */
this._extensionName = 'Extension';
try {
const {name, version} = chrome.runtime.getManifest();
this._extensionName = `${name} ${version}`;
} catch (e) {
// NOP
}
}
/** @type {boolean} */
get unloaded() {
return this._unloaded;
}
/** @type {string} */
get extensionName() {
return this._extensionName;
}
/**
* @param {string} path
* @returns {string}
*/
getUrl(path) {
return chrome.runtime.getURL(path);
}
/**
* @param {unknown} message
* @param {(response: unknown) => void} responseCallback
* @throws {Error}
*/
sendMessage(message, responseCallback) {
try {
chrome.runtime.sendMessage(message, responseCallback);
} catch (error) {
this.triggerUnloaded();
throw toError(error);
}
}
/**
* @param {unknown} message
* @returns {Promise<unknown>}
*/
sendMessagePromise(message) {
return new Promise((resolve, reject) => {
try {
this.sendMessage(message, (response) => {
const error = this.getLastError();
if (error !== null) {
reject(error);
} else {
resolve(response);
}
});
} catch (error) {
reject(error);
}
});
}
/**
* @param {unknown} message
*/
sendMessageIgnoreResponse(message) {
this.sendMessage(message, () => {
// Clear the last error
this.getLastError();
});
}
/**
* @returns {?Error}
*/
getLastError() {
const {lastError} = chrome.runtime;
if (lastError) {
if (lastError instanceof Error) { return lastError; }
const {message} = lastError;
return new Error(typeof message === 'string' ? message : 'An unknown web extension error occured');
}
return null;
}
/** */
triggerUnloaded() {
if (this._unloaded) { return; }
this._unloaded = true;
this.trigger('unloaded', {});
}
/**
* Checks whether or not a URL is an extension URL.
* @param {string} url The URL to check.
* @returns {boolean} `true` if the URL is an extension URL, `false` otherwise.
*/
isExtensionUrl(url) {
return this._extensionBaseUrl !== null && url.startsWith(this._extensionBaseUrl);
}
}