-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserve.js
400 lines (381 loc) · 17 KB
/
observe.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
function hasClass(element, className) {
return (' ' + element.className + ' ').indexOf(' ' + className + ' ') > -1;
}
function findAncs(el, cls=0, clparam=0) {
try {
if (clparam==0) {
while ((el = el.parentNode) && !hasClass(el, cls));
} else {
while ((el = el.parentNode) && (el.tagName != clparam.toUpperCase()));
}
return el;
} catch(err) {
console.warn("%c findAncs() func was unable to find a parent element with the pre-defined className. If you are interested in helping fix this issue, please report this to https://github.com/robertfrosty/shift-yt-optimizer", "background: #222222;color: cyan; font-weight: bold;");
return false;
}
}
function attrQueryAll(cls=null, tag=null, attr, qterm) {
if (cls) {
var attrll = document.getElementsByClassName(cls);
} else if (tag) {
var attrll = document.querySelectorAll(tag);
}
let newarr = new Array();
for(i=0; i<attrll.length;i++) {
if(attrll[i].getAttribute(attr) == qterm) {
newarr.push(attrll[i]);
}
}
return newarr;
}
function resetReapply() {
// Reset everything
let toremove = document.getElementsByClassName('shift-yt-wm-deleteme');
for(let i=toremove.length-1;i>=0;i--){toremove[i].style.display="block";toremove[i].style.opacity="1";toremove[i].classList.remove('shift-yt-wm-deleteme')};
let toremoven = document.getElementsByClassName('shift-yt-wm-used');
for(let ii=toremoven.length-1;ii>=0;ii--){toremoven[ii].classList.remove('shift-yt-wm-used')};
document.body.setAttribute('async-inject-listener', 'false');
//Reapply hideWorkMode() - should just be able to call func, it will check for incl / exclude
hideWorkMode();
}
const notsyncGet = async (key) => {
return new Promise((resolve, reject) => {
chrome.storage.sync.get([key], function(obj) {
if(obj[key] === 'undefined') {
reject();
} else {
resolve(obj[key]);
}
});
});
};
async function hideWorkMode(elist=null) {
if(!elist) {
elist = await notsyncGet('incl_tags');
}
exclude = await notsyncGet('excl');
for(tag in elist) {
if(elist[tag].length > 0) {
let e = elist[tag].trim();
let locname = window.location.pathname.split('/');
if(e.includes(">")) {
} else { // search channels and video titles
if(locname[1] == 'watch') { //watching video
hideparams = 'ytd-compact-video-renderer';
allmeta = document.querySelectorAll("div.style-scope.metadata:not(.shift-yt-wm-used):not(.ytd-miniplayer)");
textparams = ["yt-formatted-string[id='text']"];
} else if (locname[1] == 'results'){
hideparams = 'ytd-video-renderer';
allmeta = document.querySelector('ytd-search').querySelectorAll("div.text-wrapper:not(.shift-yt-wm-used)");
textparams = ["a[id='video-title']", "yt-formatted-string[id='text']", "yt-formatted-string.metadata-snippet-text"];
} else {
if(locname[1] == '') { //homepage
hideparams = 'ytd-rich-item-renderer';
allmeta = document.querySelectorAll("div.ytd-rich-grid-media:not(.shift-yt-wm-used)[id='meta']");
textparams = ["yt-formatted-string[id='video-title']", "yt-formatted-string[id='text']"];
} else if (locname[1] == 'c' || locname[1] == 'user' || locname[1] == 'channel' || locname[1].includes("@")) { //someone's channel
hideparams = 'ytd-rich-item-renderer';
allmeta = document.querySelectorAll("div.ytd-rich-grid-media:not(.shift-yt-wm-used)[id='meta']");
textparams = ["yt-formatted-string[id='video-title']", "yt-formatted-string[id='text']"];
} else if (locname[1] == 'feed') { //explore
if(locname[2] == 'subscriptions' || locname[2] == 'library') {
allmeta = document.querySelectorAll("div.ytd-rich-grid-media:not(.shift-yt-wm-used):not(.ytd-mini-player):not(.ytd-watch-flexy)[id='meta']");
hideparams = 'ytd-rich-item-renderer';
textparams = ["yt-formatted-string[id='video-title']", "yt-formatted-string[id='text']"];
} else {
allmeta = document.querySelectorAll("div.text-wrapper:not(.shift-yt-wm-used)");
hideparams = 'ytd-video-renderer';
textparams = ["a[id='video-title']", "yt-formatted-string[id='text']", "yt-formatted-string[id='description-text']"];
}
} else if (locname[1].includes('playlist')) { //watching playlist
hideparams = 'ytd-playlist-video-renderer';
allmeta = document.querySelectorAll("div.ytd-playlist-video-renderer:not(.shift-yt-wm-used)[id='meta']");
textparams = ["a[id='video-title']", "yt-formatted-string[id='text']"];
}
}
let re = new RegExp(e, 'i');
for(i=0; i<allmeta.length;i++) {
for(text in textparams) {
if(exclude) {
if(allmeta[i].querySelector(textparams[text]).innerText.match(re)) { // if video title contains words to remove, hide whole video
let torem = findAncs(allmeta[i], null, hideparams);
torem.classList.add('shift-yt-wm-deleteme', e);
torem.style.opacity = "0";
setTimeout(() => {torem.style.display = "none"}, 1000);
allmeta[i].classList.add('shift-yt-wm-used');
break;
}
} else {
if(allmeta[i].querySelector(textparams[text]).innerText.match(re)) { // if video title does NOT contains words to remove, hide whole video
allmeta[i].setAttribute('shift-save-for-later', true);
break;
}
if(text == textparams.length - 1) { //If this is last item in textparams list then hide the video
if(allmeta[i].getAttribute('shift-save-for-later')) {
break;
}
let torem = findAncs(allmeta[i], null, hideparams);
torem.classList.add('shift-yt-wm-deleteme', e);
torem.style.opacity = "0";
setTimeout(() => {torem.style.display = "none"}, 1000);
allmeta[i].classList.add('shift-yt-wm-used');
}
}
}
}
}
}
}
}
function hideAds(){
var hideparam = null;
var tohide = null;
if(window.location.pathname.split('/')[1] ==''){
hideparam = ["ytd-rich-grid-renderer"];
tohide = document.querySelector("ytd-browse").querySelectorAll("ytd-ad-slot-renderer");
for(i=0;i<tohide.length;i++){
ancCont="";
ancCont = findAncs(tohide[i], hideparam);
if(ancCont) {
ancCont.classList.add('shift-yt-ads-deleteme');
ancCont.style.opacity = "0";
setTimeout(function(ancCont){ancCont.style.display="none"}, 1000, ancCont);
tohide[i].classList.add('shift-yt-ads-used');
}
}
}
}
function hideMixes(){
var hideparam = null;
var tohide = null;
if(window.location.pathname.split('/')[1] ==''){
hideparam = ["ytd-rich-grid-renderer"];
tohide = document.querySelector("ytd-browse").querySelectorAll("yt-collection-thumbnail-view-model");
for(i=0;i<tohide.length;i++){
ancCont="";
ancCont = findAncs(tohide[i], hideparam);
if(ancCont) {
ancCont.classList.add('shift-yt-mix-deleteme');
ancCont.style.opacity = "0";
setTimeout(function(ancCont){ancCont.style.display="none"}, 1000, ancCont);
tohide[i].classList.add('shift-yt-mix-used');
}
}
}
}
function hideWatched(hideparam=['ytd-rich-grid-renderer']) {
if(window.location.href.split("/")[3] == 'playlist?list=LL') {
console.log("shift-yt-optimizer has been disabled for this page.");
return;
} else if (window.location.href.split("/")[4] == "history"){
console.log("shift-yt-optimizer has been disabled for this page.");
return;
}
let locname = window.location.pathname.split('/');
var tohide = null;
var flag = (yttype == 'ytd' ? true : false);
if (flag) { // Desktop version
if(locname[1] == 'watch') { //watching video
hideparam=['ytd-item-section-renderer'];
tohide = attrQueryAll(null, 'ytd-watch-flexy', 'role', 'main')[0].querySelectorAll('ytd-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
} else if (locname[1] == 'results'){
hideparam=['ytd-vertical-list-renderer', 'ytd-item-section-renderer'];
tohide = attrQueryAll(null, 'ytd-search', 'role', 'main')[0].querySelectorAll('ytd-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
} else {
if(locname[1] == '') { //homepage
hideparam=['ytd-rich-grid-renderer'];
tohide = attrQueryAll(null, 'ytd-browse', 'role', 'main')[0].querySelectorAll('ytd-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)'); //yt-thumbnail-overlay-progress-bar-view-model:not(.shift-yt-used)
} else if (locname[1] == 'c' || locname[1] == 'user' || locname[1] == 'channel' || locname[1].includes("@")) { //someone's channel
if (locname[locname.length - 1] == 'videos') {
hideparam=['ytd-rich-grid-renderer'];
} else {
hideparam=['yt-horizontal-list-renderer'];
}
tohide = attrQueryAll(null, 'ytd-browse', 'role', 'main')[0].querySelectorAll('ytd-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
} else if (locname[1] == 'feed') { //explore
if(locname[2] == 'explore' || locname[2] == 'trending') {
hideparam = ['ytd-expanded-shelf-contents-renderer'];
} else if (locname[2] == 'history') {
hideparam = ['ytd-item-section-renderer']
}
else {
hideparam = ['ytd-grid-renderer'];
}
tohide = attrQueryAll(null, 'ytd-browse', 'role', 'main')[0].querySelectorAll('ytd-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
} else if (locname[1].includes('playlist')) { //watching playlist
hideparam=['ytd-playlist-video-list-renderer'];
tohide = attrQueryAll(null, 'ytd-browse', 'role', 'main')[0].querySelectorAll('ytd-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
}
}
if(tohide.length>0) {
for (let i=0;i<tohide.length;i++) {
for(let ii=0;ii<hideparam.length;ii++) {
ancCont = "";
ancCont = findAncs(tohide[i], hideparam[ii]);
if(ancCont) {
ancCont.classList.add('shift-yt-deleteme');
ancCont.style.opacity = "0";
setTimeout(function(ancCont){ancCont.style.display="none"}, 1000, ancCont);
tohide[i].classList.add('shift-yt-used');
}
}
}
console.log("%c ______ __ __ __ ______ ______ \n/\\ ___\\/\\ \\_\\ \\/\\ \\/\\ ___\\/\\__ _\\ \n\\ \\___ \\ \\ __ \\ \\ \\ \\ __\\\\/_/\\ \\/ \n \\/\\_____\\ \\_\\ \\_\\ \\_\\ \\_\\ \\ \\_\\\n \\/_____/\\/_/\\/_/\\/_/\\/_/ \\/_/ \n\n", "color: #2196F3;");
}
} else { // Mobile Version
if(locname[1] == 'watch') { //watching video
hideparam=['ytm-video-with-context-renderer'];
tohide = document.querySelector('ytm-watch').querySelectorAll('ytm-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
} else if (locname[1] == 'results'){
hideparam=['ytm-compact-video-renderer'];
tohide = document.querySelector('ytm-search').querySelectorAll('ytm-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
} else {
if(locname[1] == '') { //homepage
hideparam=['ytm-rich-item-renderer']
tohide = document.querySelector('ytm-browse').querySelectorAll('ytm-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
} else if (locname[1] == 'c' || locname[1] == 'user' || locname[1] == 'channel') { //someone's channel
hideparam=['ytm-compact-video-renderer'];
tohide = document.querySelector('ytm-browse').querySelectorAll('ytm-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
} else if (locname[1] == 'feed') { //explore
hideparam=['ytm-item-section-renderer'];
tohide = document.querySelector('ytm-browse').querySelectorAll('ytm-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
} else if (locname[1].includes('playlist')) { //watching playlist
hideparam=['ytm-playlist-video-renderer'];
tohide = document.querySelector('ytm-browse').querySelectorAll('ytm-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
}
}
if(tohide.length>0) {
for (let i=0;i<tohide.length;i++) {
for(let ii=0;ii<hideparam.length;ii++) {
var ancCont = findAncs(tohide[i], null, hideparam[ii]); // Search using tagName instead of className
if(ancCont) {
ancCont.classList.add('shift-yt-deleteme');
ancCont.style.opacity = "0";
setTimeout(function(){findAncs(tohide[i], null, hideparam[ii]).style.display = "none"}, 1000);
tohide[i].classList.add('shift-yt-used');
}
}
}
console.log("%c ______ __ __ __ ______ ______ \n/\\ ___\\/\\ \\_\\ \\/\\ \\/\\ ___\\/\\__ _\\ \n\\ \\___ \\ \\ __ \\ \\ \\ \\ __\\\\/_/\\ \\/ \n \\/\\_____\\ \\_\\ \\_\\ \\_\\ \\_\\ \\ \\_\\\n \\/_____/\\/_/\\/_/\\/_/\\/_/ \\/_/ \n\n", "color: #2196F3;");
}
}
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.ftr == 'work-mode') {
if(request.checkval) {
if(request.insparams) {
hideWorkMode(request.insparams);
}
} else {
if(request.remparams) { //remove params are defined
let r = request.remparams.trim();
let toremove = document.getElementsByClassName(r);
for(let i=toremove.length-1;i>=0;i--){
toremove[i].style.display="block";
toremove[i].style.opacity="1";
toremove[i].classList.remove('shift-yt-wm-deleteme');
toremove[i].querySelector('.shift-yt-wm-used').classList.remove('shift-yt-wm-used');
}
} else { // no remove params, remove everything
let toremove = document.getElementsByClassName('shift-yt-wm-deleteme');
for(let i=toremove.length-1;i>=0;i--){toremove[i].style.display="block";toremove[i].style.opacity="1";toremove[i].classList.remove('shift-yt-wm-deleteme')};
let toremoven = document.getElementsByClassName('shift-yt-wm-used');
for(let ii=toremoven.length-1;ii>=0;ii--){toremoven[ii].classList.remove('shift-yt-wm-used')};
document.body.setAttribute('async-inject-listener', 'false');
}
}
}
}
)
function scrollHide(e) {
if(this.oldScroll < this.scrollY) {
chrome.storage.sync.get('mK', function(obj) {
if (obj['mK'] > 0) {
hideWatched();
}
});
chrome.storage.sync.get('wM', function(obj) {
if (obj['wM'] > 0) {
hideWorkMode();
}
});
chrome.storage.sync.get('adMode', function(obj) {
if(obj['adMode']){
hideAds();
}
})
}
this.oldScroll = this.scrollY;
}
function loadFunc() {
stylesh = document.createElement('style');
stylesh.type = 'text/css';
stylesh.innerHTML = `.shift-yt-used {display: none;} ${yttype}-video-with-context-renderer, ${yttype}-rich-item-renderer, ${yttype}-grid-renderer, ${yttype}-grid-video-renderer, ${yttype}-compact-video-renderer, ${yttype}-playlist-video-renderer, ${yttype}-video-renderer, ${yttype}-shelf-renderer {-webkit-transition: opacity 1s ease-in-out;-moz-transition: opacity 1s ease-in-out;-ms-transition: opacity 1s ease-in-out;-o-transition: opacity 1s ease-in-out;}`;
document.getElementsByTagName('head')[0].appendChild(stylesh);
window.addEventListener('scroll', scrollHide);
}
if(window.location.href.split("//")[1].split(".")[0] == 'm'|| document.querySelector('ytm-app')) {
yttype = 'ytm'; }
else {
yttype = 'ytd';
}
if(yttype == 'ytd') {
var loadObs = document.querySelectorAll(`${yttype}-app`)[0];
lobserver = new MutationObserver(function(mutationsList, lobserver) {
elemObs = document.querySelector('yt-page-navigation-progress');
observer = new MutationObserver(function(mutationsList, observer) {
if(elemObs.hidden) {
loadFunc();
let toremoven = document.getElementsByClassName('shift-yt-used');
for(let ii=toremoven.length-1;ii>=0;ii--){toremoven[ii].classList.remove('shift-yt-used')};
chrome.storage.sync.get('mK', function(obj) {
if (obj['mK'] > 0) {
hideWatched();
}
});
chrome.storage.sync.get('wM', function(obj) {
if (obj['wM'] > 0) {
hideWorkMode();
}
});
}
});
if(document.querySelector('yt-page-navigation-progress')){
observer.observe(elemObs, {characterData:true, childList:true, attributes: true});
}
chrome.storage.sync.get('mK', function(obj) {
if (obj['mK'] > 0) {
hideWatched();
}
});
chrome.storage.sync.get('wM', function(obj) {
if (obj['wM'] > 0) {
hideWorkMode();
}
});
});
lobserver.observe(loadObs, {characterData:true, childList:true, attributes: true});
} else {
window.addEventListener('load', () => {
elemObs = document.querySelector('div.spinner');
observer = new MutationObserver(function(mutationsList, observer) {
if(elemObs.hidden) {
let toremoven = document.getElementsByClassName('shift-yt-used');
for(let ii=toremoven.length-1;ii>=0;ii--){toremoven[ii].classList.remove('shift-yt-used')};
chrome.storage.sync.get('mK', function(obj) {
if (obj['mK'] > 0) {
hideWatched();
}
});
}
});
observer.observe(elemObs, {characterData:true, childList:true, attributes: true});
chrome.storage.sync.get('mK', function(obj) {
if (obj['mK'] > 0) {
hideWatched();
}
});
});
}
loadFunc();