-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathoptions.js
126 lines (113 loc) · 3.89 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const form = document.getElementById('options-form')
const warning = document.getElementById('warning')
const saveBtn = form.querySelector('#save')
const locations = form.querySelector('#locations')
const locationName = form.querySelector('#location_name')
const locationPath = form.querySelector('#location_path')
const locationNew = form.querySelector('#location_new')
const locationDelete = form.querySelector('#location_delete')
form.addEventListener('submit', e => {
e.preventDefault()
const data = Array.prototype.reduce.call(form.elements, (obj, el) => {
obj[el.name] = el.value
return obj
}, {})
delete data['']
data.locations = saveLocations()
/*
When this works everywhere, replace <all_urls> with http://transmitter.web-extension/* in manifest.json.
Unfortunately, right now this is unsupported in Firefox and Edge…
and even worse, in Opera the API exists but requesting a user-specified host is not possible!
if (browser.permissions) {
browser.permissions.request({
permissions: ['webRequest', 'webRequestBlocking'],
origins: [data.base_url + '*']
})
}
*/
if (!data.base_url.endsWith('/transmission/')) {
warning.innerHTML = 'WARNING: Server URL does not end with /transmission/ — make sure it is correct! (If you actually customized it on the server side, it will work fine.)'
warning.hidden = false
} else {
warning.hidden = true
}
browser.storage.local.set({server: data}).then(() => {
saveBtn.innerHTML = 'Saved!'
setTimeout(() => {
saveBtn.innerHTML = 'Save'
}, 600)
})
})
browser.storage.local.get('server').then(({server}) => {
if (!server) {
return
}
for (const x of Object.keys(server)) {
const el = form.querySelector('[name="' + x + '"]')
if (el) {
el.value = server[x]
} else if (x === 'locations') {
renderLocations(server[x])
}
}
})
function renderLocations (locationsData) {
locations.querySelectorAll('option:not(:first-child)').forEach(opt => opt.remove())
locationsData.map(loc => createLocationOption(loc.name, loc.path))
.forEach(opt => locations.appendChild(opt))
}
locations.addEventListener('change', refreshLocationEditor)
function refreshLocationEditor () {
if (locations.selectedIndex === -1) {
locationName.value = ''
locationPath.value = ''
locationName.disabled = true
locationPath.disabled = true
locationDelete.disabled = true
} else {
let option = locations.options[locations.selectedIndex]
locationName.value = option.dataset.name
locationPath.value = option.dataset.path
locationName.disabled = false
locationPath.disabled = false
locationDelete.disabled = false
}
}
locationName.addEventListener('input', () => {
let option = locations.options[locations.selectedIndex]
option.dataset.name = locationName.value
refreshLocationOptionText(option)
})
locationPath.addEventListener('input', () => {
let option = locations.options[locations.selectedIndex]
option.dataset.path = locationPath.value
refreshLocationOptionText(option)
})
locationDelete.addEventListener('click', () => {
locations.options[locations.selectedIndex].remove()
if (locations.options.length > 1) {
locations.selectedIndex = locations.options.length - 1
}
refreshLocationEditor()
})
locationNew.addEventListener('click', () => {
let option = createLocationOption('New location', '/path/to/location')
locations.appendChild(option)
locations.selectedIndex = locations.options.length - 1
refreshLocationEditor()
locationName.select()
})
function createLocationOption (name, path) {
let option = document.createElement('option')
option.dataset.name = name
option.dataset.path = path
refreshLocationOptionText(option)
return option
}
function refreshLocationOptionText (option) {
option.innerText = option.dataset.name + ' [' + option.dataset.path + ']'
}
function saveLocations () {
return [...locations.querySelectorAll('option:not(:first-child)')]
.map((opt, index) => ({ name: opt.dataset.name, path: opt.dataset.path, index: index }))
}