Commit 3ef5382c authored by spenibus's avatar spenibus

added option: staticOrigin

parent be811740
......@@ -38,6 +38,7 @@ Preferences
Available in about:addons.
- `enabledAtStartup` : Enables this addon on startup
- `staticOrigin` : Force the value of `access-control-allow-origin`
FAQ
......
//************************************************************* class definition
let spenibus_corsEverywhere = {
/***************************************************************************
props
***/
enabled : false
enabled : false
,prefs : {} // holds user prefs
,transactions : {} // contains requests/responses
......@@ -18,16 +20,25 @@ let spenibus_corsEverywhere = {
spenibus_corsEverywhere.toggle();
});
// enabled at startup
let prefs = browser.storage.sync.get('enabledAtStartup');
prefs.then((res) => {
if(res.enabledAtStartup) {
// 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.toggle(true);
}
// update button
spenibus_corsEverywhere.updateButton();
});
// update button
spenibus_corsEverywhere.updateButton();
return this;
}
......@@ -78,6 +89,8 @@ let spenibus_corsEverywhere = {
spenibus_corsEverywhere.responseHandler
);
}
return this;
}
......@@ -87,6 +100,7 @@ let spenibus_corsEverywhere = {
,updateButton : function() {
let buttonStatus = spenibus_corsEverywhere.enabled ? 'on' : 'off';
browser.browserAction.setIcon({path:{48:'media/button-48-'+buttonStatus+'.png'}});
return this;
}
......@@ -154,12 +168,19 @@ let spenibus_corsEverywhere = {
transaction.responseHeaders[name] = header;
}
// set "access-control-allow-origin", prioritize "origin" else "*"
transaction.responseHeaders['access-control-allow-origin'].value =
transaction.requestHeaders['origin']
&& transaction.requestHeaders['origin'].value !== null
? transaction.requestHeaders['origin'].value
: '*';
// set "access-control-allow-origin"
// use static origin if set in prefs
if(bg.prefs.staticOrigin) {
transaction.responseHeaders['access-control-allow-origin'].value = bg.prefs.staticOrigin;
}
// default: prioritize "origin" else "*"
else {
transaction.responseHeaders['access-control-allow-origin'].value =
transaction.requestHeaders['origin']
&& transaction.requestHeaders['origin'].value !== null
? transaction.requestHeaders['origin'].value
: '*';
}
// set "access-control-allow-methods"
if(
......@@ -194,7 +215,7 @@ let spenibus_corsEverywhere = {
};
/*******************************************************************************
run
***/
spenibus_corsEverywhere.init();
\ No newline at end of file
//************************************************************************** run
var bg = spenibus_corsEverywhere.init();
......@@ -2,7 +2,7 @@
"manifest_version" : 2
,"name" : "CORS Everywhere"
,"version" : "17.7.16.1852"
,"version" : "18.3.8.2031"
,"author" : "spenibus"
,"description" : "Bypass CORS restrictions by altering http responses."
......
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form>
<div>
<label>
<input type="checkbox" id="enabledAtStartup">
Enabled at startup
</label>
</div>
<div>
<button type="submit">Save</button>
</div>
</form>
<script src="options.js"></script>
<form>
<fieldset>
<legend>Options</legend>
<div>
<label>
<input type="checkbox" id="enabledAtStartup">
Enabled at startup
</label>
</div>
<hr>
</div>
<label>
<input type="text" id="staticOrigin" placeholder="disabled">
Force the value of "access-control-allow-origin" (leave empty for default behaviour)
</label>
</div>
<hr>
<div>
<button type="submit">Save</button>
</div>
</fieldset>
</form>
<script src="options.js"></script>
</body>
</html>
\ No newline at end of file
function saveOptions(e) {
browser.storage.sync.set({
// prefs object
let prefs = {
enabledAtStartup : document.querySelector('#enabledAtStartup').checked
,staticOrigin : document.querySelector('#staticOrigin').value
}
browser.storage.sync.set(prefs);
// update background script
browser.runtime.getBackgroundPage().then(function(page){
page.bg.prefs = prefs;
});
e.preventDefault();
}
function restoreOptions() {
let prefs = browser.storage.sync.get('enabledAtStartup');
prefs.then((res) => {
browser.storage.sync.get('enabledAtStartup').then((res) => {
document.querySelector('#enabledAtStartup').checked = res.enabledAtStartup || false;
});
browser.storage.sync.get('staticOrigin').then((res) => {
document.querySelector('#staticOrigin').value = res.staticOrigin;
});
}
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