-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
38 lines (34 loc) · 1.1 KB
/
Copy pathbackground.js
File metadata and controls
38 lines (34 loc) · 1.1 KB
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
chrome.runtime.onInstalled.addListener(() => {
console.log('RBStats extension installed.');
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'FETCH_JSON') {
const fetchOptions = {
method: request.method || 'GET',
headers: request.headers || {},
credentials: 'include'
};
if (request.body) {
fetchOptions.body = typeof request.body === 'string' ? request.body : JSON.stringify(request.body);
}
fetch(request.url, fetchOptions)
.then(async response => {
const text = await response.text();
let json = null;
try { json = JSON.parse(text); } catch(e) {}
if (!response.ok) {
return sendResponse({
success: false,
status: response.status,
error: `HTTP ${response.status}`,
data: json
});
}
sendResponse({ success: true, data: json });
})
.catch(error => {
sendResponse({ success: false, error: error.message });
});
return true;
}
});