-
Notifications
You must be signed in to change notification settings - Fork 2
/
inject.js
45 lines (40 loc) · 1.23 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
(function () {
var isLoadDispatched = false;
var retryCount = 100;
var failSafeTimeout = null;
readyComments = function (data) {
// console.log('Ready Comments');
isLoadDispatched = false;
clearTimeout(failSafeTimeout);
window.postMessage({ comments: data }, '*');
}
getComments = function (count) {
// console.log('Get comments', count);
var comments = document.querySelector('ytd-comments#comments');
if (!isLoadDispatched && comments && comments.$) {
comments.setAttribute('style', 'position: absolute; top: 0; z-index: -999;');
window.dispatchEvent(new Event('scroll'));
isLoadDispatched = true;
// Failsafe
failSafeTimeout = setTimeout(function () {
comments.removeAttribute('style');
}, 5000);
}
if (comments && comments.$ && comments.$.sections && comments.$.sections.items_.length > 1) {
comments.removeAttribute('style');
readyComments(comments.$.sections.items_);
} else if (count < retryCount) {
setTimeout(function () {
getComments(++count);
}, 200);
}
}
window.onload = function () {
// console.log('Loaded Inject');
window.addEventListener('message', function (event) {
if (event.data && event.data.requestComments) {
getComments(0);
}
}, false);
}
})();