This repository has been archived by the owner on May 10, 2024. It is now read-only.
forked from chibisafe/chibisafe-extension
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.js
58 lines (48 loc) · 1.94 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
/* global browser */
const storage = browser.storage.local
const background = browser.extension.getBackgroundPage()
storage.get().then(function (items) {
document.querySelector('form').addEventListener('submit', function (event) {
event.preventDefault()
})
const textKeys = ['textDomain', 'textToken']
textKeys.forEach(key => {
if (items[key]) document.querySelector(`#${key}`).value = items[key]
})
// Defaults to checked
if (items.autoCopyUrl !== false)
document.querySelector('#autoCopyUrl').checked = true
})
document.querySelector('#reset').addEventListener('click', async function () {
document.querySelector('#textDomain').value = ''
document.querySelector('#textToken').value = ''
document.querySelector('#autoCopyUrl').checked = true
let notificationID
try {
await storage.clear()
notificationID = await background.notifications.create('Settings cleared.')
} catch (error) {
notificationID = await background.notifications.create(error.toString())
}
return background.notifications.clear(notificationID, 5000)
})
document.querySelector('#save').addEventListener('click', async function () {
const textDomain = document.querySelector('#textDomain').value || null
const textToken = document.querySelector('#textToken').value || null
const autoCopyUrl = document.querySelector('#autoCopyUrl').checked
if (textDomain) {
if (!/^https?:\/\//.test(textDomain))
return alert('Domain must begin with a valid HTTP/HTTPS protocol.')
if (/\/$/.test(textDomain))
return alert('Domain should not have a trailing slash.')
}
let notificationID
try {
await storage.set({ textDomain, textToken, autoCopyUrl })
notificationID = await background.notifications.create('Settings saved.')
await background.createMenus()
} catch (error) {
notificationID = await background.notifications.create(error.toString())
}
return background.notifications.clear(notificationID, 5000)
})