-
Notifications
You must be signed in to change notification settings - Fork 38
/
autoconfirm.js
160 lines (134 loc) · 4.24 KB
/
autoconfirm.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
const tag = '[Youtube NonStop]';
const isYoutubeMusic = window.location.hostname === 'music.youtube.com';
const popupEventNodename = isYoutubeMusic ? 'YTMUSIC-YOU-THERE-RENDERER' : 'YT-CONFIRM-DIALOG-RENDERER';
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
let appObserver = null;
const appName = isYoutubeMusic ? 'ytmusic-app' : 'ytd-app';
const popupContainer = isYoutubeMusic ? 'ytmusic-popup-container' : 'ytd-popup-container';
let pauseRequested = false;
let pauseRequestedTimeout;
const pauseRequestedTimeoutMillis = 5000;
const idleTimeoutMillis = 5000;
let lastInteractionTime = new Date().getTime();
let videoElement = null;
function log(message) {
console.log(`${tag}[${getTimestamp()}] ${message}`);
}
function debug(message) {
console.debug(`${tag}[${getTimestamp()}] ${message}`);
}
function asDoubleDigit(value) {
return value < 10 ? '0' + value : value;
}
function getTimestamp() {
let dt = new Date();
let time = asDoubleDigit(dt.getHours()) + ':' + asDoubleDigit(dt.getMinutes()) + ':' + asDoubleDigit(dt.getSeconds());
return time;
}
function isIdle() {
return getIdleTime() >= idleTimeoutMillis;
}
function getIdleTime() {
return new Date().getTime() - lastInteractionTime;
}
function listenForMediaKeys() {
if (navigator.mediaSession === undefined) {
log("Your browser doesn't seem to support navigator.mediaSession yet :/");
return;
}
debug('Listening to "pause" media key...');
navigator.mediaSession.setActionHandler('pause', () => {
debug('Paused due to [media key pause]');
pauseVideo();
});
navigator.mediaSession.yns_setActionHandler = navigator.mediaSession.setActionHandler;
navigator.mediaSession.setActionHandler = (action, fn) => {
if (action === 'pause') {
debug("Blocked attempt to override media key 'pause' action");
return;
}
navigator.mediaSession.yns_setActionHandler(action, fn);
};
}
function listenForMouse() {
const eventName = window.PointerEvent ? 'pointer' : 'mouse';
debug(`Using ${eventName} events`);
document.addEventListener(eventName + 'down', (e) => {
processInteraction(eventName + 'down');
});
document.addEventListener(eventName + 'up', (e) => {
processInteraction(eventName + 'up');
});
}
function listenForKeyboard() {
document.addEventListener('keydown', (e) => {
processInteraction('keydown');
});
document.addEventListener('keyup', (e) => {
processInteraction('keyup');
});
}
function processInteraction(action) {
if (pauseRequested) {
debug(`Paused due to [${action}]`);
pauseVideo();
return;
}
lastInteractionTime = new Date().getTime();
}
function observeApp() {
debug(`Observing ${appName}...`);
appObserver = new MutationObserver((mutations, observer) => {
overrideVideoPause();
});
appObserver.observe(document.querySelector(appName), {
childList: true,
subtree: true
});
}
function listenForPopupEvent() {
debug('Listening for popup event...');
document.addEventListener('yt-popup-opened', (e) => {
if (isIdle() && e.detail.nodeName === popupEventNodename) {
debug('[closing popup]');
document.querySelector(popupContainer).click();
pauseVideo();
videoElement.play();
}
});
}
function overrideVideoPause() {
if (videoElement?.yns_pause !== undefined) return;
if (document.querySelector('video') === null) return;
videoElement = document.querySelector('video');
listenForMediaKeys();
debug('Overriding video pause...');
videoElement.yns_pause = videoElement.pause;
videoElement.pause = () => {
debug('Video pause requested');
if (!isIdle()) {
debug('Paused due to [pause]');
pauseVideo();
return;
}
pauseRequested = true;
setPauseRequestedTimeout();
};
}
function setPauseRequestedTimeout(justClear = false) {
clearTimeout(pauseRequestedTimeout);
if (justClear) return;
pauseRequestedTimeout = setTimeout(() => {
pauseRequested = false;
}, pauseRequestedTimeoutMillis);
}
function pauseVideo() {
videoElement?.yns_pause();
pauseRequested = false;
setPauseRequestedTimeout(true);
}
listenForMouse();
listenForKeyboard();
listenForPopupEvent();
observeApp();
log(`Monitoring YouTube ${isYoutubeMusic ? 'Music ' : ''}for 'Confirm watching?' action...`);