-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
110 lines (97 loc) · 2.19 KB
/
helper.js
File metadata and controls
110 lines (97 loc) · 2.19 KB
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
/**
*
* @returns
*/
export async function getAllStates() {
return await browser.storage.local.get(null);
}
/**
*
* @param {*} item
* @returns
* @description
*/
export async function getLocalItem(item) {
const data = await browser.storage.local.get(item);
return data;
}
/**
*
* @param {*} item
* @description
*/
export async function setLocalItem(item) {
const data = await browser.storage.local.set(item);
return data;
}
/**
*
* @param {*} item
*/
export async function removeLocalItem(item) {
await browser.storage.local.remove(item);
}
/**
*
*/
export async function reset(items) {
await browser.storage.local.clear();
}
/**
*
* @param {*} tab
* @param {*} command
* @description
*/
export function transmitCommand(command) {
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
const id = tabs[0].id;
browser.tabs.sendMessage(id, command);
});
}
/**
*
* @param {*} element
* @param {*} e
*/
export function toggleActiveClass(element, e) {
if (element) {
e.target.classList.add("active");
} else {
e.target.classList.remove("active");
}
}
export async function toggleItemState(key) {
const item = await getLocalItem(key);
if (Object.entries(item).length === 0) {
await setLocalItem({ [key]: true });
} else {
await setLocalItem({
[key]: item[key] === true ? false : true,
});
}
}
export function resetState() {
document.querySelectorAll(".input").forEach((input) => {
input.checked = false;
input.setAttribute("data-page", "");
});
document.querySelector("#apply-all").setAttribute("data-page", "");
}
export function generateElements(data) {
const container = document.querySelector(".options-container");
const page = data.webpage;
const html = data.items
.map(
(item, index) => `<label class="input-item" for=${item}-${index} data-option=${item
.split(" ")
.join("")} data-page='${page}'>
<input id=${item}-${index} type="checkbox" class="input" />
${item}
</label>`
)
.join("");
document.querySelectorAll(".input-item").forEach((input) => input.remove());
container.insertAdjacentHTML("beforebegin", html);
document.querySelector("#apply-all").dataset.page = data.webpage + "-apply-all";
}