-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
138 lines (119 loc) · 2.66 KB
/
utils.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
/**
* Get extentions.
*/
export async function getExtensions() {
const excludedIds = await getExcludedExtensionIds();
return (await getAllExtensions()).filter(isTarget);
// Check if the app is a target.
function isTarget(app) {
return !excludedIds.includes(app.id);
}
}
/**
* Get all extensions.
*/
export async function getAllExtensions() {
const id = chrome.runtime.id;
return (await getApps()).filter(isTarget).sort(compareName);
// Check if the app is a target.
function isTarget(app) {
return app.type === "extension" && app.id !== id;
}
// Comparator which uses `.name` property.
function compareName(e1, e2) {
if (e1.name < e2.name) {
return -1;
} else if (e1.name > e2.name) {
return 1;
}
return 0;
}
}
/**
* Get the excluded extension ids.
*/
export async function getExcludedExtensionIds() {
const KEY = "extensionsExcluded";
const result = await getStorage(KEY);
return result[KEY] || [];
}
/**
* Get the apps using the Chrome API.
*/
async function getApps() {
return new Promise((resolve) => {
chrome.management.getAll((apps) => {
resolve(apps);
});
});
}
/**
* Get a storage value.
*/
async function getStorage(key) {
return new Promise((resolve) => {
chrome.storage.sync.get(key, (result) => {
resolve(result);
});
});
}
/**
* Get an extension info.
*/
async function getExtension(id) {
return new Promise((resolve) => {
chrome.management.get(id, (app) => {
resolve(app);
});
});
}
/**
* Switch the state of an extension.
*/
export async function switchExtension(app) {
return new Promise((resolve) => {
chrome.management.setEnabled(app.id, !app.enabled, () => {
resolve();
});
});
}
/**
* Switch the excluded setting of an extension.
*/
export async function switchExcludedStatus(id, excludedIds) {
return new Promise((resolve) => {
const index = excludedIds.indexOf(id);
if (index > -1) {
excludedIds.splice(index, 1);
} else {
excludedIds.push(id);
}
const setting = {
extensionsExcluded: excludedIds,
};
chrome.storage.sync.set(setting, () => {
resolve();
});
});
}
/**
* Get the extension icon url.
*/
export function getIconUrl(extension) {
const icons = extension.icons;
// `icons` can be undefined and should be checked.
if (Array.isArray(icons)) {
if (icons.length > 0) {
let [icon] = [...icons].reverse();
return icon.url;
}
}
return null;
}
/**
* Get the extension name for display.
*/
export function getNameDisplay(extension, max) {
let name = extension.name;
return name.length > max ? name.slice(0, max) + "..." : name;
}