-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
112 lines (99 loc) · 3.19 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict'
const newProfileDefaultOptions = {
profileName: '',
saveTo: 'saveToClipboard',
filterStreamURL: '.*\\.m3u8',
escapeMethod: 'escapeMethodLinux',
command: '<exe> ' +
'-i \'${stream}\' -o \'${filename}_${timestamp}_${tabtitle}.${ext}\' ' +
'${cookie:|-first \'|\' -next \'|\'|} -useragent ${useragent} -referer ${referer}'
}
function loadProfiles () {
browser.storage.local.get().then(storageLocal => {
const profileList = [_('profileSelectNew')]
profileList.push(
...Object.keys(storageLocal)
.filter(e => e.startsWith('profile['))
.map(e => e.slice('profile['.length, -1))
.sort()
)
const replSelect = document.createElement('select')
{
let profileSelect = document.getElementById('profileSelect')
replSelect.id = 'profileSelect'
for (const e of profileList) {
const opt = document.createElement('option')
opt.value = e
opt.textContent = e
replSelect.appendChild(opt)
}
replSelect.firstChild.value = 'profileSelectNew'
profileSelect.parentElement.replaceChild(replSelect, profileSelect)
profileSelect = null
}
let active = 'profileSelectNew'
let profile = newProfileDefaultOptions
if ('activeProfile' in storageLocal) {
const active_ = storageLocal.activeProfile
if (profileList.indexOf(active_) > 0) { // ignore profileSelectNew at [0]
active = active_
profile = storageLocal[`profile[${active}]`]
}
}
for (const id of Object.keys(profile)) {
if (document.getElementById(id) !== null) {
document.getElementById(id).value = profile[id]
}
}
replSelect.value = active
replSelect.addEventListener('change', uaChangeProfile)
})
}
function uaChangeProfile () {
browser.storage.local.set({
activeProfile: document.getElementById('profileSelect').value
})
loadProfiles()
}
function uaDeleteProfile () {
const pname = document.getElementById('profileSelect').value
if (pname === 'profileSelectNew') {
loadProfiles()
return
}
browser.storage.local.remove(`profile[${pname}]`)
browser.storage.local.set({
activeProfile: 'profileSelectNew'
})
loadProfiles()
}
function uaSave () {
const pname = document.getElementById('profileName').value
if (pname === '') {
alert('Error: Profile name not set.')
return
}
if (pname === 'profileSelectNew') {
alert('Error: Invalid profile name.')
return
}
browser.storage.local.set({
[`profile[${pname}]`]: [...document.getElementsByClassName('savable')].reduce((o, e) => { o[e.id] = e.value; return o }, {}),
activeProfile: pname
})
loadProfiles()
}
// i18n translations
const _ = str => { return browser.i18n.getMessage(str) || '_(' + str + ')' }
const labels = document.getElementsByTagName('label')
for (const label of labels) {
label.textContent = _(label.htmlFor)
}
const options = document.getElementsByTagName('option')
for (const option of options) {
option.textContent = _(option.value)
}
// input event handler
document.getElementById('buttonSave').addEventListener('click', uaSave)
document.getElementById('buttonDeleteProfile').addEventListener('click', uaDeleteProfile)
loadProfiles()