forked from dpoeschl/StashPop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
31 lines (28 loc) · 990 Bytes
/
background.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
chrome.runtime.onMessage.addListener(function(request, sender, callback) {
if (request.action == "xhttp") {
var xhttp = new XMLHttpRequest();
var method = request.method ? request.method.toUpperCase() : 'GET';
xhttp.onload = function() {
callback(xhttp.responseText);
};
xhttp.onerror = function() {
callback();
};
xhttp.open(method, request.url, true);
if (method == 'POST') {
xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
xhttp.send(request.data);
return true; // prevents the callback from being called too early on return
}
else if (request.method == "getSettings") {
var keys = {};
for (var i = 0; i < request.keys.length; i++) {
keys[request.keys[i]] = true;
}
chrome.storage.sync.get(keys, function(items) {
callback({data: items});
});
return true;
}
});