-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
26 lines (24 loc) · 985 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
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(request);
if (request.action == "xhttp") {
var xhttp = new XMLHttpRequest();
var method = request.method ? request.method.toUpperCase() : 'GET';
xhttp.onload = function() {
//return xhttp.responseText;
sendResponse(JSON.parse(xhttp.responseText));
};
xhttp.onerror = function() {
// Do whatever you want on error. Don't forget to invoke the
// callback to clean up the communication port.
sendResponse();
};
xhttp.open(method, request.url, true);
if (method == 'POST') {
xhttp.setRequestHeader('Content-Type', 'application/json');
}
xhttp.send(JSON.stringify(request.data));
return true; // prevents the callback from being called too early on return
}
}
);