Update local messages script now supports message strings or an object

Made error logging in options and content scripts consistent
Sorted translation labels in options
This commit is contained in:
Jonny Buchanan 2025-06-09 08:42:44 +10:00
parent c577422094
commit c401ecb219
3 changed files with 27 additions and 20 deletions

View File

@ -18,10 +18,6 @@ let channel
//#endregion //#endregion
//#region Functions //#region Functions
function error(...messages) {
console.error('❌ [content]', ...messages)
}
// Can't import this from storage.js in a content script // Can't import this from storage.js in a content script
function get(keys) { function get(keys) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -99,14 +95,14 @@ async function storeConfigChangesFromPageScript({data: changes}) {
configToStore.settings = {...settings, ...changes.settings} configToStore.settings = {...settings, ...changes.settings}
} }
} catch(e) { } catch(e) {
error('error merging settings change from page script', e) console.error('[content] error merging settings change from page script', e)
} }
chrome.storage.local.onChanged.removeListener(onStorageChanged) chrome.storage.local.onChanged.removeListener(onStorageChanged)
try { try {
await set(configToStore) await set(configToStore)
} catch(e) { } catch(e) {
error('error storing settings change from page script', e) console.error('[content] error storing settings change from page script', e)
} finally { } finally {
chrome.storage.local.onChanged.addListener(onStorageChanged) chrome.storage.local.onChanged.addListener(onStorageChanged)
} }

View File

@ -25,12 +25,12 @@ for (let optionValue of [
for (let translationId of [ for (let translationId of [
'addAddMutedWordMenuItemLabel_desktop', 'addAddMutedWordMenuItemLabel_desktop',
'addAddMutedWordMenuItemLabel_mobile', 'addAddMutedWordMenuItemLabel_mobile',
'defaultToFollowingLabel',
'customCssLabel', 'customCssLabel',
'debugInfo', 'debugInfo',
'debugLabel', 'debugLabel',
'debugLogTimelineStatsLabel', 'debugLogTimelineStatsLabel',
'debugOptionsLabel', 'debugOptionsLabel',
'defaultToFollowingLabel',
'defaultToLatestSearchLabel', 'defaultToLatestSearchLabel',
'disableHomeTimelineInfo', 'disableHomeTimelineInfo',
'disableHomeTimelineLabel', 'disableHomeTimelineLabel',
@ -74,6 +74,8 @@ for (let translationId of [
'hideMonetizationNavLabel', 'hideMonetizationNavLabel',
'hideMoreSlideOutMenuItemsOptionsLabel_desktop', 'hideMoreSlideOutMenuItemsOptionsLabel_desktop',
'hideMoreSlideOutMenuItemsOptionsLabel_mobile', 'hideMoreSlideOutMenuItemsOptionsLabel_mobile',
'hidePremiumRepliesLabel',
'hidePremiumUpsellsLabel',
'hideProfileHeaderMetricsLabel', 'hideProfileHeaderMetricsLabel',
'hideProfileRetweetsLabel', 'hideProfileRetweetsLabel',
'hideQuoteTweetMetricsLabel', 'hideQuoteTweetMetricsLabel',
@ -88,8 +90,6 @@ for (let translationId of [
'hideTimelineTweetBoxLabel', 'hideTimelineTweetBoxLabel',
'hideToggleNavigationLabel', 'hideToggleNavigationLabel',
'hideTweetAnalyticsLinksLabel', 'hideTweetAnalyticsLinksLabel',
'hidePremiumRepliesLabel',
'hidePremiumUpsellsLabel',
'hideUnavailableQuoteTweetsLabel', 'hideUnavailableQuoteTweetsLabel',
'hideUnusedUiItemsOptionsLabel', 'hideUnusedUiItemsOptionsLabel',
'hideVerifiedTabsLabel', 'hideVerifiedTabsLabel',
@ -101,6 +101,8 @@ for (let translationId of [
'mutableQuoteTweetsLabel', 'mutableQuoteTweetsLabel',
'navBaseFontSizeLabel', 'navBaseFontSizeLabel',
'navDensityLabel', 'navDensityLabel',
'premiumBlueChecksLabel',
'premiumBlueChecksOption_replace',
'preventNextVideoAutoplayInfo', 'preventNextVideoAutoplayInfo',
'preventNextVideoAutoplayLabel', 'preventNextVideoAutoplayLabel',
'quoteTweetsLabel', 'quoteTweetsLabel',
@ -109,16 +111,16 @@ for (let translationId of [
'reduceEngagementOptionsLabel', 'reduceEngagementOptionsLabel',
'reducedInteractionModeInfo', 'reducedInteractionModeInfo',
'reducedInteractionModeLabel', 'reducedInteractionModeLabel',
'revertXBrandingLabel',
'restoreLinkHeadlinesLabel', 'restoreLinkHeadlinesLabel',
'restoreOtherInteractionLinksLabel', 'restoreOtherInteractionLinksLabel',
'restoreQuoteTweetsLinkLabel', 'restoreQuoteTweetsLinkLabel',
'restoreTweetSourceLabel', 'restoreTweetSourceLabel',
'retweetsLabel', 'retweetsLabel',
'showPremiumReplyFollowersCountAmountLabel', 'revertXBrandingLabel',
'showBookmarkButtonUnderFocusedTweetsLabel', 'showBookmarkButtonUnderFocusedTweetsLabel',
'showPremiumReplyBusinessLabel', 'showPremiumReplyBusinessLabel',
'showPremiumReplyFollowedByLabel', 'showPremiumReplyFollowedByLabel',
'showPremiumReplyFollowersCountAmountLabel',
'showPremiumReplyFollowingLabel', 'showPremiumReplyFollowingLabel',
'showPremiumReplyGovernmentLabel', 'showPremiumReplyGovernmentLabel',
'showRelevantPeopleLabel', 'showRelevantPeopleLabel',
@ -127,8 +129,6 @@ for (let translationId of [
'tweakNewLayoutInfo', 'tweakNewLayoutInfo',
'tweakNewLayoutLabel', 'tweakNewLayoutLabel',
'tweakQuoteTweetsPageLabel', 'tweakQuoteTweetsPageLabel',
'premiumBlueChecksLabel',
'premiumBlueChecksOption_replace',
'uiImprovementsOptionsLabel', 'uiImprovementsOptionsLabel',
'uiTweaksOptionsLabel', 'uiTweaksOptionsLabel',
'unblurSensitiveContentLabel', 'unblurSensitiveContentLabel',
@ -156,7 +156,13 @@ for (let amount of [1_000, 10_000, 100_000, 1_000_000]) {
} }
//#endregion //#endregion
const INTERNAL_CONFIG_OPTIONS = new Set(['enabled', 'debug', 'debugLogTimelineStats']) /**
* Internal options which map directly to a form element.
* @type {import('./types').StoredConfigKey[]}
*/
const INTERNAL_CONFIG_FORM_KEYS = ['enabled', 'debug', 'debugLogTimelineStats']
/** @type {Set<string>} */
const INTERNAL_CONFIG_FORM_KEYSET = new Set(INTERNAL_CONFIG_FORM_KEYS)
/** @type {boolean} */ /** @type {boolean} */
let desktop let desktop
@ -290,7 +296,7 @@ function onFormChanged(/** @type {Event} */ e) {
let $el = /** @type {HTMLInputElement} */ (e.target) let $el = /** @type {HTMLInputElement} */ (e.target)
if ($el.type == 'checkbox') { if ($el.type == 'checkbox') {
if (INTERNAL_CONFIG_OPTIONS.has($el.name)) { if (INTERNAL_CONFIG_FORM_KEYSET.has($el.name)) {
config[$el.name] = changedConfig[$el.name] = $el.checked config[$el.name] = changedConfig[$el.name] = $el.checked
} }
else if (checkboxGroups.has($el.name)) { else if (checkboxGroups.has($el.name)) {
@ -362,7 +368,7 @@ function shouldDisplayMutedQuotes() {
async function storeConfigChanges(changes) { async function storeConfigChanges(changes) {
/** @type {Partial<import("./types").StoredConfig>} */ /** @type {Partial<import("./types").StoredConfig>} */
let changesToStore = {} let changesToStore = {}
for (let key of INTERNAL_CONFIG_OPTIONS) { for (let key of Object.keys(defaultConfig)) {
if (Object.hasOwn(changes, key)) { if (Object.hasOwn(changes, key)) {
changesToStore[key] = changes[key] changesToStore[key] = changes[key]
} }
@ -372,9 +378,13 @@ async function storeConfigChanges(changes) {
let {settings: storedSettings} = await get({settings: {}}) let {settings: storedSettings} = await get({settings: {}})
changesToStore.settings = {...storedSettings, ...changes.settings} changesToStore.settings = {...storedSettings, ...changes.settings}
} }
} catch(e) {
console.error('[options] error merging settings change', e)
}
try {
await set(changesToStore) await set(changesToStore)
} catch(error) { } catch(e) {
console.error('error storing settings changes from options page', String(error)) console.error('[options] error storing config change', e)
} }
} }
@ -474,7 +484,7 @@ function updateMutedQuotesDisplay() {
} }
function updateFormControls() { function updateFormControls() {
for (let key of INTERNAL_CONFIG_OPTIONS) { for (let key of INTERNAL_CONFIG_FORM_KEYSET) {
updateFormControl($form.elements[key], config[key]) updateFormControl($form.elements[key], config[key])
} }
for (let key of Object.keys(config.settings)) { for (let key of Object.keys(config.settings)) {

View File

@ -33,7 +33,8 @@ for (let [messageProp, translations] of Object.entries(translationsJson)) {
) )
} }
let messagesJson = localeMessagesJson.get(localeCode) let messagesJson = localeMessagesJson.get(localeCode)
messagesJson[messageProp] = {...messagesJson[messageProp], message} let update = typeof message == 'string' ? {message} : message
messagesJson[messageProp] = {...messagesJson[messageProp], ...update}
} }
} }