-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
77 lines (62 loc) · 1.82 KB
/
util.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
import con, {debugMode} from "/console.js";
const {menus, runtime} = browser;
export function isFolder({type, url}) {
// "type" property added in Firefox 57
switch (type) {
case "folder":
return true;
case undefined:
return url === undefined;
default:
return false;
}
}
export function isSeparator({type}) {
return type === "separator";
}
export function isSortable(node) {
if (!node) return false;
if (node.unmodifiable) {
con.log("Unmodifiable node:", node);
return false;
}
if (!isFolder(node)) {
con.log("Not a folder:", node);
return false;
}
return true;
}
const MSEC_PER_SEC = 1000;
export async function timedRun(func) {
if (!debugMode) return func();
let t = performance.now();
const res = await func();
t = performance.now() - t;
con.log("Completed in %.3fs", t / MSEC_PER_SEC);
return res;
}
export function objectsEqual(obj1, obj2) {
if (obj1 === obj2) return true;
const names1 = Object.getOwnPropertyNames(obj1);
const names2 = Object.getOwnPropertyNames(obj2);
if (names1.length !== names2.length) return false;
for (const n of names1) if (obj1[n] !== obj2[n]) return false;
return true;
}
export function merge(...objs) {
return objs.reduce(Object.assign, {});
}
export function createMenuItem(properties) {
return new Promise((resolve, reject) => {
const id = menus.create(properties, () => {
const {lastError: error} = runtime;
if (error) {
con.error("Failed to create menu item (%o):", properties, error);
reject(error);
} else {
con.log("Created menu item (%o):", properties, id);
resolve(id);
}
});
});
}