]>
Commit | Line | Data |
---|---|---|
dc7033a8 | 1 | // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. |
a98f3ef6 MF |
2 | |
3 | function update_settings() { | |
3a003566 MF |
4 | var url = document.getElementById('url').value + '/*'; |
5 | var msg = document.getElementById('msg'); | |
6 | ||
7 | console.log('requesting access to', url); | |
8 | chrome.permissions.request({ | |
9 | origins: [url] | |
10 | }, function(granted) { | |
11 | if (granted) { | |
12 | msg.innerText = 'Saved!'; | |
13 | ||
14 | // Sync all of the settings to storage first. | |
15 | var settings = {} | |
16 | settings_keys.forEach(function(key) { | |
17 | var field = document.getElementById(key); | |
18 | settings[field.id] = field.value; | |
19 | }); | |
20 | storage.set(settings); | |
21 | ||
22 | // Then revoke existing perms that the user gave us. | |
23 | chrome.permissions.getAll(function(perms) { | |
24 | perms.origins.forEach(function(key) { | |
25 | if (key == url) | |
26 | return; | |
27 | ||
28 | console.log('revoking access to', key); | |
29 | chrome.permissions.remove({ | |
30 | origins: [key], | |
31 | }); | |
32 | }); | |
33 | }); | |
34 | } else { | |
35 | msg.innerText = 'You must grant permission in order to save!'; | |
36 | } | |
37 | }); | |
38 | ||
39 | msg.timeout = setTimeout(function() { | |
d5664f4c MF |
40 | // Can't leave this blank or Chrome will resize the options page. |
41 | msg.innerHTML = ' '; | |
3a003566 | 42 | }, 5000); |
a98f3ef6 MF |
43 | } |
44 | ||
d5664f4c MF |
45 | function keydown(e) { |
46 | if (e.key == 'Enter') { | |
47 | update_settings(); | |
48 | } | |
49 | } | |
50 | ||
e09bdd59 MF |
51 | function toggle_visible_pass() { |
52 | const ele = document.getElementById('pass'); | |
53 | ele.type = (ele.type == 'password') ? 'text' : 'password'; | |
54 | // Disable form submission. | |
55 | return false; | |
56 | } | |
57 | ||
00a6ceea MF |
58 | function theme_select(theme, init) { |
59 | const theme_system = $('#theme-system'); | |
60 | const theme_light = $('#theme-light'); | |
61 | const theme_dark = $('#theme-dark'); | |
62 | ||
63 | theme_system.className = theme == 'system' ? 'selected' : ''; | |
64 | theme_light.className = theme == 'light' ? 'selected' : ''; | |
65 | theme_dark.className = theme == 'dark' ? 'selected' : ''; | |
66 | ||
67 | if (init) { | |
68 | theme_system.onclick = theme_click; | |
69 | theme_light.onclick = theme_click; | |
70 | theme_dark.onclick = theme_click; | |
71 | } | |
72 | } | |
73 | ||
74 | function theme_click() { | |
75 | const theme = this.textContent.toLowerCase(); | |
76 | theme_select(theme); | |
77 | storage.set({theme}); | |
78 | } | |
79 | ||
a98f3ef6 | 80 | window.onload = function() { |
00a6ceea MF |
81 | storage.get(settings_keys, function(settings_storage) { |
82 | const settings = Object.assign({}, settings_defaults, settings_storage); | |
83 | ||
84 | theme_select(settings['theme'], true); | |
85 | ||
3a003566 MF |
86 | var field = document.getElementById('save'); |
87 | field.onclick = update_settings; | |
88 | ||
a98f3ef6 MF |
89 | settings_keys.forEach(function(key) { |
90 | var field = document.getElementById(key); | |
00a6ceea | 91 | field.value = settings[key]; |
d5664f4c | 92 | field.onkeydown = keydown; |
a98f3ef6 MF |
93 | }); |
94 | }); | |
e09bdd59 | 95 | document.getElementById('show-pass').onclick = toggle_visible_pass; |
a98f3ef6 | 96 | }; |