]> git.wh0rd.org Git - chrome-ext/web-power-switch.git/commitdiff
convert to using dynamic runtime chrome.permissions API
authorMike Frysinger <vapier@gentoo.org>
Thu, 22 Aug 2013 04:44:08 +0000 (00:44 -0400)
committerMike Frysinger <vapier@gentoo.org>
Thu, 22 Aug 2013 04:44:08 +0000 (00:44 -0400)
manifest.json
options.html
options.js

index 241098fd3b1961ce23ba6a2f945284ab90395d41..65a0b897e18cf699870826a3bd9e67dfd10b9a08 100644 (file)
@@ -2,7 +2,7 @@
   "manifest_version": 2,
   "minimum_chrome_version": "22",
   "name": "Web Power Switch Manager",
-  "version": "1.2",
+  "version": "2.0",
   "description": "Quickly control Web Power Switches",
   "icons": {
     "128": "images/outlet-128x128.png"
@@ -17,7 +17,9 @@
   },
   "options_page": "options.html",
   "permissions": [
-    "storage",
+    "storage"
+  ],
+  "optional_permissions": [
     "http://*/"
   ]
 }
index 849f183b72864ebe5c421a2799052d4f67b1e2f5..2bc1f4a92cc32514369316aab76b65df993c2798 100644 (file)
 <tr>
  <td>Pass:</td><td><input type='text' id='pass'></td>
 </tr>
+<tr>
+ <td colspan=2 align=center><input type='button' id='save' value='Save'></td>
+</tr>
 </table>
+<div id='msg'>You must hit "Save" the first time to authorize the client</div>
 </body>
 </html>
index 3464826d014e1819c1004ce991a3bed3f350fd26..b017cd8087c240d7aaf1d1f4e7f0d21f66fca40e 100644 (file)
@@ -1,17 +1,54 @@
 // Written by Mike Frysinger <vapier@gmail.com>.  Released into the public domain.  Suck it.
 
 function update_settings() {
-       var setting = {};
-       setting[this.id] = this.value;
-       storage.set(setting);
+       var url = document.getElementById('url').value + '/*';
+       var msg = document.getElementById('msg');
+
+       console.log('requesting access to', url);
+       chrome.permissions.request({
+               origins: [url]
+       }, function(granted) {
+               if (granted) {
+                       msg.innerText = 'Saved!';
+
+                       // Sync all of the settings to storage first.
+                       var settings = {}
+                       settings_keys.forEach(function(key) {
+                               var field = document.getElementById(key);
+                               settings[field.id] = field.value;
+                       });
+                       storage.set(settings);
+
+                       // Then revoke existing perms that the user gave us.
+                       chrome.permissions.getAll(function(perms) {
+                               perms.origins.forEach(function(key) {
+                                       if (key == url)
+                                               return;
+
+                                       console.log('revoking access to', key);
+                                       chrome.permissions.remove({
+                                               origins: [key],
+                                       });
+                               });
+                       });
+               } else {
+                       msg.innerText = 'You must grant permission in order to save!';
+               }
+       });
+
+       msg.timeout = setTimeout(function() {
+               msg.innerText = '';
+       }, 5000);
 }
 
 window.onload = function() {
        storage.get(settings_keys, function(settings) {
+               var field = document.getElementById('save');
+               field.onclick = update_settings;
+
                settings_keys.forEach(function(key) {
                        var field = document.getElementById(key);
                        field.value = settings[key] || settings_defaults[key];
-                       field.oninput = update_settings;
                });
        });
 };