-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
59 lines (57 loc) · 1.88 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
// Utility functions for the extension
const GlifUtils = {
// Storage utilities
storage: {
get: async (key) => {
return new Promise((resolve) => {
chrome.storage.local.get(key, (result) => {
resolve(result[key]);
});
});
},
set: async (key, value) => {
return new Promise((resolve) => {
chrome.storage.local.set({ [key]: value }, resolve);
});
}
},
// Theme utilities
theme: {
isDarkMode: async () => {
const darkMode = await GlifUtils.storage.get('darkMode');
return darkMode === true;
},
setDarkMode: async (isDark) => {
await GlifUtils.storage.set('darkMode', isDark);
document.documentElement.setAttribute('data-glif-dark-mode', isDark);
},
toggle: async () => {
const isDark = await GlifUtils.theme.isDarkMode();
await GlifUtils.theme.setDarkMode(!isDark);
return !isDark;
}
},
// DOM utilities
dom: {
createElement: (tag, attributes = {}, children = []) => {
const element = document.createElement(tag);
Object.entries(attributes).forEach(([key, value]) => {
if (key === 'className') {
element.className = value;
} else if (key === 'innerHTML') {
element.innerHTML = value;
} else {
element.setAttribute(key, value);
}
});
children.forEach(child => {
if (typeof child === 'string') {
element.appendChild(document.createTextNode(child));
} else {
element.appendChild(child);
}
});
return element;
}
}
};