(function(){

var $twilio_form = document.querySelector('#twilio');
var $twilio_api_input = document.querySelector('#twilio_api');
var $twilio_url_input = document.querySelector('#twilio_url');
var $key_visibility_btn = document.querySelector('#api_key_visibility');
var $save_gateway_info = document.querySelector('#save_gateway');
var $status_text = document.querySelector('#status');
var $clear_phones_btn = document.querySelector('#clear_phones');
var $storage = document.querySelector('#storage_usage');

var api_visible = false;

$key_visibility_btn.addEventListener('click', function(evt){
    evt.preventDefault();
    if(api_visible){
        this.textContent = '(show)';
        $twilio_api_input.setAttribute('type', 'password');
        api_visible = false;
    }
    else{
        this.textContent = '(hide)';
        $twilio_api_input.setAttribute('type', 'text');
        api_visible = true;
    }
});

$save_gateway_info.addEventListener('click', function(){
    STORAGE.set({'ACCESS_TOKEN': $twilio_api_input.value}, function(){
        $status_text.textContent = 'Access token saved. ';
        STORAGE.set({'GATEWAY_URL': $twilio_url_input.value}, function(){
            $status_text.textContent += 'Gateway url saved';
        });
    });
});

$clear_phones_btn.addEventListener('click', function(){
    var twilio_api = $twilio_api_input.value || '';
    var twilio_url = $twilio_url_input.value || '';
    // Clear all the data, but hold on to the options
    STORAGE.clear(function(){
        STORAGE.set({'ACCESS_TOKEN': $twilio_api_input.value}, function(){
            STORAGE.set({'GATEWAY_URL': $twilio_url_input.value});
            $status_text.textContent = 'Phone numbers cleared';
            get_storage_usage();
        });
    });
});

// Provide the inital twilio settings if available
STORAGE.get(['ACCESS_TOKEN', 'GATEWAY_URL'], function(data){
    var twilio_api = data['ACCESS_TOKEN'] || '';
    var twilio_url = data['GATEWAY_URL'] || '';
    $twilio_api_input.value = twilio_api;
    $twilio_url_input.value = twilio_url;
});

var get_storage_usage = (function f(){
    STORAGE.getBytesInUse(null, function(bytes){
        var percentage = bytes / STORAGE.QUOTA_BYTES;
        percentage = Math.round(percentage * 100) / 100;
        var info = '(' + bytes + ' / ' + STORAGE.QUOTA_BYTES + '): ' + percentage + '%';
        $storage.textContent = info;
    });
    return f;
})();

})();

@@ -1,18 +1,4 @@
-(function()%7B%0A%0A
 var 
@@ -1412,230 +1412,830 @@
     
-// Clear all the data, but hold on to the options%0A    STORAGE.clear(function()%7B%0A        STORAGE.set(%7B'ACCESS_TOKEN': $twilio_api_input.value%7D, function()%7B%0A            STORAGE.set(%7B'GATEWAY_URL': $twilio_url_input.value%7D);%0A
+var options_to_keep = %5B%0A        'ACCESS_TOKEN',%0A        'GATEWAY_URL'%0A    %5D;%0A    STORAGE.get(options_to_keep, function(data)%7B%0A        var options = %7B%7D;%0A        for (var d in data) %7B%0A            if (data.hasOwnProperty(d)) %7B%0A                if(data%5Bd%5D)%0A                    options%5Bd%5D = data%5Bd%5D;%0A            %7D%0A        %7D%0A        STORAGE.clear(function()%7B%0A            var option_keys = Object.keys(options);%0A            if(!option_keys.length)%0A                return;%0A%0A            for(var i=0; i%3Coption_keys.length-1; i++)%7B%0A                var k = option_keys%5Bi%5D;%0A                var q = %7B%7D;%0A                q%5Bk%5D = options%5Bk%5D;%0A                STORAGE.set(q);%0A            %7D%0A            var k = option_keys%5Boption_keys.length-1%5D;%0A            var q = %7B%7D;%0A            q%5Bk%5D = options%5Bk%5D;%0A            STORAGE.set(q, function()%7B%0A    
     
@@ -2306,16 +2306,20 @@
         
+    
 get_stor
@@ -2327,24 +2327,40 @@
 ge_usage();%0A
+            %7D);%0A
         %7D);%0A
@@ -2351,32 +2351,33 @@
 %7D);%0A        %7D);%0A
+%0A
     %7D);%0A%7D);%0A%0A// 
@@ -3000,24 +3000,24 @@
 fo;%0A    %7D);%0A
+
     return f
@@ -3024,15 +3024,8 @@
 ;%0A%7D)();%0A
-%0A%7D)();%0A

Improve clear phones logic