browser-extension-settings 0.3.0 → 0.4.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 +1 -1
- package/lib/index.ts +2 -2
- package/lib/settings.ts +71 -9
- package/lib/style.scss +31 -2
- package/lib/switch.ts +20 -2
- package/package.json +2 -2
package/lib/common.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const besVersion =
|
|
1
|
+
export const besVersion = 40
|
|
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>`
|
|
4
4
|
export const settingButton = `<svg viewBox="0 0 16 16" version="1.1">
|
package/lib/index.ts
CHANGED
package/lib/settings.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
doc,
|
|
14
14
|
parseInt10,
|
|
15
15
|
removeEventListener,
|
|
16
|
-
|
|
16
|
+
runWhenDomReady,
|
|
17
17
|
runWhenHeadExists,
|
|
18
18
|
} from "browser-extension-utils"
|
|
19
19
|
import styleText from "data-text:./style.scss"
|
|
@@ -41,11 +41,16 @@ type SettingsOptions = {
|
|
|
41
41
|
|
|
42
42
|
type SettingsTable = Record<
|
|
43
43
|
string,
|
|
44
|
-
|
|
44
|
+
| SettingsSwitchItem
|
|
45
|
+
| SettingsInputItem
|
|
46
|
+
| SettingsActionItem
|
|
47
|
+
| SettingsSelectItem
|
|
48
|
+
| SettingsTipItem
|
|
45
49
|
>
|
|
46
50
|
|
|
47
51
|
type SettingsSwitchItem = {
|
|
48
52
|
title: string
|
|
53
|
+
icon?: string
|
|
49
54
|
defaultValue: boolean
|
|
50
55
|
type?: "switch"
|
|
51
56
|
group?: number
|
|
@@ -53,6 +58,7 @@ type SettingsSwitchItem = {
|
|
|
53
58
|
|
|
54
59
|
type SettingsInputItem = {
|
|
55
60
|
title: string
|
|
61
|
+
icon?: string
|
|
56
62
|
defaultValue: string
|
|
57
63
|
placeholder?: string
|
|
58
64
|
type: string
|
|
@@ -61,6 +67,7 @@ type SettingsInputItem = {
|
|
|
61
67
|
|
|
62
68
|
type SettingsActionItem = {
|
|
63
69
|
title: string
|
|
70
|
+
icon?: string
|
|
64
71
|
type: string
|
|
65
72
|
onclick?: () => void
|
|
66
73
|
url?: string
|
|
@@ -68,8 +75,18 @@ type SettingsActionItem = {
|
|
|
68
75
|
defaultValue?: any
|
|
69
76
|
}
|
|
70
77
|
|
|
78
|
+
type SettingsSelectItem = {
|
|
79
|
+
title: string
|
|
80
|
+
icon?: string
|
|
81
|
+
type: "select"
|
|
82
|
+
options: { string: { string: any } }
|
|
83
|
+
group?: number
|
|
84
|
+
defaultValue?: any
|
|
85
|
+
}
|
|
86
|
+
|
|
71
87
|
type SettingsTipItem = {
|
|
72
88
|
title: string
|
|
89
|
+
icon?: string
|
|
73
90
|
type: string
|
|
74
91
|
tipContent: string
|
|
75
92
|
group?: number
|
|
@@ -108,7 +125,7 @@ async function getSettings() {
|
|
|
108
125
|
)
|
|
109
126
|
}
|
|
110
127
|
|
|
111
|
-
async function
|
|
128
|
+
async function saveSettingsValue(key: string, value: any) {
|
|
112
129
|
const settings = await getSettings()
|
|
113
130
|
settings[key] =
|
|
114
131
|
settingsTable[key] && settingsTable[key].defaultValue === value
|
|
@@ -118,11 +135,11 @@ async function saveSattingsValue(key: string, value: any) {
|
|
|
118
135
|
await setValue(storageKey, settings)
|
|
119
136
|
}
|
|
120
137
|
|
|
121
|
-
export async function
|
|
138
|
+
export async function resetSettingsValues() {
|
|
122
139
|
await setValue(storageKey, {})
|
|
123
140
|
}
|
|
124
141
|
|
|
125
|
-
export async function
|
|
142
|
+
export async function saveSettingsValues(
|
|
126
143
|
values: Record<string, boolean | string | undefined>
|
|
127
144
|
) {
|
|
128
145
|
const settings = await getSettings()
|
|
@@ -206,6 +223,18 @@ async function updateOptions() {
|
|
|
206
223
|
break
|
|
207
224
|
}
|
|
208
225
|
|
|
226
|
+
case "select": {
|
|
227
|
+
const options = $$(
|
|
228
|
+
`#${settingsElementId} .option_groups .select_option[data-key="${key}"] .bes_select option`
|
|
229
|
+
) as HTMLOptionElement[]
|
|
230
|
+
|
|
231
|
+
for (const option of options) {
|
|
232
|
+
option.selected = option.value === String(getSettingsValue(key))
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
break
|
|
236
|
+
}
|
|
237
|
+
|
|
209
238
|
case "textarea": {
|
|
210
239
|
const textArea = $(
|
|
211
240
|
`#${settingsElementId} .option_groups textarea[data-key="${key}"]`
|
|
@@ -324,11 +353,11 @@ function createSettingsElement() {
|
|
|
324
353
|
// console.log(key, item, type, group)
|
|
325
354
|
switch (type) {
|
|
326
355
|
case "switch": {
|
|
327
|
-
const switchOption = createSwitchOption(item.title, {
|
|
356
|
+
const switchOption = createSwitchOption(item.icon, item.title, {
|
|
328
357
|
async onchange(event: Event) {
|
|
329
358
|
const checkbox = event.target as HTMLInputElement
|
|
330
359
|
if (checkbox) {
|
|
331
|
-
await
|
|
360
|
+
await saveSettingsValue(key, checkbox.checked)
|
|
332
361
|
}
|
|
333
362
|
},
|
|
334
363
|
})
|
|
@@ -357,7 +386,7 @@ function createSettingsElement() {
|
|
|
357
386
|
|
|
358
387
|
timeoutId = setTimeout(async () => {
|
|
359
388
|
if (textArea) {
|
|
360
|
-
await
|
|
389
|
+
await saveSettingsValue(key, textArea.value.trim())
|
|
361
390
|
}
|
|
362
391
|
}, 100)
|
|
363
392
|
},
|
|
@@ -389,6 +418,39 @@ function createSettingsElement() {
|
|
|
389
418
|
break
|
|
390
419
|
}
|
|
391
420
|
|
|
421
|
+
case "select": {
|
|
422
|
+
const div = addElement(optionGroup, "div", {
|
|
423
|
+
class: "select_option bes_option",
|
|
424
|
+
"data-key": key,
|
|
425
|
+
})
|
|
426
|
+
if (item.icon) {
|
|
427
|
+
addElement(div, "img", { src: item.icon, class: "bes_icon" })
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
addElement(div, "span", {
|
|
431
|
+
textContent: item.title,
|
|
432
|
+
class: "bes_title",
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
const select = addElement(div, "select", {
|
|
436
|
+
class: "bes_select",
|
|
437
|
+
onchange: async () => {
|
|
438
|
+
console.log(select.value)
|
|
439
|
+
await saveSettingsValue(key, select.value)
|
|
440
|
+
},
|
|
441
|
+
}) as HTMLSelectElement
|
|
442
|
+
|
|
443
|
+
for (const option of Object.entries(
|
|
444
|
+
(item as SettingsSelectItem).options
|
|
445
|
+
)) {
|
|
446
|
+
addElement(select, "option", {
|
|
447
|
+
textContent: option[0],
|
|
448
|
+
value: option[1],
|
|
449
|
+
})
|
|
450
|
+
}
|
|
451
|
+
break
|
|
452
|
+
}
|
|
453
|
+
|
|
392
454
|
case "tip": {
|
|
393
455
|
const tip = addElement(optionGroup, "div", {
|
|
394
456
|
class: "bes_tip",
|
|
@@ -518,7 +580,7 @@ export const initSettings = async (options: SettingsOptions) => {
|
|
|
518
580
|
runWhenHeadExists(() => {
|
|
519
581
|
addStyle(getSettingsStyle())
|
|
520
582
|
})
|
|
521
|
-
|
|
583
|
+
runWhenDomReady(() => {
|
|
522
584
|
initExtensionList()
|
|
523
585
|
addSideMenu()
|
|
524
586
|
})
|
package/lib/style.scss
CHANGED
|
@@ -30,12 +30,18 @@
|
|
|
30
30
|
font-size: 26px;
|
|
31
31
|
font-weight: 800;
|
|
32
32
|
border: none;
|
|
33
|
+
color: var(--browser-extension-settings-text-color);
|
|
34
|
+
margin: 18px 0;
|
|
35
|
+
padding: 0;
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
h2 {
|
|
36
39
|
font-size: 18px;
|
|
37
40
|
font-weight: 600;
|
|
38
41
|
border: none;
|
|
42
|
+
color: var(--browser-extension-settings-text-color);
|
|
43
|
+
margin: 14px 0;
|
|
44
|
+
padding: 0;
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
.extension_list_container {
|
|
@@ -134,6 +140,7 @@
|
|
|
134
140
|
font-size: 18px;
|
|
135
141
|
font-weight: 600;
|
|
136
142
|
border: none;
|
|
143
|
+
color: var(--browser-extension-settings-text-color);
|
|
137
144
|
}
|
|
138
145
|
|
|
139
146
|
footer {
|
|
@@ -208,7 +215,8 @@
|
|
|
208
215
|
box-sizing: border-box;
|
|
209
216
|
}
|
|
210
217
|
|
|
211
|
-
.switch_option
|
|
218
|
+
.switch_option,
|
|
219
|
+
.select_option {
|
|
212
220
|
display: flex;
|
|
213
221
|
justify-content: space-between;
|
|
214
222
|
align-items: center;
|
|
@@ -224,8 +232,29 @@
|
|
|
224
232
|
border-top: none;
|
|
225
233
|
}
|
|
226
234
|
|
|
227
|
-
.
|
|
235
|
+
.bes_option > .bes_icon {
|
|
236
|
+
width: 24px;
|
|
237
|
+
height: 24px;
|
|
238
|
+
margin-right: 10px;
|
|
239
|
+
}
|
|
240
|
+
.bes_option > .bes_title {
|
|
228
241
|
margin-right: 10px;
|
|
242
|
+
flex-grow: 1;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
.bes_option > .bes_select {
|
|
246
|
+
box-sizing: border-box;
|
|
247
|
+
background-color: #fff;
|
|
248
|
+
height: 24px;
|
|
249
|
+
padding: 0 2px 0 2px;
|
|
250
|
+
margin: 0;
|
|
251
|
+
border-radius: 6px;
|
|
252
|
+
border: 1px solid #ccc;
|
|
253
|
+
/*
|
|
254
|
+
-webkit-appearance: none;
|
|
255
|
+
-moz-appearance: none;
|
|
256
|
+
appearance: none;
|
|
257
|
+
*/
|
|
229
258
|
}
|
|
230
259
|
|
|
231
260
|
.option_groups .bes_tip {
|
package/lib/switch.ts
CHANGED
|
@@ -29,9 +29,27 @@ export function createSwitch(options = {} as SwichOptions): HTMLElement {
|
|
|
29
29
|
export function createSwitchOption(
|
|
30
30
|
text: string,
|
|
31
31
|
options: SwichOptions
|
|
32
|
+
): HTMLElement
|
|
33
|
+
export function createSwitchOption(
|
|
34
|
+
icon: string | undefined,
|
|
35
|
+
text: string,
|
|
36
|
+
options: SwichOptions
|
|
37
|
+
): HTMLElement
|
|
38
|
+
export function createSwitchOption(
|
|
39
|
+
icon: string,
|
|
40
|
+
text: string | SwichOptions,
|
|
41
|
+
options?: SwichOptions
|
|
32
42
|
): HTMLElement {
|
|
33
|
-
|
|
34
|
-
|
|
43
|
+
if (typeof text !== "string") {
|
|
44
|
+
return createSwitchOption(undefined, icon, text)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const div = createElement("div", { class: "switch_option bes_option" })
|
|
48
|
+
if (icon) {
|
|
49
|
+
addElement(div, "img", { src: icon, class: "bes_icon" })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
addElement(div, "span", { textContent: text, class: "bes_title" })
|
|
35
53
|
div.append(createSwitch(options))
|
|
36
54
|
return div
|
|
37
55
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "browser-extension-settings",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Settings module for developing browser extensions and userscripts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.ts",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"homepage": "https://github.com/utags/browser-extension-settings#readme",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"browser-extension-storage": "^0.1.2",
|
|
31
|
-
"browser-extension-utils": "^0.1.
|
|
31
|
+
"browser-extension-utils": "^0.1.17"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/chrome": "^0.0.243",
|