browser-extension-settings 0.0.8 → 0.1.0

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 +75 -39
  2. package/package.json +1 -1
package/lib/settings.ts CHANGED
@@ -41,12 +41,16 @@ type SettingsTable = Record<string, SettingsSwitchItem | SettingsInputItem>
41
41
  type SettingsSwitchItem = {
42
42
  title: string
43
43
  defaultValue: boolean
44
+ type?: "switch"
45
+ group?: number
44
46
  }
45
47
 
46
48
  type SettingsInputItem = {
47
49
  title: string
48
50
  defaultValue: string
51
+ placeholder?: string
49
52
  type: string
53
+ group?: number
50
54
  }
51
55
 
52
56
  type RelatedExtension = {
@@ -93,8 +97,8 @@ async function saveSattingsValue(key: string, value: any) {
93
97
 
94
98
  export function getSettingsValue(key: string): boolean | string | undefined {
95
99
  return Object.hasOwn(settings, key)
96
- ? settings[key]
97
- : settingsTable[key]?.defaultValue
100
+ ? (settings[key] as boolean | string | undefined)
101
+ : (settingsTable[key]?.defaultValue as boolean | string | undefined)
98
102
  }
99
103
 
100
104
  const closeModal = () => {
@@ -136,11 +140,33 @@ async function updateOptions() {
136
140
 
137
141
  for (const key in settingsTable) {
138
142
  if (Object.hasOwn(settingsTable, key)) {
139
- const checkbox = $(
140
- `#${settingsElementId} .option_groups .switch_option[data-key="${key}"] input`
141
- )
142
- if (checkbox) {
143
- checkbox.checked = getSettingsValue(key)
143
+ const item = settingsTable[key]
144
+ const type = item.type || "switch"
145
+
146
+ // console.log(key, type)
147
+ switch (type) {
148
+ case "switch": {
149
+ const checkbox = $(
150
+ `#${settingsElementId} .option_groups .switch_option[data-key="${key}"] input`
151
+ ) as HTMLInputElement
152
+ if (checkbox) {
153
+ checkbox.checked = getSettingsValue(key) as boolean
154
+ }
155
+
156
+ break
157
+ }
158
+
159
+ case "textarea": {
160
+ const textArea = $(
161
+ `#${settingsElementId} .option_groups textarea[data-key="${key}"]`
162
+ ) as HTMLTextAreaElement
163
+ textArea.value = getSettingsValue(key) as string
164
+ break
165
+ }
166
+
167
+ default: {
168
+ break
169
+ }
144
170
  }
145
171
  }
146
172
  }
@@ -154,11 +180,6 @@ async function updateOptions() {
154
180
  ? "block"
155
181
  : "none"
156
182
  }
157
-
158
- const customStyleValue = $(`#${settingsElementId} .option_groups textarea`)
159
- if (customStyleValue) {
160
- customStyleValue.value = settings[`customRulesForCurrentSite_${host}`] || ""
161
- }
162
183
  }
163
184
 
164
185
  function getSettingsContainer() {
@@ -231,11 +252,29 @@ function createSettingsElement() {
231
252
  addElement(settingsMain, "h2", { textContent: settingsOptions.title })
232
253
  }
233
254
 
234
- const options = addElement(settingsMain, "div", { class: "option_groups" })
255
+ const optionGroups: HTMLElement[] = []
256
+ const getOptionGroup = (index: number) => {
257
+ if (index > optionGroups.length) {
258
+ for (let i = optionGroups.length; i < index; i++) {
259
+ optionGroups.push(
260
+ addElement(settingsMain!, "div", {
261
+ class: "option_groups",
262
+ })
263
+ )
264
+ }
265
+ }
266
+
267
+ return optionGroups[index - 1]
268
+ }
269
+
235
270
  for (const key in settingsTable) {
236
271
  if (Object.hasOwn(settingsTable, key)) {
237
272
  const item = settingsTable[key]
238
- if (!item.type || item.type === "switch") {
273
+ const type = item.type || "switch"
274
+ const group = item.group || 1
275
+ const optionGroup = getOptionGroup(group)
276
+ // console.log(key, item, type, group)
277
+ if (type === "switch") {
239
278
  const switchOption = createSwitchOption(item.title, {
240
279
  async onchange(event: Event) {
241
280
  if (event.target) {
@@ -246,35 +285,31 @@ function createSettingsElement() {
246
285
 
247
286
  switchOption.dataset.key = key
248
287
 
249
- addElement(options, switchOption)
288
+ addElement(optionGroup, switchOption)
289
+ } else if (type === "textarea") {
290
+ let timeoutId: number | undefined
291
+ addElement(optionGroup, "textarea", {
292
+ "data-key": key,
293
+ placeholder: (item as SettingsInputItem).placeholder || "",
294
+ onkeyup(event: Event) {
295
+ const textArea = event.target as HTMLTextAreaElement
296
+ if (timeoutId) {
297
+ clearTimeout(timeoutId)
298
+ timeoutId = undefined
299
+ }
300
+
301
+ timeoutId = setTimeout(async () => {
302
+ if (textArea) {
303
+ await saveSattingsValue(key, textArea.value.trim())
304
+ }
305
+ }, 100)
306
+ },
307
+ })
250
308
  }
251
309
  }
252
310
  }
253
311
 
254
- const options2 = addElement(settingsMain, "div", {
255
- class: "option_groups",
256
- })
257
- let timeoutId: number | undefined
258
- addElement(options2, "textarea", {
259
- placeholder: `/* Custom rules for internal URLs, matching URLs will be opened in new tabs */`,
260
- onkeyup(event: Event) {
261
- const textArea = event.target as HTMLTextAreaElement
262
- if (timeoutId) {
263
- clearTimeout(timeoutId)
264
- timeoutId = undefined
265
- }
266
-
267
- timeoutId = setTimeout(async () => {
268
- const host = location.host
269
- if (textArea) {
270
- await saveSattingsValue(
271
- `customRulesForCurrentSite_${host}`,
272
- textArea.value.trim()
273
- )
274
- }
275
- }, 100)
276
- },
277
- })
312
+ const options2 = getOptionGroup(2)
278
313
 
279
314
  const tip = addElement(options2, "div", {
280
315
  class: "tip",
@@ -362,6 +397,7 @@ export const initSettings = async (options: SettingsOptions) => {
362
397
  settingsTable = options.settingsTable || {}
363
398
  addValueChangeListener(storageKey, async () => {
364
399
  settings = await getSettings()
400
+ // console.log(JSON.stringify(settings, null, 2))
365
401
  await updateOptions()
366
402
  if (typeof options.onValueChange === "function") {
367
403
  options.onValueChange()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-settings",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
4
4
  "description": "Settings module for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.ts",