-
Notifications
You must be signed in to change notification settings - Fork 2
/
inject.js
117 lines (95 loc) · 3.34 KB
/
inject.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function removeElementsByClass(className) {
const elements = document.getElementsByClassName(className);
while (elements.length > 0) {
elements[0].parentNode.removeChild(elements[0]);
}
}
function removeElementById(id) {
const element = document.getElementById(id);
if (element) {
element.parentNode.removeChild(element);
}
}
function setDisplayStyleUsingElementById(id, styleValue) {
if (document.getElementById(id)) {
document.getElementById(id).style.display = styleValue;
}
}
function setDisplayStyleUsingElementsByClass(className, styleValue) {
var x = document.getElementsByClassName(className);
for (var i = 0; i < x.length; i++) {
x[i].style.display = styleValue;
}
}
function setDisplayStyleUsingElementsdByName(name, styleValue) {
document.querySelectorAll(name).forEach((item) => {
item.style.display = styleValue;
});
}
function removeShelfByCategory(category, styleValue) {
document.querySelectorAll('ytd-rich-shelf-renderer').forEach((item) => {
if (item.parentElement.querySelector('span').innerText.includes(category)) {
item.style.display = styleValue;
}
});
}
// Variables to control
var blockComments = 'none';
var leftSidebar = 'none';
var recommendation = 'none';
var homeFeed = 'none';
var shorts = 'none';
chrome.storage.sync.get(['block_comments', 'left_side_bar', 'recommendations', 'home_feed', 'shorts'], function (obj) {
blockComments = obj.block_comments = undefined ? blockComments : obj.block_comments;
leftSidebar = obj.left_side_bar == undefined ? leftSidebar : obj.left_side_bar;
recommendation = obj.recommendations == undefined ? recommendation : obj.recommendations;
homeFeed = obj.home_feed == undefined ? homeFeed : obj.home_feed;
shorts = obj.shorts == undefined ? shorts : obj.shorts;
});
function removeYoutubeElements() {
//Comments section
setDisplayStyleUsingElementById("comments", blockComments);
//Home feed
setDisplayStyleUsingElementsByClass("ytd-rich-grid-renderer", homeFeed);
//Left side bar
setDisplayStyleUsingElementById("guide-renderer", leftSidebar);
setDisplayStyleUsingElementsByClass("ytd-mini-guide-renderer", leftSidebar);
//Recommendations
setDisplayStyleUsingElementById("related", recommendation);
setDisplayStyleUsingElementsByClass("ytp-endscreen-content", recommendation);
//Shorts
setDisplayStyleUsingElementsdByName('ytd-reel-shelf-renderer', shorts);
setDisplayStyleUsingElementById("shorts-container", shorts);
removeShelfByCategory("Shorts", shorts);
}
let observer = new MutationObserver((mutations) => {
removeYoutubeElements();
})
observer.observe(document, {
childList: true,
subtree: true
})
chrome.storage.onChanged.addListener(function (changes, namespace) {
for (let [key, {
oldValue,
newValue
}
] of Object.entries(changes)) {
if (key == "block_comments") {
blockComments = newValue;
}
if (key == "left_side_bar") {
leftSidebar = newValue;
}
if (key == "recommendations") {
recommendation = newValue;
}
if (key == "home_feed") {
homeFeed = newValue;
}
if (key == "shorts") {
shorts = newValue;
}
}
removeYoutubeElements();
});