]> git.wh0rd.org - chrome-ext/web-power-switch.git/blobdiff - popup.js
options: improve field handling
[chrome-ext/web-power-switch.git] / popup.js
index 640aa5bb0c03ea23440fabdb54a3e2351106cec8..22c31604dccc3be9d89a189555ed4f36a887c637 100644 (file)
--- a/popup.js
+++ b/popup.js
@@ -14,8 +14,11 @@ function fetchpage(url, callback) {
                                        callback(xhr, state);
                                } else {
                                        xhr.setstatus = true;
-                                       setstatus('Could not connect;<br>check your ' +
-                                                 '<a href="' + chrome.extension.getURL('options.html') + '" target=_blank>settings</a>');
+                                       setstatus(
+                                               'Could not connect;<br>check your ' +
+                                               '<a id="open-settings" href="">settings</a>'
+                                       );
+                                       document.getElementById('open-settings').onclick = open_settings_page;
                                        console.log('connect error', state);
                                }
                        }
@@ -161,11 +164,34 @@ function setstatus(msg) {
        status.style.position   = msg ? '' : 'absolute';
 }
 
+function open_settings_page() {
+       chrome.runtime.openOptionsPage();
+}
+
 document.addEventListener('DOMContentLoaded', function() {
        storage.get(settings_keys, function(settings) {
                url_base = settings['url'] || settings_defaults['url'];
                user = settings['user'] || settings_defaults['user'];
                pass = settings['pass'] || settings_defaults['pass'];
-               fetchpage('index.htm', initpopup);
+               chrome.permissions.contains({
+                       origins: [url_base + '/*']
+               }, function(granted) {
+                       if (granted) {
+                               fetchpage('index.htm', initpopup);
+                       } else {
+                               setstatus(
+                                       'Missing permissions;<br>please visit the ' +
+                                       '<a id="open-settings" href="">settings page</a>' +
+                                       '<br>to grant access.<br>' +
+                                       '<center><input id=retry type=submit value=Retry></center>'
+                               );
+                               document.getElementById('open-settings').onclick = open_settings_page;
+                               // Work around http://crbug.com/125706.
+                               document.getElementById('retry').onclick = function() {
+                                       chrome.permissions.request({origins: [url_base + '/*']});
+                                       fetchpage('index.htm', initpopup);
+                               };
+                       }
+               });
        });
 });