-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
150 lines (124 loc) · 3.7 KB
/
scripts.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(() => {
// global state
const state = {
extensions: [],
selfExtension: null
};
// layout classes
const classes = {
list: 'extensions',
listItem: 'extensions__item',
listItemActive: 'extensions__item--active',
listItemIcon: 'extensions__icon',
listItemName: 'extensions__name',
};
async function init() {
try {
await Promise.all([loadExtensions(), loadSelfExtension()]);
prepareExtensions();
render();
} catch (e) {
showError(e);
}
}
async function loadExtensions() {
await new Promise((resolve, reject) => {
chrome.management.getAll((response) => {
if (response.length - 1 > 0) {
state.extensions = response;
resolve();
} else {
reject("You have no extensions yet");
}
});
});
}
async function loadSelfExtension() {
await new Promise((resolve) => {
chrome.management.getSelf((response) => {
state.selfExtension = response;
resolve();
});
});
}
function prepareExtensions() {
const filtered = filterExtensions(state);
const sortedByApphabet = sortExtensionsByAlhabet(filtered);
state.extensions = sortedByApphabet;
}
function filterExtensions(globalState) {
return globalState.extensions.filter(
(item) =>
item.type === "extension" &&
item.id !== globalState.selfExtension.id &&
(item.mayEnable || item.mayDisable)
);
}
function sortExtensionsByAlhabet(list) {
return [...list].sort((a, b) => {
const aName = a.shortName || a.name;
const bName = b.shortName || b.name;
switch (true) {
case aName < bName:
return -1;
case aName > bName:
return 1;
default:
return 0;
}
});
}
function render() {
const root = document.getElementById("root");
root.onmousedown = () => false;
root.addEventListener("click", toggleExtension);
const extensionsList = buildExtensionsList(state);
root.appendChild(extensionsList);
}
function buildExtensionsList(globalState) {
const list = document.createElement("ul");
list.className = classes.list;
globalState.extensions.forEach((item) => {
let extension = document.createElement("li");
extension.dataset.id = item.id;
extension.className = classes.listItem;
const icon = document.createElement("img");
icon.src = item.icons[0].url;
icon.className = classes.listItemIcon;
extension.appendChild(icon);
const name = document.createElement("span");
name.textContent = item.shortName || item.name;
name.className = classes.listItemName;
extension.appendChild(name);
if (item.enabled) {
extension.classList.add(classes.listItemActive);
}
list.appendChild(extension);
});
return list;
}
function toggleExtension({ target }) {
const elem = target.closest(`.${classes.listItem}`);
if (!elem) return;
const id = elem.dataset.id;
const isActive = elem.classList.contains(classes.listItemActive);
chrome.management.setEnabled(id, !isActive);
elem.classList.toggle(classes.listItemActive);
}
function showError(error) {
const message = typeof error === "object" ? error.message : error;
showNote(message, "error");
console.error(error);
}
function showNote(message, type) {
const classes = {
error: "note--error",
};
const note = document.createElement("div");
note.className = `note ${classes[type] || ""}`.trim();
note.innerText = message;
const root = document.getElementById("root");
root.appendChild(note);
}
document.addEventListener("DOMContentLoaded", init);
})();