forked from sniklaus/youtube-watchmarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.js
89 lines (65 loc) · 2.85 KB
/
youtube.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
'use strict';
var strLastchange = null;
var objVideocache = [];
var objProgresscache = [];
var boolMarkcache = {};
// ##########################################################
var refresh = function() {
for (var objProgress of objProgresscache) {
var objVideo = objProgress.parentNode.parentNode;
if (objVideo.matches('a.ytd-thumbnail[href^="/watch?v="], a.ytd-thumbnail[href^="/shorts/"]') === false) {
continue;
}
var strIdent = objVideo.href.split('&')[0].slice(-11);
if (boolMarkcache.hasOwnProperty(strIdent) === false) {
boolMarkcache[strIdent] = false;
}
if (boolMarkcache[strIdent] === false) {
boolMarkcache[strIdent] = true;
chrome.runtime.sendMessage({
'strMessage': 'youtubeEnsure',
'strIdent': strIdent,
'strTitle': document.querySelector('a[title][href^="/watch?v=' + strIdent + '"], a[title][href^="/shorts/' + strIdent + '"]').title
});
}
}
for (var objVideo of objVideocache) {
var strIdent = objVideo.href.split('&')[0].slice(-11);
if (boolMarkcache.hasOwnProperty(strIdent) === false) {
boolMarkcache[strIdent] = false;
}
mark(objVideo, boolMarkcache[strIdent]);
}
};
var mark = function(objVideo, boolMark) {
if ((boolMark === true) && (objVideo.classList.contains('youwatch-mark') === false)) {
objVideo.classList.add('youwatch-mark');
} else if ((boolMark !== true) && (objVideo.classList.contains('youwatch-mark') !== false)) {
objVideo.classList.remove('youwatch-mark');
}
};
// ##########################################################
chrome.runtime.onMessage.addListener(function(objData) {
if (objData.strMessage === 'youtubeRefresh') {
refresh();
}
if (objData.strMessage === 'youtubeMark') {
boolMarkcache[objData.strIdent] = true;
for (var objVideo of document.querySelectorAll('a.ytd-thumbnail[href^="/watch?v=' + objData.strIdent + '"], a.ytd-thumbnail[href^="/shorts/' + objData.strIdent + '"]')) {
mark(objVideo, true);
}
}
});
// ##########################################################
window.setInterval(function() {
if (document.hidden === true) {
return;
}
objVideocache = window.document.querySelectorAll('a.ytd-thumbnail[href^="/watch?v="], a.ytd-thumbnail[href^="/shorts/"]');
objProgresscache = window.document.querySelectorAll('ytd-thumbnail-overlay-resume-playback-renderer');
if (strLastchange === window.location.href + ':' + window.document.title + ':' + objVideocache.length + ':' + objProgresscache.length) {
return;
}
strLastchange = window.location.href + ':' + window.document.title + ':' + objVideocache.length + ':' + objProgresscache.length;
refresh();
}, 300);