-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
65 lines (57 loc) · 1.63 KB
/
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
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
// with trailing slash
// const BASE_URL = "http://localhost:5080/";
const BASE_URL = "https://app.vetd.com/";
async function forwardMessage(message) {
try {
const response = await fetch(`${BASE_URL}forward`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ message })
});
const json = await response.json();
return json;
} catch (e) {
console.log("forwardMessage Error: ", e);
return false;
}
};
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.command == "forwardMessage") {
forwardMessage(request.args.message).then(response => sendResponse(response));
return true; // Will respond asynchronously.
}
}
);
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
// TODO? additional security
// if (sender.url !== "substring domain?")
// return; // don't allow this web page access
if (request.command == "setVetdUser") {
chrome.storage.sync.set(
{
vetdUser: JSON.stringify(request.args.vetdUser)
},
() => {
sendResponse(true)
}
);
return true; // Will respond asynchronously.
}
}
);
// On first install, open Vetd platform
// This increases the liklihood that user will link their Vetd account.
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason === 'install') {
chrome.tabs.create(
{
url: `${BASE_URL}chrome-extension-installed`
}
)
}
})