/
service-worker.js
123 lines (108 loc) · 3.58 KB
/
service-worker.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// This is the service worker script, which executes in its own context
// when the extension is installed or refreshed (or when you access its console).
console.log("Service worker (background script)")
const prod = true;
const host = prod ? "mirthturtle.com" : "localhost:3000";
const statusUrl = `http${prod ? 's' : ''}://${host}/go/learners/extension`;
const syncUrl = `http${prod ? 's' : ''}://${host}/go/learners/sync_all.json`;
// Listen for messages
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// Returns logged-in status & settings
if (message.action === "getShimariStatus") {
getStatusForExtension(sendResponse);
return true;
} else if ( message.action === "goToGoSite" ) {
chrome.tabs.update(null, {
url: 'https://mirthturtle.com/go'
});
} else if ( message.action === "goToMainSite" ) {
chrome.tabs.update(null, {
url: 'https://mirthturtle.com'
});
} else if ( message.action === "goToGoLogin" ) {
chrome.tabs.update(null, {
url: 'https://mirthturtle.com/go/login'
});
} else if ( message.action === "goToSourceCode" ) {
chrome.tabs.update(null, {
url: 'https://github.com/christiancodes/shimari-extension'
});
} else if ( message.action === "goToSettingsPage" ) {
chrome.tabs.update(null, {
url: 'https://mirthturtle.com/go/settings#extension'
});
} else if ( message.action === "goToPregame" ) {
chrome.tabs.update(null, {
url: 'https://mirthturtle.com/go/pregame'
});
} else if ( message.action === "refreshForWidget" ) {
getStatusForExtension(sendResponse);
return true;
} else if (message.action === "requestServerSync") {
requestSyncFromBackend(sendResponse);
return true;
}
});
function getStatusForExtension(sendResponse) {
chrome.cookies.getAll({ domain: host }, function (cookies) {
fetch(statusUrl, {
headers: {
Cookie: cookies
},
credentials: 'include'
})
.then((response) => response.json())
.then((json) => {
console.log('Status from API', json);
// Save status data with chrome.storage
chrome.storage.sync.set(json, function() {
console.log('Status saved.');
sendResponse(json);
});
}).catch(error => {
if (typeof error.json === "function") {
error.json().then(jsonError => {
console.log("Json error from status API", jsonError);
sendResponse('error');
}).catch(genericError => {
console.log("Generic error from status API", error.statusText);
sendResponse('error');
});
} else {
console.log("Shimari status error");
console.log(error);
sendResponse('error');
}
});
});
}
function requestSyncFromBackend(sendResponse) {
chrome.cookies.getAll({ domain: host }, function (cookies) {
fetch(syncUrl, {
method: "post",
headers: {
Cookie: cookies
},
credentials: 'include'
})
.then((response) => response.json())
.then((json) => {
console.log('Sync response', json);
console.log('Updating status for widget...');
getStatusForExtension(sendResponse);
}).catch(error => {
if (typeof error.json === "function") {
error.json().then(jsonError => {
console.log("Shimari sync error");
console.log(jsonError);
}).catch(genericError => {
console.log("Shimari sync generic error");
console.log(error.statusText);
});
} else {
console.log("Sync error");
console.log(error);
}
});;
});
}