browser-extension-settings 0.0.7 → 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.
package/lib/common.ts CHANGED
@@ -1,3 +1,3 @@
1
- export const besVersion = 13
1
+ export const besVersion = 14
2
2
  export const openButton = `<svg viewBox="0 0 60.2601318359375 84.8134765625" version="1.1" xmlns="http://www.w3.org/2000/svg" class=" glyph-box" style="height: 9.62969px; width: 6.84191px;"><g transform="matrix(1 0 0 1 -6.194965820312518 77.63671875)"><path d="M66.4551-35.2539C66.4551-36.4746 65.9668-37.5977 65.0391-38.4766L26.3672-76.3672C25.4883-77.1973 24.4141-77.6367 23.1445-77.6367C20.6543-77.6367 18.7012-75.7324 18.7012-73.1934C18.7012-71.9727 19.1895-70.8496 19.9707-70.0195L55.5176-35.2539L19.9707-0.488281C19.1895 0.341797 18.7012 1.41602 18.7012 2.68555C18.7012 5.22461 20.6543 7.12891 23.1445 7.12891C24.4141 7.12891 25.4883 6.68945 26.3672 5.81055L65.0391-32.0312C65.9668-32.959 66.4551-34.0332 66.4551-35.2539Z"></path></g></svg>`
3
3
  export const openInNewTabButton = `<svg viewBox="0 0 72.127685546875 72.2177734375" version="1.1" xmlns="http://www.w3.org/2000/svg" class=" glyph-box" style="height: 8.19958px; width: 8.18935px;"><g transform="matrix(1 0 0 1 -12.451127929687573 71.3388671875)"><path d="M84.5703-17.334L84.5215-66.4551C84.5215-69.2383 82.7148-71.1914 79.7852-71.1914L30.6641-71.1914C27.9297-71.1914 26.0742-69.0918 26.0742-66.748C26.0742-64.4043 28.1738-62.4023 30.4688-62.4023L47.4609-62.4023L71.2891-63.1836L62.207-55.2246L13.8184-6.73828C12.9395-5.85938 12.4512-4.73633 12.4512-3.66211C12.4512-1.31836 14.5508 0.878906 16.9922 0.878906C18.1152 0.878906 19.1895 0.488281 20.0684-0.439453L68.5547-48.877L76.6113-58.0078L75.7324-35.2051L75.7324-17.1387C75.7324-14.8438 77.7344-12.6953 80.127-12.6953C82.4707-12.6953 84.5703-14.6973 84.5703-17.334Z"></path></g></svg>`
package/lib/settings.ts CHANGED
@@ -6,13 +6,13 @@ import {
6
6
  import {
7
7
  $,
8
8
  $$,
9
- addClass,
10
9
  addElement,
11
10
  addEventListener,
12
11
  addStyle,
13
- createElement,
14
12
  doc,
13
+ parseInt10,
15
14
  removeEventListener,
15
+ runWhenBodyExists,
16
16
  } from "browser-extension-utils"
17
17
  import styleText from "data-text:./style.scss"
18
18
  import { createSwitchOption } from "./switch"
@@ -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,17 +180,12 @@ 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() {
165
186
  const container = $(`.${prefix}container`)
166
187
  if (container) {
167
- const theVersion = Number.parseInt(container.dataset.besVersion || "0", 10)
188
+ const theVersion = parseInt10(container.dataset.besVersion, 0)
168
189
  if (theVersion < besVersion) {
169
190
  container.id = settingsContainerId
170
191
  container.dataset.besVersion = String(besVersion)
@@ -192,11 +213,6 @@ function getSettingsWrapper() {
192
213
  }
193
214
 
194
215
  function initExtensionList() {
195
- if (!doc.body) {
196
- setTimeout(initExtensionList, 100)
197
- return
198
- }
199
-
200
216
  const wrapper = getSettingsWrapper()
201
217
  if (!$(".extension_list_container", wrapper)) {
202
218
  const list = createExtensionList([])
@@ -236,11 +252,29 @@ function createSettingsElement() {
236
252
  addElement(settingsMain, "h2", { textContent: settingsOptions.title })
237
253
  }
238
254
 
239
- 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
+
240
270
  for (const key in settingsTable) {
241
271
  if (Object.hasOwn(settingsTable, key)) {
242
272
  const item = settingsTable[key]
243
- 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") {
244
278
  const switchOption = createSwitchOption(item.title, {
245
279
  async onchange(event: Event) {
246
280
  if (event.target) {
@@ -251,35 +285,31 @@ function createSettingsElement() {
251
285
 
252
286
  switchOption.dataset.key = key
253
287
 
254
- 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
+ })
255
308
  }
256
309
  }
257
310
  }
258
311
 
259
- const options2 = addElement(settingsMain, "div", {
260
- class: "option_groups",
261
- })
262
- let timeoutId: number | undefined
263
- addElement(options2, "textarea", {
264
- placeholder: `/* Custom rules for internal URLs, matching URLs will be opened in new tabs */`,
265
- onkeyup(event: Event) {
266
- const textArea = event.target as HTMLTextAreaElement
267
- if (timeoutId) {
268
- clearTimeout(timeoutId)
269
- timeoutId = undefined
270
- }
271
-
272
- timeoutId = setTimeout(async () => {
273
- const host = location.host
274
- if (textArea) {
275
- await saveSattingsValue(
276
- `customRulesForCurrentSite_${host}`,
277
- textArea.value.trim()
278
- )
279
- }
280
- }, 100)
281
- },
282
- })
312
+ const options2 = getOptionGroup(2)
283
313
 
284
314
  const tip = addElement(options2, "div", {
285
315
  class: "tip",
@@ -321,11 +351,6 @@ function createSettingsElement() {
321
351
  }
322
352
 
323
353
  function addSideMenu() {
324
- if (!doc.body) {
325
- setTimeout(addSideMenu, 100)
326
- return
327
- }
328
-
329
354
  const menu =
330
355
  $("#browser_extension_side_menu") ||
331
356
  addElement(doc.body, "div", {
@@ -336,7 +361,7 @@ function addSideMenu() {
336
361
  const button = $("button[data-bes-version]", menu)
337
362
 
338
363
  if (button) {
339
- const theVersion = Number.parseInt(button.dataset.besVersion || "0", 10)
364
+ const theVersion = parseInt10(button.dataset.besVersion, 0)
340
365
  if (theVersion >= besVersion) {
341
366
  return
342
367
  }
@@ -372,6 +397,7 @@ export const initSettings = async (options: SettingsOptions) => {
372
397
  settingsTable = options.settingsTable || {}
373
398
  addValueChangeListener(storageKey, async () => {
374
399
  settings = await getSettings()
400
+ // console.log(JSON.stringify(settings, null, 2))
375
401
  await updateOptions()
376
402
  if (typeof options.onValueChange === "function") {
377
403
  options.onValueChange()
@@ -380,6 +406,8 @@ export const initSettings = async (options: SettingsOptions) => {
380
406
 
381
407
  settings = await getSettings()
382
408
  addStyle(getSettingsStyle())
383
- initExtensionList()
384
- addSideMenu()
409
+ runWhenBodyExists(() => {
410
+ initExtensionList()
411
+ addSideMenu()
412
+ })
385
413
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-settings",
3
- "version": "0.0.7",
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",
@@ -26,7 +26,7 @@
26
26
  "homepage": "https://github.com/utags/browser-extension-settings#readme",
27
27
  "dependencies": {
28
28
  "browser-extension-storage": "^0.1.2",
29
- "browser-extension-utils": "^0.1.4"
29
+ "browser-extension-utils": "^0.1.7"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/chrome": "^0.0.237",