-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
100 lines (89 loc) · 3.03 KB
/
popup.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
var domLoaded = false;
var synced = false;
document.addEventListener("DOMContentLoaded", () => {
domLoaded = true;
init();
});
chrome.runtime.onMessage.addListener(msg => {
if (msg.synced) {
synced = true;
}
if (domLoaded && synced) {
init();
}
});
async function init() {
console.log("creating extension UI");
await createExtensionUI();
console.log("creating checkbox event listeners");
await createCheckboxEventListeners();
}
async function createExtensionUI() {
const views = await getViews();
const viewContainer = document.getElementById("view-container");
// reset views if already loaded
document.querySelectorAll(".view-wrapper").forEach(viewWrapperElement => {
viewContainer.removeChild(viewWrapperElement);
});
Object.values(views).filter(view => { return view.active; }).sort((a, b) => { return a.internalId - b.internalId; })
.forEach(view => {
const viewTitle = `<div class="view-title-wrapper"><span class="view-title">${view.title}</span></div>`;
const checkbox = `<div class="view-toggle-wrapper"><input id="view-${view.id}" view-id="${view.id}" type="checkbox" class="checkbox"><label for="view-${view.id}" class="toggle"></label></div>`;
const viewWrapper = document.createElement("div");
viewWrapper.classList.add("view-wrapper");
viewWrapper.innerHTML = `${checkbox}${viewTitle}`;
viewContainer.appendChild(viewWrapper);
document.getElementById(`view-${view.id}`).checked = view.displayed;
});
}
async function createCheckboxEventListeners() {
document.querySelectorAll(".checkbox").forEach(async checkbox => {
checkbox.addEventListener("change", async event => {
const viewId = event.target.getAttribute("view-id");
const checked = event.target.checked;
const views = await getViews();
const displayed = views[viewId].displayed;
if (checked && !displayed) {
views[viewId].displayed = true;
await setViews(views);
sendMessage(viewId, "show");
}
else if (!checked && displayed) {
views[viewId].displayed = false;
await setViews(views);
sendMessage(viewId, "hide");
}
});
});
}
function sendMessage(viewId, action) {
chrome.tabs.query({ url: "https://datadog.zendesk.com/agent/*" }, tabs => {
tabs.forEach(tab => {
chrome.tabs.sendMessage(tab.id, { id: viewId, action: action });
});
});
}
function getViews() {
return new Promise((resolve, reject) => {
chrome.storage.local.get(["views"], value => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
resolve(value.views);
});
});
}
function setViews(views) {
return new Promise((resolve, reject) => {
chrome.storage.local.set({ views: views }, () => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
resolve();
});
});
}
// close extension UI if new window is focused on
chrome.windows.onFocusChanged.addListener(() => {
window.close();
}, { windowTypes: ['normal'] });