-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_worker.js
77 lines (65 loc) · 2.15 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
/*
Copyright (c) 2025 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.0 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
Author: [email protected]
*/
let api_token = null;
/*
DEFINE LISTENERS
*/
// reset api_token on login
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({ api_token: null });
});
// Add Listener for receiving messages
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
switch (request.type) {
case 'popup': {
processPopupMessage(request.action, request?.value);
break;
}
}
}
);
/*
IMPLEMENT FUNCTIONALITY
*/
function processPopupMessage(action, value = null) {
switch (action) {
case 'get-api-token':
// verify if token is set and if we can query Catalyst Center API
if (!api_token) {
chrome.storage.session.get(["api_token"]).then((result) => {
if (!result.api_token) {
chrome.runtime.sendMessage(chrome.runtime.id, { type: "service_worker", action: "no-token" });
} else {
api_token = result.api_token;
chrome.runtime.sendMessage(chrome.runtime.id, { type: "service_worker", action: "token", token: api_token });
}
});
} else {
chrome.runtime.sendMessage(chrome.runtime.id, { type: "service_worker", action: "token", token: api_token });
}
break;
case 'set-api-token':
apiToken = value;
chrome.storage.session.set({ api_token: apiToken });
break;
case 'reset-api-token':
chrome.storage.session.set({ api_token: null });
break;
}
}
function onError(tabs) {
console.error(`Error: ${error}`);
}