From b15a4e501f3f8dce1879f115637aa6b5c3108c64 Mon Sep 17 00:00:00 2001 From: gdpinheiro Date: Thu, 28 May 2026 23:54:30 -0300 Subject: [PATCH 1/5] feat: implement core initialization logic and runtime configuration for Userscript-Plus --- src/UserJS/main.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/UserJS/main.js b/src/UserJS/main.js index c0e4661..c2597b7 100644 --- a/src/UserJS/main.js +++ b/src/UserJS/main.js @@ -424,9 +424,10 @@ class i18nHandler { */ i18n$(key) { const { navigator } = _self; - const current = navigator.language.split('-')[0] ?? 'en'; + const lang = navigator.language.replace('-', '_'); + const current = lang.split('_')[0] ?? 'en'; try { - return i18nMap.get(current)?.[key] ?? 'Invalid Key'; + return i18nMap.get(lang)?.[key] ?? i18nMap.get(current)?.[key] ?? i18nMap.get('en')?.[key] ?? 'Invalid Key'; } catch (e) { err(e); return 'error'; @@ -435,7 +436,9 @@ class i18nHandler { get current() { const { navigator } = _self; - return navigator.language.split('-')[0] ?? 'en'; + const lang = navigator.language.replace('-', '_'); + const current = lang.split('_')[0] ?? 'en'; + return i18nMap.has(lang) ? lang : i18nMap.has(current) ? current : 'en'; } } const language = new i18nHandler(); From 5220ef96fee9ed8fba0c1a1f281662eb059c9e1c Mon Sep 17 00:00:00 2001 From: gdpinheiro Date: Fri, 29 May 2026 00:16:35 -0300 Subject: [PATCH 2/5] Fix popup failing to load when currentTab.url is undefined --- src/js/popup.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/js/popup.js b/src/js/popup.js index 4f6febe..505035f 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -2074,14 +2074,15 @@ async function init() { Object.assign(currentTab, tab); let url; try { - url = new URL(currentTab.url); + url = new URL(currentTab.url || BLANK_PAGE); tabURL.href = url.href || ''; } catch (ex) { err(ex); + url = new URL(BLANK_PAGE); } if (url !== undefined) { - const hostname = formatURL(normalizedHostname(tabURL.hostname)); + const hostname = url.href === BLANK_PAGE ? BLANK_PAGE : formatURL(normalizedHostname(tabURL.hostname)); const response = await sendMessage({ init: true, type: 'getData', From 0b890d7191cf455f99b2531485a516f80b8c03ca Mon Sep 17 00:00:00 2001 From: gdpinheiro Date: Fri, 29 May 2026 00:16:57 -0300 Subject: [PATCH 3/5] checkout: temporary commit for worktree checkout From 9ca1f44681eda8b556f4003369a17931892d1165 Mon Sep 17 00:00:00 2001 From: gdpinheiro Date: Fri, 29 May 2026 00:16:59 -0300 Subject: [PATCH 4/5] checkout: temporary commit for worktree checkout From db8f73944efbfac9cbf0fb3b4785f137da86820e Mon Sep 17 00:00:00 2001 From: gdpinheiro Date: Fri, 29 May 2026 00:44:15 -0300 Subject: [PATCH 5/5] feat: add Cross-browser extension utility, i18n support, and popup logic modules --- src/js/ext.js | 2 +- src/js/i18n.js | 9 ++++++--- src/js/popup.js | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/js/ext.js b/src/js/ext.js index ebd6ec7..986623f 100644 --- a/src/js/ext.js +++ b/src/js/ext.js @@ -1,5 +1,5 @@ export const webext = - self.browser instanceof Object && self.browser instanceof Element === false + self.browser instanceof Object && (typeof Element === 'undefined' || !(self.browser instanceof Element)) ? self.browser : self.chrome; export const runtime = webext.runtime; diff --git a/src/js/i18n.js b/src/js/i18n.js index c8a39a0..935bdc3 100644 --- a/src/js/i18n.js +++ b/src/js/i18n.js @@ -1,7 +1,7 @@ 'use strict'; const i18n = - self.browser instanceof Object && self.browser instanceof Element === false + self.browser instanceof Object && (typeof Element === 'undefined' || !(self.browser instanceof Element)) ? self.browser.i18n : self.chrome.i18n; @@ -31,12 +31,15 @@ class i18nHandler { const { i18n$, toDate, toNumber } = i18nHandler; const language = { i18n$, toDate, toNumber }; -const isBackgroundProcess = document && document.title === 'Magic UserJS+ Background Page'; +// In a Service Worker, `document` is not defined — treat as background process to skip DOM rendering. +const isBackgroundProcess = + typeof document === 'undefined' || document.title === 'Magic UserJS+ Background Page'; if (isBackgroundProcess !== true) { // Helper to deal with the i18n'ing of HTML files. i18n.render = function (context) { - const docu = document; + const docu = typeof document !== 'undefined' ? document : null; + if (!docu) return; const root = context || docu; for (const elem of root.querySelectorAll('[data-i18n]')) { const text = i18n$(elem.getAttribute('data-i18n')); diff --git a/src/js/popup.js b/src/js/popup.js index 505035f..8f781bf 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -28,7 +28,7 @@ import { BaseContainer, BaseList } from './container.js'; /** * @type { import("../typings/types").config } */ -let cfg = {}; +let cfg = JSON.parse(JSON.stringify(DEFAULT_CONFIG)); /******************************************************************************/ /** @@ -1138,7 +1138,9 @@ ael(main, 'click', async (evt) => { openInTab(dataset.webpage); } else if (cmd === 'fullscreen') { const tab = container.Tabs.getActive(); - openInTab(`${location.href}?host=${tab.dataset.host}`); + if (tab) { + openInTab(`${location.href}?host=${tab.dataset.host}`); + } } else if (cmd === 'navigation') { for (const e of qsA('mujs-btn', target.parentElement)) { if (dom.cl.has(e, 'nav')) continue;