forked from brookhong/Surfingkeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firefox_bg.js
53 lines (48 loc) · 1.55 KB
/
firefox_bg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function loadRawSettings(keys, cb, defaultSet) {
var rawSet = defaultSet || {};
chrome.storage.local.get(null, function(localSet) {
var localSavedAt = localSet.savedAt || 0;
extendObject(rawSet, localSet);
var subset = getSubSettings(rawSet, keys);
if (chrome.runtime.lastError) {
subset.error = "Settings sync may not work thoroughly because of: " + chrome.runtime.lastError.message;
}
cb(subset);
});
}
function _applyProxySettings(proxyConf) {
}
function readLocalFile(path, cb) {
fetch(path, {mode:'same-origin'})
.then(function(res) {
return res.blob();
})
.then(function(blob) {
var reader = new FileReader();
reader.addEventListener("loadend", function() {
cb(this.result);
});
reader.readAsText(blob);
});
}
function request(url, onReady, headers, data, onException) {
if (/^file:\/\/\//.test(url)) {
return readLocalFile(url, onReady);
}
headers = headers || {};
return new Promise(function(acc, rej) {
var xhr = new XMLHttpRequest();
var method = (data !== undefined) ? "POST" : "GET";
xhr.open(method, url);
for (var h in headers) {
xhr.setRequestHeader(h, headers[h]);
}
xhr.onload = function() {
acc(xhr.responseText);
};
xhr.onerror = rej.bind(null, xhr);
xhr.send(data);
}).then(onReady).catch(function(exp) {
onException && onException(exp);
});
}