Commit b5351c72 authored by spenibus's avatar spenibus

whitelist can be edited; button is reflecting whitelist status

parent 2882a82d
//************************************************************* class definition
let spenibus_corsEverywhere = {
var spenibus_corsEverywhere = {
/***************************************************************************
props
***/
enabled : false
,prefs : {} // holds user prefs
,transactions : {} // contains requests/responses
enabled : false
,activationWhitelistEnabled : false
,prefs : {} // holds user prefs
,transactions : {} // contains requests/responses
/***************************************************************************
......@@ -21,16 +22,9 @@ let spenibus_corsEverywhere = {
});
// load prefs
browser.storage.sync.get([
'enabledAtStartup',
'staticOrigin',
]).then((res) => {
// get prefs, set default value if n/a
this.prefs.enabledAtStartup = res.enabledAtStartup || false;
this.prefs.staticOrigin = res.staticOrigin || '';
if(this.prefs.enabledAtStartup) {
spenibus_corsEverywhere.loadPrefs(function(){
// enact enabled at startup
if(spenibus_corsEverywhere.prefs.enabledAtStartup) {
spenibus_corsEverywhere.toggle(true);
}
......@@ -94,18 +88,64 @@ let spenibus_corsEverywhere = {
}
/***************************************************************************
re/load preferences
Because fetching prefs returns a promise, we use a callback to do stuff when
the promise is fullfilled.
***/
,loadPrefs : function(callback) {
browser.storage.sync.get([
'enabledAtStartup',
'staticOrigin',
'activationWhitelist',
]).then((res) => {
// get prefs, set default value if n/a
spenibus_corsEverywhere.prefs.enabledAtStartup = res.enabledAtStartup || false;
spenibus_corsEverywhere.prefs.staticOrigin = res.staticOrigin || '';
spenibus_corsEverywhere.prefs.activationWhitelist = res.activationWhitelist || '';
// parse activation whitelist
spenibus_corsEverywhere.prefs.activationWhitelist = spenibus_corsEverywhere.prefs.activationWhitelist
? spenibus_corsEverywhere.prefs.activationWhitelist.split(/[\r\n]+/)
: [];
spenibus_corsEverywhere.activationWhitelistEnabled = spenibus_corsEverywhere.prefs.activationWhitelist.length > 0
? true
: false;
if(callback) {
callback();
}
});
return this;
}
/***************************************************************************
updateButton
***/
,updateButton : function() {
// icon
let buttonStatus = spenibus_corsEverywhere.enabled ? 'on' : 'off';
browser.browserAction.setIcon({path:{48:'media/button-48-'+buttonStatus+'.png'}});
// button tooltip text
let str = spenibus_corsEverywhere.enabled
// tooltip text
let buttonTitle = spenibus_corsEverywhere.enabled
? 'CorsE enabled, CORS rules are bypassed'
: 'CorsE disabled, CORS rules are followed';
browser.browserAction.setTitle({title:str});
// using activation whitelist while enabled
if(spenibus_corsEverywhere.enabled && spenibus_corsEverywhere.activationWhitelistEnabled) {
buttonStatus = 'on-filter';
buttonTitle += ' (using activation whitelist)';
}
// proceed
browser.browserAction.setIcon({path:{48:'media/button-48-'+buttonStatus+'.png'}});
browser.browserAction.setTitle({title:buttonTitle});
return this;
}
......
......@@ -29,7 +29,7 @@
One filter per line.
This uses regular expressions (ex: /^https?...localhost:8080\//i).
</div>
<textarea rows="5" style="width:100%;" placeholder="no filter"></textarea>
<textarea rows="5" style="width:100%;" placeholder="no filter" id="activationWhitelist"></textarea>
</fieldset>
<fieldset>
<legend>Remember to save your changes</legend>
......
......@@ -2,15 +2,19 @@ function saveOptions(e) {
// prefs object
let prefs = {
enabledAtStartup : document.querySelector('#enabledAtStartup').checked
,staticOrigin : document.querySelector('#staticOrigin').value
enabledAtStartup : document.querySelector('#enabledAtStartup').checked || false
,staticOrigin : document.querySelector('#staticOrigin').value || ''
,activationWhitelist : document.querySelector('#activationWhitelist').value || ''
}
browser.storage.sync.set(prefs);
// update background script
browser.runtime.getBackgroundPage().then(function(page){
page.bg.prefs = prefs;
// reload prefs
browser.runtime.getBackgroundPage().then((res) => {
res.spenibus_corsEverywhere.loadPrefs(function(){
// refresh options
restoreOptions();
});
});
e.preventDefault();
......@@ -23,6 +27,9 @@ function restoreOptions() {
browser.storage.sync.get('staticOrigin').then((res) => {
document.querySelector('#staticOrigin').value = res.staticOrigin;
});
browser.storage.sync.get('activationWhitelist').then((res) => {
document.querySelector('#activationWhitelist').value = res.activationWhitelist;
});
}
document.addEventListener('DOMContentLoaded', restoreOptions);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment