Disable YouTube AutoPause

"Video paused. Continue watching?" and "Still watching? Video will pause soon" will not appear anymore.

  1. /*
  2.  
  3. MIT License
  4.  
  5. Copyright 2022 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 AutoPause
  28. // @name:en Disable YouTube AutoPause
  29. // @name:ja Disable YouTube AutoPause
  30. // @name:zh-TW Disable YouTube AutoPause
  31. // @name:zh-CN Disable YouTube AutoPause
  32. // @namespace http://tampermonkey.net/
  33. // @version 2024.02.21.0
  34. // @license MIT License
  35. // @description "Video paused. Continue watching?" and "Still watching? Video will pause soon" will not appear anymore.
  36. // @description:en "Video paused. Continue watching?" and "Still watching? Video will pause soon" will not appear anymore.
  37. // @description:ja 「動画が一時停止されました。続きを視聴しますか?」と「視聴を続けていますか?動画がまもなく一時停止されます」は二度と起こりません。
  38. // @description:zh-TW 「影片已暫停,要繼續觀賞嗎?」和「你還在螢幕前嗎?影片即將暫停播放」不再顯示。
  39. // @description:zh-CN 「视频已暂停。是否继续观看?」和「仍在观看?视频即将暂停」不再显示。
  40. // @author CY Fung
  41. // @match https://www.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 true
  49. // @inject-into page
  50. // ==/UserScript==
  51.  
  52. /* jshint esversion:8 */
  53.  
  54. (function (__Promise__) {
  55. 'use strict';
  56.  
  57. /** @type {globalThis.PromiseConstructor} */
  58. const Promise = (async () => { })().constructor; // YouTube hacks Promise in WaterFox Classic and "Promise.resolve(0)" nevers resolve.
  59.  
  60. const youThereDataHashMapPauseDelay = new WeakMap();
  61. const youThereDataHashMapPromptDelay = new WeakMap();
  62. const youThereDataHashMapLactThreshold = new WeakMap();
  63. const websiteName = 'YouTube';
  64. let noDelayLogUntil = 0;
  65.  
  66. const insp = o => o ? (o.polymerController || o.inst || o || 0) : (o || 0);
  67. const indr = o => insp(o).$ || o.$ || 0;
  68.  
  69. function delayLog(...args) {
  70. if (Date.now() < noDelayLogUntil) return;
  71. noDelayLogUntil = Date.now() + 280; // avoid duplicated delay log in the same time ticker
  72. console.log(...args);
  73. }
  74.  
  75. function defineProp1(youThereData, key, retType, constVal, fGet, fSet, hashMap) {
  76. Object.defineProperty(youThereData, key, {
  77. enumerable: true,
  78. configurable: true,
  79. get() {
  80. Promise.resolve(new Date).then(fGet).catch(console.warn);
  81. const ret = constVal;
  82. return retType === 2 ? `${ret}` : ret;
  83. },
  84. set(newValue) {
  85. const oldValue = hashMap.get(this);
  86. Promise.resolve([oldValue, newValue, new Date]).then(fSet).catch(console.warn);
  87. hashMap.set(this, newValue);
  88. return true;
  89. }
  90. });
  91. }
  92.  
  93. function defineProp2(youThereData, key, qKey) {
  94. Object.defineProperty(youThereData, key, {
  95. enumerable: true,
  96. configurable: true,
  97. get() {
  98. const r = this[qKey];
  99. if ((r || 0).length >= 1) r.length = 0;
  100. return r;
  101. },
  102. set(nv) {
  103. return true;
  104. }
  105. });
  106. }
  107.  
  108. function hookYouThereData(youThereData) {
  109. if (!youThereData || youThereDataHashMapPauseDelay.has(youThereData)) return;
  110. const retPauseDelay = youThereData.playbackPauseDelayMs;
  111. const retPromptDelay = youThereData.promptDelaySec;
  112. const retLactThreshold = youThereData.lactThresholdMs;
  113. const tenPU = Math.floor(Number.MAX_SAFE_INTEGER * 0.1);
  114. const mPU = Math.floor(tenPU / 1000);
  115.  
  116. if ('playbackPauseDelayMs' in youThereData && retPauseDelay >= 0 && retPauseDelay < 4 * tenPU) {
  117. youThereDataHashMapPauseDelay.set(youThereData, retPauseDelay);
  118. const retType = typeof retPauseDelay === 'string' ? 2 : +(typeof retPauseDelay === 'number');
  119. if (retType >= 1) {
  120. defineProp1(youThereData, 'playbackPauseDelayMs', retType, 5 * tenPU, d => {
  121. delayLog(`${websiteName} is trying to pause video...`, d.toLocaleTimeString());
  122. }, args => {
  123. const [oldValue, newValue, d] = args;
  124. console.log(`${websiteName} is trying to change value 'playbackPauseDelayMs' from ${oldValue} to ${newValue} ...`, d.toLocaleTimeString());
  125. }, youThereDataHashMapPauseDelay);
  126. }
  127. if (typeof ((youThereData.showPausedActions || 0).length) === 'number' && !youThereData.tvTyh) {
  128. youThereData.tvTyh = [];
  129. defineProp2(youThereData, 'showPausedActions', 'tvTyh');
  130. }
  131. }
  132.  
  133. if ('promptDelaySec' in youThereData && retPromptDelay >= 0 && retPromptDelay < 4 * mPU) {
  134. youThereDataHashMapPromptDelay.set(youThereData, retPromptDelay);
  135. const retType = typeof retPromptDelay === 'string' ? 2 : +(typeof retPromptDelay === 'number');
  136. // lact -> promptDelaySec -> showDialog -> playbackPauseDelayMs -> pause
  137. if (retType >= 1) {
  138. defineProp1(youThereData, 'promptDelaySec', retType, 5 * mPU, d => {
  139. delayLog(`${websiteName} is trying to pause video...`, d.toLocaleTimeString());
  140. }, args => {
  141. const [oldValue, newValue, d] = args;
  142. console.log(`${websiteName} is trying to change value 'promptDelaySec' from ${oldValue} to ${newValue} ...`, d.toLocaleTimeString());
  143. }, youThereDataHashMapPromptDelay);
  144.  
  145. }
  146. }
  147.  
  148. if ('lactThresholdMs' in youThereData && retLactThreshold >= 0 && retLactThreshold < 4 * tenPU) {
  149. youThereDataHashMapLactThreshold.set(youThereData, retLactThreshold);
  150. const retType = typeof retLactThreshold === 'string' ? 2 : +(typeof retLactThreshold === 'number');
  151. // lact -> promptDelaySec -> showDialog -> playbackPauseDelayMs -> pause
  152. if (retType >= 1) {
  153. defineProp1(youThereData, 'lactThresholdMs', retType, 5 * tenPU, d => {
  154. // console.log(`${websiteName} is trying to pause video...`, d.toLocaleTimeString());
  155. }, args => {
  156. const [oldValue, newValue, d] = args;
  157. console.log(`${websiteName} is trying to change value 'lactThresholdMs' from ${oldValue} to ${newValue} ...`, d.toLocaleTimeString());
  158. }, youThereDataHashMapLactThreshold);
  159. }
  160. }
  161.  
  162. }
  163.  
  164. // e.performDataUpdate -> f.playerData = a.playerResponse;
  165. // youthereDataChanged_(playerData.messages)
  166. // youthereDataChanged_ -> b.youThereRenderer && fFb(this.youThereManager_, b.youThereRenderer)
  167. // a.youThereData_ = b.configData.youThereData;
  168. // a.youThereData_.playbackPauseDelayMs
  169. function onPageFinished() {
  170. if (arguments.length === 1) noDelayLogUntil = Date.now() + 3400; // no delay log for video changes
  171. Promise.resolve(0).then(() => {
  172. let messages = null;
  173. const pageMgrElm = document.querySelector('#page-manager') || 0;
  174. const pageMgrCnt = insp(pageMgrElm);
  175. try {
  176. messages = pageMgrCnt.data.playerResponse.messages;
  177. } catch (e) { }
  178. if (messages && messages.length > 0) {
  179. for (const message of messages) {
  180. if ((message || 0).youThereRenderer) {
  181. let youThereData = null;
  182. try {
  183. youThereData = message.youThereRenderer.configData.youThereData;
  184. } catch (e) { }
  185. if (youThereData) hookYouThereData(youThereData);
  186. youThereData = null;
  187. break;
  188. }
  189. }
  190. }
  191.  
  192. const ytdFlexyElm = document.querySelector('ytd-watch-flexy') || 0;
  193. const ytdFlexyCnt = insp(ytdFlexyElm);
  194.  
  195. if (ytdFlexyCnt) {
  196. const youThereManager_ = ytdFlexyCnt.youThereManager_ || ytdFlexyElm.youThereManager_ || 0;
  197. const youThereData_ = (youThereManager_ || 0).youThereData_ || 0;
  198. if (youThereData_) hookYouThereData(youThereData_);
  199. const f = ytdFlexyCnt.youthereDataChanged_;
  200. if (typeof f === 'function' && !f.lq2S7) {
  201. ytdFlexyCnt.youthereDataChanged_ = (function (f) {
  202. return function () {
  203. console.log('youthereDataChanged_()');
  204. const ret = f.apply(this, arguments);
  205. onPageFinished();
  206. return ret;
  207. }
  208. })(f);
  209. ytdFlexyCnt.youthereDataChanged_.lq2S7 = 1;
  210. }
  211. }
  212.  
  213. }).catch(console.warn)
  214. }
  215. document.addEventListener('yt-page-data-updated', onPageFinished, false);
  216. document.addEventListener('yt-navigate-finish', onPageFinished, false);
  217. document.addEventListener('spfdone', onPageFinished, false);
  218.  
  219. })(Promise);