From: Mike Frysinger Date: Thu, 22 Aug 2013 04:44:08 +0000 (-0400) Subject: convert to using dynamic runtime chrome.permissions API X-Git-Tag: v2.1~1 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=3a003566115fa1f778c77f3e3812494ad5ba6688;p=chrome-ext%2Fweb-power-switch.git convert to using dynamic runtime chrome.permissions API --- diff --git a/manifest.json b/manifest.json index 241098f..65a0b89 100644 --- a/manifest.json +++ b/manifest.json @@ -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://*/" ] } diff --git a/options.html b/options.html index 849f183..2bc1f4a 100644 --- a/options.html +++ b/options.html @@ -19,6 +19,10 @@ Pass: + + + +
You must hit "Save" the first time to authorize the client
diff --git a/options.js b/options.js index 3464826..b017cd8 100644 --- a/options.js +++ b/options.js @@ -1,17 +1,54 @@ // Written by Mike Frysinger . 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; }); }); };