browser-extension-settings 0.8.2 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/settings.ts +23 -26
  2. package/package.json +4 -4
package/lib/settings.ts CHANGED
@@ -25,14 +25,6 @@ import { createSwitchOption } from "./switch"
25
25
  import { besVersion } from "./common"
26
26
  import { i, resetI18n, localeNames } from "./messages/index"
27
27
 
28
- // Declare GM global variable for userscript environment
29
- // eslint-disable-next-line @typescript-eslint/naming-convention
30
- declare const GM:
31
- | {
32
- registerMenuCommand?: (name: string, function_: () => void) => void
33
- }
34
- | undefined
35
-
36
28
  const prefix = "browser_extension_settings_v2_"
37
29
 
38
30
  type SettingsOptions = {
@@ -123,11 +115,16 @@ let settingsTable: SettingsTable = {}
123
115
  let settings = {}
124
116
  async function getSettings() {
125
117
  return (
126
- ((await getValue(storageKey)) as Record<string, unknown> | undefined) ?? {}
118
+ (await getValue<Record<string, boolean | string | undefined>>(
119
+ storageKey
120
+ )) ?? {}
127
121
  )
128
122
  }
129
123
 
130
- async function saveSettingsValue(key: string, value: any) {
124
+ async function saveSettingsValue(
125
+ key: string,
126
+ value: boolean | string | undefined
127
+ ) {
131
128
  const settings = await getSettings()
132
129
  settings[key] =
133
130
  settingsTable[key] && settingsTable[key].defaultValue === value
@@ -160,10 +157,12 @@ export async function saveSettingsValues(
160
157
  await setValue(storageKey, settings)
161
158
  }
162
159
 
163
- export function getSettingsValue(key: string): boolean | string | undefined {
160
+ export function getSettingsValue<T = boolean | string | undefined>(
161
+ key: string
162
+ ): T {
164
163
  return Object.hasOwn(settings, key)
165
- ? (settings[key] as boolean | string | undefined)
166
- : (settingsTable[key]?.defaultValue as boolean | string | undefined)
164
+ ? (settings[key] as T)
165
+ : (settingsTable[key]?.defaultValue as T)
167
166
  }
168
167
 
169
168
  const closeModal = () => {
@@ -246,7 +245,7 @@ async function updateOptions() {
246
245
  root
247
246
  ) as HTMLInputElement
248
247
  if (checkbox) {
249
- checkbox.checked = getSettingsValue(key) as boolean
248
+ checkbox.checked = getSettingsValue(key)
250
249
  }
251
250
 
252
251
  break
@@ -273,7 +272,7 @@ async function updateOptions() {
273
272
  root
274
273
  ) as HTMLTextAreaElement
275
274
  if (textArea) {
276
- textArea.value = getSettingsValue(key) as string
275
+ textArea.value = getSettingsValue(key)
277
276
  }
278
277
 
279
278
  break
@@ -288,7 +287,7 @@ async function updateOptions() {
288
287
 
289
288
  if (typeof settingsOptions.onViewUpdate === "function") {
290
289
  const settingsMain = createSettingsElement()
291
- settingsOptions.onViewUpdate(settingsMain)
290
+ settingsOptions.onViewUpdate(settingsMain!)
292
291
  }
293
292
  }
294
293
 
@@ -379,11 +378,10 @@ function createSettingsElement() {
379
378
  const getOptionGroup = (index: number) => {
380
379
  if (index > optionGroups.length) {
381
380
  for (let i = optionGroups.length; i < index; i++) {
382
- optionGroups.push(
383
- addElement(settingsMain!, "div", {
384
- class: "option_groups",
385
- })
386
- )
381
+ const optionGroup = addElement(settingsMain, "div", {
382
+ class: "option_groups",
383
+ })
384
+ if (optionGroup) optionGroups.push(optionGroup)
387
385
  }
388
386
  }
389
387
 
@@ -537,7 +535,7 @@ function createSettingsElement() {
537
535
 
538
536
  if (settingsOptions.footer) {
539
537
  const footer = addElement(settingsMain, "footer")
540
- footer.innerHTML = createHTML(
538
+ footer!.innerHTML = createHTML(
541
539
  typeof settingsOptions.footer === "string"
542
540
  ? settingsOptions.footer
543
541
  : `<p>Made with ❤️ by
@@ -632,8 +630,7 @@ let lastLocale: string | undefined
632
630
  // Reset settings UI on init and locale change
633
631
  // eslint-disable-next-line @typescript-eslint/naming-convention
634
632
  const resetSettingsUI = (optionsProvider: () => SettingsOptions) => {
635
- lastLocale =
636
- (getSettingsValue("locale") as string | undefined) || getPrefferedLocale()
633
+ lastLocale = getSettingsValue("locale") || getPrefferedLocale()
637
634
  resetI18n(lastLocale)
638
635
 
639
636
  const options = optionsProvider()
@@ -663,14 +660,14 @@ const resetSettingsUI = (optionsProvider: () => SettingsOptions) => {
663
660
  }
664
661
 
665
662
  export const initSettings = async (optionsProvider: () => SettingsOptions) => {
666
- addValueChangeListener(storageKey, async () => {
663
+ await addValueChangeListener(storageKey, async () => {
667
664
  settings = await getSettings()
668
665
  // console.log(JSON.stringify(settings, null, 2))
669
666
  await updateOptions()
670
667
  // addSideMenu()
671
668
 
672
669
  const newLocale =
673
- (getSettingsValue("locale") as string | undefined) || getPrefferedLocale()
670
+ getSettingsValue<string | undefined>("locale") || getPrefferedLocale()
674
671
  // console.log("lastLocale:", lastLocale, "newLocale:", newLocale)
675
672
  if (lastLocale !== newLocale) {
676
673
  const isShown = isSettingsShown()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-settings",
3
- "version": "0.8.2",
3
+ "version": "0.8.4",
4
4
  "description": "Settings module for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.ts",
@@ -31,13 +31,13 @@
31
31
  "homepage": "https://github.com/utags/browser-extension-settings#readme",
32
32
  "dependencies": {
33
33
  "browser-extension-i18n": "^0.1.3",
34
- "browser-extension-storage": "^0.1.2",
35
- "browser-extension-utils": "^0.2.2"
34
+ "browser-extension-storage": "^0.2.8",
35
+ "browser-extension-utils": "^0.3.3"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/chrome": "^0.1.32",
39
39
  "@vitest/coverage-v8": "^4.0.16",
40
- "jsdom": "^27.3.0",
40
+ "jsdom": "^27.4.0",
41
41
  "npm-run-all": "^4.1.5",
42
42
  "prettier": "^3.7.4",
43
43
  "typescript": "^5.9.3",