-
Notifications
You must be signed in to change notification settings - Fork 2
/
content_main.js
89 lines (83 loc) · 2.59 KB
/
content_main.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
console.log("content_main.js loaded try8");
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === "local") {
console.log("------------------------RED ALERT------------------------");
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
if (key === "yrmPolicy" && oldValue !== newValue) {
console.log("RELOAD");
chrome.storage.sync.get(["data"], function (items) {
categories = JSON.parse(items.data);
console.log(categories);
});
location.reload();
}
}
}
});
let categories = {};
// Read it using the storage API
chrome.storage.sync.get(["data"], function (items) {
try {
categories = JSON.parse(items.data);
} catch {
categories = { data: 15 }; // Used at inital bootup
}
modify_page();
});
let NewLength = 0;
let OldLength = 0;
async function modify_page() {
let timer = setInterval(() => {
const elements = document.querySelectorAll(".ytd-rich-grid-row");
OldLength = elements.length;
if (OldLength != 0) {
clearInterval(timer);
RemoveElements(elements);
}
}, 1000);
}
window.onscroll = function (e) {
let elementsScroll = document.querySelectorAll(".ytd-rich-grid-row");
NewLength = elementsScroll.length;
if (NewLength > OldLength) {
elementsScroll = [...elementsScroll];
elementsScroll = elementsScroll.slice(OldLength);
OldLength = NewLength;
RemoveElements(elementsScroll);
}
};
async function RemoveElements(elements) {
let sum = Object.values(categories).reduce(
(partialSum, a) => partialSum + a,
0
);
elements.forEach((item) => {
let firstChild = item.firstElementChild;
if (firstChild.id == "content") {
if (sum == 15) {
console.log("All categories selected");
} else if (sum == 0) {
firstChild.style.display = "none";
} else {
link =
firstChild.firstElementChild.firstElementChild.firstElementChild
.firstElementChild.href;
fetch(link)
.then((response) => response.text())
.then((text) => {
let category = text.substring(
text.search('"category"') + 12,
text.search('"category"') + 15
);
if (!categories[category]) {
firstChild.style.display = "none";
}
})
.catch();
}
}
});
}
function logc(fn = "notsent", data, data2 = "") {
console.log(`[content_main.js] function:[${fn}] data:[${data}] [${data2}]`);
}