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(); 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 4f6febe..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; @@ -2074,14 +2076,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',