From d80c620dfcfdcf851ffa45a6c395c1ffc54f9128 Mon Sep 17 00:00:00 2001 From: Toil <62353659+ilyhalight@users.noreply.github.com> Date: Fri, 4 Apr 2025 03:04:32 +0300 Subject: [PATCH] chore: made some improvements --- .webpack/ext.config.js | 85 +++++++++++++++++++++++++++++++++++++ .webpack/webpack.config.js | 82 ++++++++++++++++------------------- scripts/wiki-gen/locales.js | 10 +++-- src/config/config.js | 25 ++++++++--- src/global.d.ts | 10 ++--- src/headers.json | 1 - 6 files changed, 150 insertions(+), 63 deletions(-) create mode 100644 .webpack/ext.config.js diff --git a/.webpack/ext.config.js b/.webpack/ext.config.js new file mode 100644 index 00000000..034b5c90 --- /dev/null +++ b/.webpack/ext.config.js @@ -0,0 +1,85 @@ +import path from "node:path"; +import fs from "node:fs"; +import { fileURLToPath } from "url"; + +import webpack from "webpack"; + +import { styleLoaderInsertStyleElement } from "webpack-monkey/lib/client/css.js"; +import ESLintPlugin from "eslint-webpack-plugin"; +import TerserPlugin from "terser-webpack-plugin"; + +import configShared from "./shared.config.js"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.resolve(path.dirname(__filename), ".."); +const localesDir = path.resolve(__dirname, "src", "localization", "locales"); +const priorityLocales = ["auto", "en", "ru"]; + +const dev = process.env.NODE_ENV === "development"; +const availableLocales = getAvailableLocales(); + +function getAvailableLocales() { + const files = fs.readdirSync(localesDir); + const locales = files.reduce((result, file) => { + if (!file.endsWith(".json")) { + return result; + } + + const locale = file.replace(".json", ""); + if (priorityLocales.includes(locale)) { + return result; + } + + result.push(locale); + return result; + }, []); + + return [...priorityLocales, ...locales]; +} + +/** + * @type {import('webpack').Configuration} + */ +const config = { + mode: "production", + entry: path.resolve(__dirname, "src", "index.js"), + output: { + path: path.resolve(__dirname, "dist"), + filename: "ext.js", + }, + plugins: [ + new ESLintPlugin({ + configType: "flat", + }), + new webpack.optimize.LimitChunkCountPlugin({ + maxChunks: 1, + }), + new webpack.DefinePlugin({ + DEBUG_MODE: dev, + // DEBUG_MODE: true, + IS_BETA_VERSION: false, + AVAILABLE_LOCALES: JSON.stringify(availableLocales), + ...(() => { + if (!dev) { + return { + __MK_GLOBAL__: { + styleLoaderInsertStyleElement, + }, + }; + } + })(), + }), + new webpack.IgnorePlugin({ + resourceRegExp: /^node:crypto$/, + }), + ], + optimization: { + emitOnErrors: true, + moduleIds: "named", + minimize: false, + minimizer: [new TerserPlugin()], + }, + ...configShared, +}; + +export default config; diff --git a/.webpack/webpack.config.js b/.webpack/webpack.config.js index db2605e8..12a9fae6 100644 --- a/.webpack/webpack.config.js +++ b/.webpack/webpack.config.js @@ -16,59 +16,53 @@ import { sitesPeertube, sitesPoketube, sitesCoursehunterLike, + sitesRicktube, + // sitesMaterialious, } from "@vot.js/shared/alternativeUrls"; import configShared from "./shared.config.js"; import { repositoryUrl, contentUrl } from "../src/config/config.js"; -const dev = process.env.NODE_ENV === "development"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.resolve(path.dirname(__filename), ".."); const localesDir = path.resolve(__dirname, "src", "localization", "locales"); +const metaHeadersPath = path.resolve(__dirname, "src", "headers.json"); const priorityLocales = ["auto", "en", "ru"]; let isBeta = getHeaders().version.includes("beta"); -const availableLocales = getAvailableLocales(); +const availableLocales = await getAvailableLocales(); -console.log("development mode: ", dev); +// globals +const DEBUG_MODE = process.env.NODE_ENV === "development"; +const REPO_BRANCH = DEBUG_MODE || isBeta ? "dev" : "master"; +const REPO_UPDATE_BRANCH = isBeta ? "dev" : "master"; function getHeaders(lang = "") { const headersPath = lang ? path.resolve(localesDir, "headers", lang) - : path.resolve(__dirname, "src", "headers.json"); - return JSON.parse(fs.readFileSync(headersPath).toString()); + : metaHeadersPath; + return JSON.parse(fs.readFileSync(headersPath, "utf8")); } -function getAvailableLocales() { - const files = fs.readdirSync(localesDir); - const locales = files.reduce((result, file) => { - if (!file.endsWith(".json")) { - return result; - } - - const locale = file.replace(".json", ""); - if (priorityLocales.includes(locale)) { - return result; - } - - result.push(locale); - return result; - }, []); - +async function getAvailableLocales() { + const hashes = await fs.promises.readFile( + path.resolve(localesDir, "..", "hashes.json"), + "utf8", + ); + const content = JSON.parse(hashes); + const locales = Object.keys(content).filter( + (locale) => !priorityLocales.includes(locale), + ); return [...priorityLocales, ...locales]; } export default (env) => { - const build_type = env.build_type; - console.log("build type: ", build_type); + const BUILD_MINIFIED = env.build_type === "minify"; + console.log(`minified: ${BUILD_MINIFIED}, dev: ${DEBUG_MODE}. Building...`); - function getFilename() { - let name = "vot"; - if (build_type === "minify") { - name += "-min"; - } - - return name + ".user.js"; + let filename = "vot"; + if (BUILD_MINIFIED) { + filename += "-min"; } function altUrlsToMatch() { @@ -79,11 +73,13 @@ export default (env) => { sitesProxiTok, sitesPeertube, sitesPoketube, + sitesRicktube, + // sitesMaterialious, sitesCoursehunterLike, ] .map((sites) => sites.map((site) => { - const isSubdomain = site.match(/\./g)?.length > 1; + const isSubdomain = /\./g.exec(site)?.length > 1; return `*://${isSubdomain ? "" : "*."}${site}/*`; }), ) @@ -91,24 +87,21 @@ export default (env) => { } return monkey({ - mode: dev ? "development" : "production", + mode: DEBUG_MODE ? "development" : "production", ...configShared, entry: path.resolve(__dirname, "src", "index.js"), output: { path: path.resolve(__dirname, "dist"), - ...(!dev ? { filename: getFilename() } : {}), + ...(!DEBUG_MODE ? { filename: `${filename}.user.js` } : {}), }, monkey: { - debug: dev, + debug: DEBUG_MODE, meta: { - resolve: path.resolve(__dirname, "src", "headers.json"), + resolve: metaHeadersPath, transform({ meta }) { - const extFileName = getFilename().slice(0, -8); - const finalURL = `${contentUrl}/${ - isBeta ? "dev" : "master" - }/dist/${extFileName}.user.js`; + const finalURL = `${contentUrl}/${REPO_UPDATE_BRANCH}/dist/${filename}.user.js`; - meta.namespace = extFileName; + meta.namespace = filename; meta.homepageURL = repositoryUrl; meta.updateURL = meta.downloadURL = finalURL; meta.supportURL = `${repositoryUrl}/issues`; @@ -145,12 +138,11 @@ export default (env) => { maxChunks: 1, }), new webpack.DefinePlugin({ - DEBUG_MODE: dev, - // DEBUG_MODE: true, - IS_BETA_VERSION: isBeta, + DEBUG_MODE, AVAILABLE_LOCALES: JSON.stringify(availableLocales), + REPO_BRANCH: JSON.stringify(REPO_BRANCH), ...(() => { - if (!dev) { + if (!DEBUG_MODE) { return { __MK_GLOBAL__: { styleLoaderInsertStyleElement, @@ -166,7 +158,7 @@ export default (env) => { optimization: { emitOnErrors: true, moduleIds: "named", - minimize: build_type === "minify", + minimize: BUILD_MINIFIED, minimizer: [new TerserPlugin()], }, }); diff --git a/scripts/wiki-gen/locales.js b/scripts/wiki-gen/locales.js index 0f26913f..c406e952 100644 --- a/scripts/wiki-gen/locales.js +++ b/scripts/wiki-gen/locales.js @@ -1,3 +1,5 @@ +import { repositoryUrl } from "../../src/config/config"; + export default { limitations: { ru: "Ограничения", @@ -36,16 +38,16 @@ export default { en: "Translation of live broadcasts is not available", }, needBypassCSP: { - ru: 'Для гарантированной работы скрипта необходимо [включить настройку "Обход Media CSP"](https://github.com/ilyhalight/voice-over-translation/wiki/%5BRU%5D-FAQ) в расширение или удалить CSP другим способом', - en: 'To ensure that the script works, you need to [enable the "Bypass Media CSP" setting](https://github.com/ilyhalight/voice-over-translation/wiki/%5BEN%5D-FAQ) in the extension or delete the CSP in another way', + ru: `Для гарантированной работы скрипта необходимо [включить настройку "Обход Media CSP"](${repositoryUrl}/wiki/%5BRU%5D-FAQ) в расширение или удалить CSP другим способом`, + en: `To ensure that the script works, you need to [enable the "Bypass Media CSP" setting](${repositoryUrl}/wiki/%5BEN%5D-FAQ) in the extension or delete the CSP in another way`, }, cantTranslatePHPremium: { ru: "Недоступен перевод для PH Premium", en: "Translation is not available for PH Premium", }, needSetAccessToken: { - ru: "Для работы необходимо [установить Access Token](https://github.com/ilyhalight/voice-over-translation/wiki/%5BRU%5D-Where-to-get-Udemy-Access-Token%3F)", - en: "To work, you need to [set an Access Token](https://github.com/ilyhalight/voice-over-translation/wiki/%5BEN%5D-Where-to-get-Udemy-Access-Token%3F)", + ru: `Для работы необходимо [установить Access Token](${repositoryUrl}/wiki/%5BRU%5D-Where-to-get-Udemy-Access-Token%3F)"`, + en: `To work, you need to [set an Access Token](${repositoryUrl}/wiki/%5BEN%5D-Where-to-get-Udemy-Access-Token%3F)`, }, needBeLoggedIn: { ru: "Необходимо быть авторизованным на сайте", diff --git a/src/config/config.js b/src/config/config.js index f8bd792c..580cc479 100644 --- a/src/config/config.js +++ b/src/config/config.js @@ -2,18 +2,32 @@ const workerHost = "api.browser.yandex.ru"; /** * used for streaming + * + * @see https://github.com/FOSWLY/media-proxy */ const m3u8ProxyHost = "media-proxy.toil.cc/v1/proxy/m3u8"; +/** + * @see https://github.com/FOSWLY/vot-worker + */ const proxyWorkerHost = "vot-worker.toil.cc"; const votBackendUrl = "https://vot.toil.cc/v1"; -const contentUrl = - "https://raw.githubusercontent.com/ilyhalight/voice-over-translation"; -const repositoryUrl = "https://github.com/ilyhalight/voice-over-translation"; +/** + * @see https://github.com/FOSWLY/translate-backend + */ +const foswlyTranslateUrl = "https://translate.toil.cc/v2"; +const detectRustServerUrl = "https://rust-server-531j.onrender.com/detect"; + +const repoPath = "ilyhalight/voice-over-translation"; +const contentUrl = `https://raw.githubusercontent.com/${repoPath}`; +const repositoryUrl = `https://github.com/${repoPath}`; /** * 0.0 - 1.0 (0% - 100%) - default volume of the video with the translation */ const defaultAutoVolume = 0.15; +/** + * Max audio volume percentage (if available) + */ const maxAudioVolume = 900; /** * The number of repeated responses after which the message turns into "translation is delayed, please wait" @@ -22,11 +36,7 @@ const minLongWaitingCount = 5; const defaultTranslationService = "yandexbrowser"; const defaultDetectService = "yandexbrowser"; -const foswlyTranslateUrl = "https://translate.toil.cc/v2"; -const detectRustServerUrl = "https://rust-server-531j.onrender.com/detect"; - const nonProxyExtensions = ["Tampermonkey", "Violentmonkey"]; - const proxyOnlyCountries = ["UA", "LV", "LT"]; export { @@ -36,6 +46,7 @@ export { detectRustServerUrl, votBackendUrl, contentUrl, + repoPath, repositoryUrl, foswlyTranslateUrl, defaultTranslationService, diff --git a/src/global.d.ts b/src/global.d.ts index 9e18decc..ce3470e1 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -1,6 +1,4 @@ -declare global { - // DEFINED IN WEBPACK - const DEBUG_MODE: boolean; - const IS_BETA_VERSION: boolean; - const AVAILABLE_LOCALES: string[]; -} +// DEFINED IN WEBPACK +const DEBUG_MODE: boolean; +const AVAILABLE_LOCALES: string[]; +const REPO_BRANCH: "master" | "dev"; diff --git a/src/headers.json b/src/headers.json index 418c0173..d0791bae 100644 --- a/src/headers.json +++ b/src/headers.json @@ -76,7 +76,6 @@ "*://learning.sap.com/*", "*://*.watchporn.to/*", "*://*.linkedin.com/*", - "*://*.ricktube.ru/*", "*://*.incestflix.net/*", "*://*.incestflix.to/*", "*://*.porntn.com/*",