-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathchrome.js
82 lines (73 loc) · 2.04 KB
/
chrome.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
init();
async function getCurrentTab() {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
return { tabId: tab.id };
}
async function init() {
await chrome.storage.local.remove("data");
await chrome.scripting.executeScript({
target: await getCurrentTab(),
files: ["capo.js"],
});
chrome.storage.onChanged.addListener((changes) => {
console.log("Storage changed", changes);
const { data } = changes;
if (data?.newValue) {
print(data.newValue);
}
});
}
function print(result) {
console.log("Data", result);
let frag = document.createDocumentFragment();
for (let r of result.actual) {
frag.appendChild(getCapoHeadElement(r));
}
actual.appendChild(frag);
result.sorted = result.actual.sort((a, b) => {
return b.weight - a.weight;
});
frag = document.createDocumentFragment();
for (let r of result.sorted) {
frag.appendChild(getCapoHeadElement(r));
}
sorted.appendChild(frag);
document.body.addEventListener("click", handleCapoClick);
}
function getCapoHeadElement({
weight,
color,
selector,
innerHTML,
isValid,
customValidations,
}) {
const span = document.createElement("span");
span.classList.add("capo-head-element");
span.classList.toggle("invalid", !isValid);
span.dataset.weight = weight;
span.style.backgroundColor = color;
span.dataset.selector = selector;
span.dataset.innerHTML = innerHTML;
span.dataset.customValidations = JSON.stringify(customValidations);
span.title = `[${weight + 1}] ${selector}`;
return span;
}
async function handleCapoClick(event) {
const { weight, selector, innerHTML } = event.target.dataset;
const customValidations = JSON.parse(event.target.dataset.customValidations);
const isValid = !event.target.classList.contains("invalid");
await chrome.storage.local.set({
click: JSON.stringify({
weight,
selector,
innerHTML,
isValid,
customValidations,
}),
});
await chrome.scripting.executeScript({
target: await getCurrentTab(),
files: ["capo.js"],
});
}