Disable YouTube Music AutoPause

"Video paused. Continue watching?" will not appear anymore.

Per 26-04-2023. Zie de nieuwste versie.

  1. /*
  2.  
  3. MIT License
  4.  
  5. Copyright 2023 CY Fung
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24.  
  25. */
  26. // ==UserScript==
  27. // @name Disable YouTube Music AutoPause
  28. // @name:en Disable YouTube Music AutoPause
  29. // @name:ja Disable YouTube Music AutoPause
  30. // @name:zh-TW Disable YouTube Music AutoPause
  31. // @name:zh-CN Disable YouTube Music AutoPause
  32. // @namespace http://tampermonkey.net/
  33. // @version 2023.04.26a
  34. // @license MIT License
  35. // @description "Video paused. Continue watching?" will not appear anymore.
  36. // @description:en "Video paused. Continue watching?" will not appear anymore.
  37. // @description:ja 「動画が一時停止されました。続きを視聴しますか?」は二度と起こりません。
  38. // @description:zh-TW 「影片已暫停,要繼續撥放嗎?」不再顯示。
  39. // @description:zh-CN 「视频已暂停。是否继续观看?」不再显示。
  40. // @author CY Fung
  41. // @match https://music.youtube.com/*
  42. // @exclude /^https?://\S+\.(txt|png|jpg|jpeg|gif|xml|svg|manifest|log|ini)[^\/]*$/
  43. // @icon https://raw.githubusercontent.com/cyfung1031/userscript-supports/main/icons/disable-youtube-autopause.svg
  44. // @supportURL https://github.com/cyfung1031/userscript-supports
  45. // @run-at document-start
  46. // @grant none
  47. // @unwrap
  48. // @allFrames
  49. // @inject-into page
  50. // ==/UserScript==
  51.  
  52. /* jshint esversion:8 */
  53.  
  54. (function () {
  55. 'use strict';
  56. const youThereDataHashMapPauseDelay = new WeakMap();
  57. const youThereDataHashMapPromptDelay = new WeakMap();
  58. const websiteName = 'YouTube Music';
  59.  
  60. function hookYouThereData(youThereData) {
  61. if (!youThereData || youThereDataHashMapPauseDelay.has(youThereData)) return;
  62. const retPauseDelay = youThereData.playbackPauseDelayMs;
  63. const retPromptDelay = youThereData.promptDelaySec;
  64. const tenPU = Math.floor(Number.MAX_SAFE_INTEGER * 0.1);
  65. const mPU = Math.floor(tenPU / 1000);
  66. if ('playbackPauseDelayMs' in youThereData && retPauseDelay >= 0 && retPauseDelay < 4 * tenPU) {
  67. youThereDataHashMapPauseDelay.set(youThereData, retPauseDelay);
  68. const retType = typeof retPauseDelay === 'string' ? 2 : +(typeof retPauseDelay === 'number')
  69. if (retType >= 1) {
  70. Object.defineProperty(youThereData, 'playbackPauseDelayMs', {
  71. enumerable: true,
  72. configurable: true,
  73. get() {
  74. Promise.resolve(new Date).then(d => {
  75. console.log(`${websiteName} is trying to pause video...`, d.toLocaleTimeString());
  76. }).catch(console.warn);
  77. let ret = 5 * tenPU;
  78. if (retType === 2) return `${ret}`;
  79. return ret;
  80. },
  81. set(newValue) {
  82. let oldValue = youThereDataHashMapPauseDelay.get(this);
  83. Promise.resolve([oldValue, newValue, new Date]).then(args => {
  84. const [oldValue, newValue, d] = args;
  85. console.log(`${websiteName} is trying to change value 'playbackPauseDelayMs' from ${oldValue} to ${newValue} ...`, d.toLocaleTimeString());
  86. }).catch(console.warn)
  87. youThereDataHashMapPauseDelay.set(this, newValue);
  88. return true;
  89. }
  90. });
  91. }
  92. if (typeof ((youThereData.showPausedActions || 0).length) === 'number' && !youThereData.tvTyh) {
  93. youThereData.tvTyh = []
  94. Object.defineProperty(youThereData, 'showPausedActions', {
  95. enumerable: true,
  96. configurable: true,
  97. get() {
  98. const r = this.tvTyh;
  99. if ((r || 0).length >= 1) r.length = 0;
  100. return r;
  101. },
  102. set(nv) {
  103. return true;
  104. }
  105. })
  106. }
  107. }
  108.  
  109. if ('promptDelaySec' in youThereData && retPromptDelay >= 0 && retPromptDelay < 4 * mPU) {
  110. youThereDataHashMapPromptDelay.set(youThereData, retPromptDelay);
  111. const retType = typeof retPromptDelay === 'string' ? 2 : +(typeof retPromptDelay === 'number')
  112. // this is for YouTube Music to not show the prompt. I guess this should be added to YouTube version too.
  113. // lact -> promptDelaySec -> showDialog -> playbackPauseDelayMs -> pause
  114. if (retType >= 1) {
  115. Object.defineProperty(youThereData, 'promptDelaySec', {
  116. enumerable: true,
  117. configurable: true,
  118. get() {
  119. Promise.resolve(new Date).then(d => {
  120. console.log(`${websiteName} is trying to pause video...`, d.toLocaleTimeString());
  121. }).catch(console.warn);
  122. let ret = 5 * mPU;
  123. if (retType === 2) return `${ret}`;
  124. return ret;
  125. },
  126. set(newValue) {
  127. let oldValue = youThereDataHashMapPromptDelay.get(this);
  128. Promise.resolve([oldValue, newValue, new Date]).then(args => {
  129. const [oldValue, newValue, d] = args;
  130. console.log(`${websiteName} is trying to change value 'playbackPauseDelayMs' from ${oldValue} to ${newValue} ...`, d.toLocaleTimeString());
  131. }).catch(console.warn)
  132. youThereDataHashMapPromptDelay.set(this, newValue);
  133. return true;
  134. }
  135. });
  136. }
  137. }
  138.  
  139. }
  140.  
  141. let symbol877 = Symbol();
  142.  
  143. // e.performDataUpdate -> f.playerData = a.playerResponse;
  144. // youthereDataChanged_(playerData.messages)
  145. // youthereDataChanged_ -> b.youThereRenderer && fFb(this.youThereManager_, b.youThereRenderer)
  146. // a.youThereData_ = b.configData.youThereData;
  147. // a.youThereData_.playbackPauseDelayMs
  148.  
  149. let psChangeRid = 0;
  150.  
  151. function onPlayerStateChange(evtValue) {
  152. if (evtValue == 1 || evtValue == 3) { // the event is just the state value
  153. // this is after getPlayerState()
  154. // in case youThereData is added after onPlayerStateChange
  155. if (psChangeRid > 1e9) psChangeRid = 9;
  156. let tid = psChangeRid;
  157. requestAnimationFrame(() => { // assume no update of messages in background; delayed to save processing energy
  158. if (tid !== psChangeRid) return;
  159. messageHook();
  160. });
  161.  
  162. }
  163. }
  164.  
  165. function messageHook() {
  166.  
  167. let messages = null;
  168. try {
  169. messages = document.querySelector('#player').__data.playerResponse_.messages;
  170. } catch (e) { }
  171. if (messages && messages.length > 0) {
  172. for (const message of messages) {
  173. if (message.youThereRenderer) {
  174. let youThereData = null;
  175. try {
  176. youThereData = message.youThereRenderer.configData.youThereData;
  177. } catch (e) { }
  178. if (youThereData) hookYouThereData(youThereData);
  179. youThereData = null;
  180. break;
  181. }
  182. }
  183. }
  184.  
  185. }
  186.  
  187.  
  188. let messagesRunnerRid = 0;
  189.  
  190. function messagesRunner() {
  191.  
  192.  
  193. messageHook();
  194.  
  195. /*
  196.  
  197. , fhb = function(a, b) {
  198. a.reset();
  199. if (1 === a.JSC$10962_playerApi.getPlayerState() || 3 === a.JSC$10962_playerApi.getPlayerState())
  200. if (a.youThereData = Fa("configData.youThereData", b),
  201. a.youThereData) {
  202. var c = a.JSC$10962_playerApi.getCurrentTime();
  203. a.showPromptJobId = mp(0, function() {
  204. ghb(a, b)
  205. }, 1E3 * Math.max((a.youThereData.promptDelaySec || 0) - c, 0))
  206. }
  207. }
  208.  
  209. */
  210.  
  211. let playerElm = document.querySelector('#player');
  212. if (playerElm && playerElm.playerApi_ && typeof playerElm.playerApi_ == 'object') {
  213. let playerApi = playerElm.playerApi_;
  214.  
  215. if (typeof playerApi[symbol877] === 'undefined' && typeof playerApi.getPlayerState === 'function') {
  216. playerApi[symbol877] = playerApi.getPlayerState;
  217. playerApi.getPlayerState = function () {
  218. let res = this[symbol877](...arguments);
  219. if (res == 1 || res == 3) {
  220. try {
  221. messageHook();
  222. } catch (e) { }
  223. }
  224. return res;
  225. };
  226. }
  227. if ('removeEventListener' in playerApi && 'addEventListener' in playerApi) {
  228. playerApi.removeEventListener("onStateChange", onPlayerStateChange, false);
  229. playerApi.addEventListener("onStateChange", onPlayerStateChange, false);
  230. }
  231. }
  232.  
  233. }
  234.  
  235.  
  236. async function canplayHandlerAsync() {
  237.  
  238. messagesRunnerRid++;
  239. let tid = messagesRunnerRid;
  240.  
  241. await Promise.resolve(0);
  242. if (tid !== messagesRunnerRid) return;
  243. messagesRunner();
  244.  
  245. // run at 3.2s and 8.6s to ensure the page update is finished.
  246. // avoid duplicated calls if canplay is called more than one time (page is rapidly changing)
  247. await new Promise(r => setTimeout(r, 3200));
  248. if (tid !== messagesRunnerRid) return;
  249. messagesRunner();
  250.  
  251. await new Promise(r => setTimeout(r, 5400));
  252. if (tid !== messagesRunnerRid) return;
  253. messagesRunner();
  254.  
  255. }
  256.  
  257. function canplayHandler(evt) {
  258. if (evt.target.nodeName != 'VIDEO') return;
  259. if (!evt.target.closest('#player')) return;
  260. canplayHandlerAsync();
  261. }
  262.  
  263. document.addEventListener('canplay', canplayHandler, true);
  264.  
  265. })();