]>
Commit | Line | Data |
---|---|---|
1 | // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. | |
2 | ||
3 | function update_settings() { | |
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() { | |
40 | // Can't leave this blank or Chrome will resize the options page. | |
41 | msg.innerHTML = ' '; | |
42 | }, 5000); | |
43 | } | |
44 | ||
45 | function keydown(e) { | |
46 | if (e.key == 'Enter') { | |
47 | update_settings(); | |
48 | } | |
49 | } | |
50 | ||
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 | ||
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 | ||
80 | window.onload = function() { | |
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 | ||
86 | var field = document.getElementById('save'); | |
87 | field.onclick = update_settings; | |
88 | ||
89 | settings_keys.forEach(function(key) { | |
90 | var field = document.getElementById(key); | |
91 | field.value = settings[key]; | |
92 | field.onkeydown = keydown; | |
93 | }); | |
94 | }); | |
95 | document.getElementById('show-pass').onclick = toggle_visible_pass; | |
96 | }; |