-
Notifications
You must be signed in to change notification settings - Fork 5
/
disable-youtube-music-autopause.user.js
292 lines (243 loc) · 11.3 KB
/
disable-youtube-music-autopause.user.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
/*
MIT License
Copyright 2023 CY Fung
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// ==UserScript==
// @name Disable YouTube Music AutoPause
// @name:en Disable YouTube Music AutoPause
// @name:ja Disable YouTube Music AutoPause
// @name:zh-TW Disable YouTube Music AutoPause
// @name:zh-CN Disable YouTube Music AutoPause
// @namespace http://tampermonkey.net/
// @version 2023.12.01.0
// @license MIT License
// @description "Video paused. Continue watching?" and "Still watching? Video will pause soon" will not appear anymore.
// @description:en "Video paused. Continue watching?" and "Still watching? Video will pause soon" will not appear anymore.
// @description:ja 「動画が一時停止されました。続きを視聴しますか?」と「視聴を続けていますか?動画がまもなく一時停止されます」は二度と起こりません。
// @description:zh-TW 「影片已暫停,要繼續觀賞嗎?」和「你還在螢幕前嗎?影片即將暫停播放」不再顯示。
// @description:zh-CN 「视频已暂停。是否继续观看?」和「仍在观看?视频即将暂停」不再显示。
// @author CY Fung
// @match https://music.youtube.com/*
// @exclude /^https?://\S+\.(txt|png|jpg|jpeg|gif|xml|svg|manifest|log|ini)[^\/]*$/
// @icon https://raw.githubusercontent.com/cyfung1031/userscript-supports/main/icons/disable-youtube-autopause.svg
// @supportURL https://github.com/cyfung1031/userscript-supports
// @run-at document-start
// @grant none
// @unwrap
// @allFrames true
// @inject-into page
// ==/UserScript==
/* jshint esversion:8 */
(function (__Promise__) {
'use strict';
/** @type {globalThis.PromiseConstructor} */
const Promise = (async () => { })().constructor; // YouTube hacks Promise in WaterFox Classic and "Promise.resolve(0)" nevers resolve.
const youThereDataHashMapPauseDelay = new WeakMap();
const youThereDataHashMapPromptDelay = new WeakMap();
const youThereDataHashMapLactThreshold = new WeakMap();
const websiteName = 'YouTube Music';
let noDelayLogUntil = 0;
const insp = o => o ? (o.polymerController || o.inst || o || 0) : (o || 0);
const indr = o => insp(o).$ || o.$ || 0;
function delayLog(...args) {
if (Date.now() < noDelayLogUntil) return;
noDelayLogUntil = Date.now() + 280; // avoid duplicated delay log in the same time ticker
console.log(...args);
}
function defineProp1(youThereData, key, retType, constVal, fGet, fSet, hashMap) {
Object.defineProperty(youThereData, key, {
enumerable: true,
configurable: true,
get() {
Promise.resolve(new Date).then(fGet).catch(console.warn);
let ret = constVal;
if (retType === 2) return `${ret}`;
return ret;
},
set(newValue) {
let oldValue = hashMap.get(this);
Promise.resolve([oldValue, newValue, new Date]).then(fSet).catch(console.warn);
hashMap.set(this, newValue);
return true;
}
});
}
function defineProp2(youThereData, key, qKey) {
Object.defineProperty(youThereData, key, {
enumerable: true,
configurable: true,
get() {
const r = this[qKey];
if ((r || 0).length >= 1) r.length = 0;
return r;
},
set(nv) {
return true;
}
});
}
function hookYouThereData(youThereData) {
if (!youThereData || youThereDataHashMapPauseDelay.has(youThereData)) return;
const retPauseDelay = youThereData.playbackPauseDelayMs;
const retPromptDelay = youThereData.promptDelaySec;
const retLactThreshold = youThereData.lactThresholdMs;
const tenPU = Math.floor(Number.MAX_SAFE_INTEGER * 0.1);
const mPU = Math.floor(tenPU / 1000);
if ('playbackPauseDelayMs' in youThereData && retPauseDelay >= 0 && retPauseDelay < 4 * tenPU) {
youThereDataHashMapPauseDelay.set(youThereData, retPauseDelay);
const retType = typeof retPauseDelay === 'string' ? 2 : +(typeof retPauseDelay === 'number');
if (retType >= 1) {
defineProp1(youThereData, 'playbackPauseDelayMs', retType, 5 * tenPU, d => {
delayLog(`${websiteName} is trying to pause video...`, d.toLocaleTimeString());
}, args => {
const [oldValue, newValue, d] = args;
console.log(`${websiteName} is trying to change value 'playbackPauseDelayMs' from ${oldValue} to ${newValue} ...`, d.toLocaleTimeString());
}, youThereDataHashMapPauseDelay);
}
if (typeof ((youThereData.showPausedActions || 0).length) === 'number' && !youThereData.tvTyh) {
youThereData.tvTyh = [];
defineProp2(youThereData, 'showPausedActions', 'tvTyh');
}
}
if ('promptDelaySec' in youThereData && retPromptDelay >= 0 && retPromptDelay < 4 * mPU) {
youThereDataHashMapPromptDelay.set(youThereData, retPromptDelay);
const retType = typeof retPromptDelay === 'string' ? 2 : +(typeof retPromptDelay === 'number');
// lact -> promptDelaySec -> showDialog -> playbackPauseDelayMs -> pause
if (retType >= 1) {
defineProp1(youThereData, 'promptDelaySec', retType, 5 * mPU, d => {
delayLog(`${websiteName} is trying to pause video...`, d.toLocaleTimeString());
}, args => {
const [oldValue, newValue, d] = args;
console.log(`${websiteName} is trying to change value 'promptDelaySec' from ${oldValue} to ${newValue} ...`, d.toLocaleTimeString());
}, youThereDataHashMapPromptDelay);
}
}
if ('lactThresholdMs' in youThereData && retLactThreshold >= 0 && retLactThreshold < 4 * tenPU) {
youThereDataHashMapLactThreshold.set(youThereData, retLactThreshold);
const retType = typeof retLactThreshold === 'string' ? 2 : +(typeof retLactThreshold === 'number');
// lact -> promptDelaySec -> showDialog -> playbackPauseDelayMs -> pause
if (retType >= 1) {
defineProp1(youThereData, 'lactThresholdMs', retType, 5 * tenPU, d => {
// console.log(`${websiteName} is trying to pause video...`, d.toLocaleTimeString());
}, args => {
const [oldValue, newValue, d] = args;
console.log(`${websiteName} is trying to change value 'lactThresholdMs' from ${oldValue} to ${newValue} ...`, d.toLocaleTimeString());
}, youThereDataHashMapLactThreshold);
}
}
}
let symbol877 = Symbol();
// e.performDataUpdate -> f.playerData = a.playerResponse;
// youthereDataChanged_(playerData.messages)
// youthereDataChanged_ -> b.youThereRenderer && fFb(this.youThereManager_, b.youThereRenderer)
// a.youThereData_ = b.configData.youThereData;
// a.youThereData_.playbackPauseDelayMs
let psChangeRid = 0;
function onPlayerStateChange(evtValue) {
if (evtValue == 1 || evtValue == 3) { // the event is just the state value
// this is after getPlayerState()
// in case youThereData is added after onPlayerStateChange
if (psChangeRid > 1e9) psChangeRid = 9;
let tid = psChangeRid;
requestAnimationFrame(() => { // assume no update of messages in background; delayed to save processing energy
if (tid !== psChangeRid) return;
messageHook();
});
}
}
function messageHook() {
let messages = null;
const playerElm = document.querySelector('#player') || 0;
try {
messages = (insp(playerElm).__data || playerElm.__data || 0).playerResponse_.messages;
} catch (e) { }
if (messages && messages.length > 0) {
for (const message of messages) {
if (message.youThereRenderer) {
let youThereData = null;
try {
youThereData = message.youThereRenderer.configData.youThereData;
} catch (e) { }
if (youThereData) hookYouThereData(youThereData);
youThereData = null;
break;
}
}
}
}
let messagesRunnerRid = 0;
function messagesRunner() {
messageHook();
/*
, fhb = function(a, b) {
a.reset();
if (1 === a.JSC$10962_playerApi.getPlayerState() || 3 === a.JSC$10962_playerApi.getPlayerState())
if (a.youThereData = Fa("configData.youThereData", b),
a.youThereData) {
var c = a.JSC$10962_playerApi.getCurrentTime();
a.showPromptJobId = mp(0, function() {
ghb(a, b)
}, 1E3 * Math.max((a.youThereData.promptDelaySec || 0) - c, 0))
}
}
*/
const playerElm = document.querySelector('#player') || 0;
const playerApi = insp(playerElm).playerApi_ || playerElm.playerApi_ || insp(playerElm).playerApi || playerElm.playerApi || 0;
if (typeof playerApi === 'object') {
if (typeof playerApi[symbol877] === 'undefined' && typeof playerApi.getPlayerState === 'function') {
playerApi[symbol877] = playerApi.getPlayerState;
playerApi.getPlayerState = function () {
let res = this[symbol877](...arguments);
if (res == 1 || res == 3) {
try {
messageHook();
} catch (e) { }
}
return res;
};
}
if ('removeEventListener' in playerApi && 'addEventListener' in playerApi) {
playerApi.removeEventListener("onStateChange", onPlayerStateChange, false);
playerApi.addEventListener("onStateChange", onPlayerStateChange, false);
}
}
}
async function canplayHandlerAsync() {
noDelayLogUntil = Date.now() + 3400; // no delay log for video changes
messagesRunnerRid++;
let tid = messagesRunnerRid;
if (messagesRunnerRid > 1e9) messagesRunnerRid = 9;
await Promise.resolve(0);
if (tid !== messagesRunnerRid) return;
messagesRunner();
// run at 3.2s and 8.6s to ensure the page update is finished.
// avoid duplicated calls if canplay is called more than one time (page is rapidly changing)
await new Promise(r => setTimeout(r, 3200));
if (tid !== messagesRunnerRid) return;
messagesRunner();
await new Promise(r => setTimeout(r, 5400));
if (tid !== messagesRunnerRid) return;
messagesRunner();
}
function canplayHandler(evt) {
if (evt.target.nodeName != 'VIDEO') return;
if (!evt.target.closest('#player')) return;
canplayHandlerAsync();
}
document.addEventListener('canplay', canplayHandler, true);
})(Promise);