Youtube - dismiss sign-in

Hide the "sign-in" and cookies dialogs. Prevent the dialogs from pausing the video.

  1. // ==UserScript==
  2. // @name Youtube - dismiss sign-in
  3. // @name:fr Youtube - cacher "connectez-vous"
  4. // @namespace https://github.com/Procyon-b
  5. // @version 0.8.4
  6. // @description Hide the "sign-in" and cookies dialogs. Prevent the dialogs from pausing the video.
  7. // @description:fr Cache le dialogue "connectez-vous" et le dialogue des cookies. Empêche ces popups de stopper la vidéo.
  8. // @author Achernar
  9. // @match https://*.youtube.com/*
  10. // @exclude https://www.youtube.com/embed/*
  11. // @run-at document-start
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function(){
  16. "use strict";
  17. function consent(ev,r=document) {
  18. var t, e=r.querySelector('#introAgreeButton')
  19. || ( (t=r.querySelectorAll('#lightbox[class*="ytd-consent-bump"] a.ytd-button-renderer:not([href])')) && (t.length==1) && (e=t[0]) )
  20. || ( (t=r.querySelectorAll('form button')) && (t.length == 1) && (e=t[0]) )
  21. || ( (t=r.querySelectorAll(':scope form button')) && (t.length == 4) && (e=t[1]) ) /*20220524*/
  22. || ( (t=r.querySelectorAll('ytd-button-renderer > a:not([href]) tp-yt-paper-button[class*="primary"]')) && (t.length == 1) && (e=t[0]) )
  23. || ( (t=r.querySelectorAll('ytd-button-renderer > a:not([href]) tp-yt-paper-button[class*="primary"]')) && (t.length == 2) && (e=t[0]) );
  24. e && e.click();
  25. }
  26. if (location.href.startsWith('https://consent.youtube.com/')) {
  27. if (document.readyState != 'loading') consent();
  28. else document.addEventListener('DOMContentLoaded', consent);
  29. return;
  30. }
  31.  
  32. if (window !== window.top) return;
  33.  
  34. function cookies() {
  35. var r={}, a=document.cookie;
  36. a.split(';').forEach(function(e){
  37. var p=e.split('=');
  38. if (p[0]) r[p.shift().trim()]=p.join('=');
  39. });
  40. return r;
  41. }
  42.  
  43. var ck=cookies();
  44. if (ck['APISID']) return;
  45.  
  46. function hasDismiss(e, l=1) {
  47. var p=e;
  48. while (p && l-- && (p=p.parentNode)) {
  49. if (p.id=='dismiss-button') return p;
  50. }
  51. }
  52.  
  53. function SImutF(mutL){
  54. for (let mut of mutL) {
  55. let t=mut.target, db=t;
  56. if ( (t.id=='dismiss-button') || (db=hasDismiss(t,2)) ) {
  57. if (db.__c__) continue;
  58. if (t.classList.contains('yt-upsell-dialog-renderer') || t.classList.contains('ytd-mealbar-promo-renderer')) ;
  59. else if (t.classList.contains('yt-tooltip-renderer')) {
  60. t=t.querySelector('yt-button-renderer');
  61. if (!t) continue;
  62. }
  63. else continue;
  64. db.__c__=true;
  65. setTimeout(function(){
  66. t.click();
  67. delete db.__c__;
  68. }, 300);
  69. subObs.observe(t,{attributes: true, subtree: true});
  70. }
  71. }
  72. }
  73. var obs=new MutationObserver(SImutF);
  74. var subObs=new MutationObserver(SImutF);
  75. var obs_w4PU=new MutationObserver(function(mutL){
  76. for (let mut of mutL) {
  77. for (let n of mut.addedNodes) {
  78. if (n.nodeName == 'YTD-POPUP-CONTAINER') {
  79. this.disconnect();
  80. setObs();
  81. return;
  82. }
  83. }
  84. }
  85. });
  86. var obs_w4ErRd=new MutationObserver(function(mutL){
  87. for (let mut of mutL) {
  88. for (let n of mut.addedNodes) {
  89. if (n.id == 'columns') {
  90. let r=n.querySelector('yt-playability-error-supported-renderers');
  91. if (r) {
  92. this.disconnect();
  93. setErRdObs();
  94. return;
  95. }
  96. }
  97. }
  98. }
  99. });
  100. var obsCk=new MutationObserver(function(mutL){
  101. var cb;
  102. for (let mut of mutL) {
  103. for (let n of mut.addedNodes) {
  104. if (n.nodeName == 'TP-YT-IRON-OVERLAY-BACKDROP') {
  105. cb=document.querySelector('#lightbox');
  106. consent();
  107. }
  108. if ( (n.nodeName == 'YTD-CONSENT-BUMP-LIGHTBOX') || (n=cb) ) {
  109. this.disconnect();
  110. setTimeout(function(){
  111. let ck=cookies();
  112. if (ck['CONSENT'] && !ck['CONSENT'].startsWith('YES')) document.cookie='CONSENT=YES+;path=/;secure;domain=youtube.com;expires='+(new Date(Date.now()+567648000000)).toUTCString()+';';
  113. }, 5000);
  114. // not an iframe anymore
  115. consent(null,n);
  116. return;
  117. }
  118. }
  119. }
  120. });
  121.  
  122. var ErRd, ErRdIT,
  123. obsErRd=new MutationObserver(function(mutL){
  124. var t, ITc=30;
  125. for (let mut of mutL) {
  126. t=mut.target;
  127. if (t.id=='dismiss-button') {
  128. if (t.classList.contains('yt-player-error-message-renderer')) t=t.querySelector(':scope yt-button-renderer paper-button#button');
  129. else continue;
  130. if (ErRdIT) clearInterval(ErRdIT);
  131. ErRdIT=setInterval(function(){
  132. if (!ITc-- || ErRd.hidden || !t) {
  133. clearInterval(ErRdIT);
  134. ErRdIT=0;
  135. }
  136. else t.click();
  137. }, 300);
  138. return;
  139. }
  140. }
  141. });
  142.  
  143. function init() {
  144. var t;
  145. if (document.querySelector('ytm-app')) {
  146. new MutationObserver(function(mutL){
  147. for (let mut of mutL) {
  148. for (let n of mut.addedNodes) {
  149. if (n.classList.contains('upsell-dialog-lightbox') || n.classList.contains('consent-bump-lightbox') ) {
  150. if (t=document.querySelector('.upsell-dialog-dismiss-button button, .consent-bump-button-wrapper button')) {
  151. t.click();
  152. }
  153. }
  154. }
  155. }
  156. }).observe(document.body, {childList: true, subtree: false});
  157. }
  158.  
  159. setObs();
  160. setErRdObs();
  161. if (ck['CONSENT'] && !ck['CONSENT'].startsWith('YES')) {
  162. obsCk.observe(document.body, {childList:true});
  163. setTimeout(function(){obsCk.disconnect();},30000);
  164. }
  165. }
  166.  
  167. var c=1;
  168. function setObs(){
  169. var r=document.querySelector('ytd-app ytd-popup-container');
  170. if (!r) {
  171. if (c--) obs_w4PU.observe(document.querySelector('ytd-app'), {childList:true});
  172. return;
  173. }
  174. obs.observe(r, {childList: true, subtree: true});
  175. }
  176.  
  177. function setErRdObs() {
  178. ErRd=document.querySelector('ytd-app yt-playability-error-supported-renderers');
  179. if (!ErRd) {
  180. obs_w4ErRd.observe(document.querySelector('ytd-app'), {childList: true, subtree: true});
  181. setTimeout(function(){obs_w4ErRd.disconnect();}, 20000);
  182. }
  183. else obsErRd.observe(ErRd ,{childList: true, subtree: true, attributes: true});
  184. }
  185.  
  186. if (document.readyState != 'loading') init();
  187. else document.addEventListener('DOMContentLoaded', init);
  188.  
  189. function inject() {
  190.  
  191. var r=document.head || document.documentElement;
  192. if (!r) {
  193. setTimeout(inject,0);
  194. return;
  195. }
  196.  
  197. var s=document.createElement('style');
  198. r.appendChild(s);
  199. s.textContent="#consent-bump,iron-overlay-backdrop,yt-upsell-dialog-renderer{opacity:0;}yt-upsell-dialog-renderer *,yt-bubble-hint-renderer,.upsell-dialog-lightbox,.consent-bump-lightbox{display:none !important;}ytd-app > ytd-consent-bump-lightbox,ytd-app > ytd-consent-bump-v2-lightbox,ytd-app ~ iron-overlay-backdrop,ytd-app ~ tp-yt-iron-overlay-backdrop{display:none;}";
  200.  
  201. s=document.createElement('script');
  202. s.textContent= `(function(){var c=160, pl, plR, prom, oldp={}, t, done, hasPaused;
  203.  
  204. const obs=new MutationObserver(function(mutL){
  205. if (!prom) {
  206. if (prom=document.querySelector('ytd-player#ytd-player') ) {
  207. if (prom.getPlayerPromise) {
  208. prom.getPlayerPromise().then(function(a){
  209. pl=a;
  210. patch();
  211. });
  212. }
  213. else prom=null;
  214. }
  215. }
  216.  
  217. for (let mut of mutL) {
  218. for (let n of mut.addedNodes) {
  219. if (n.id == 'movie_player') {
  220. plR=n.closest('ytd-player#ytd-player');
  221. if (!plR) continue;
  222. this.disconnect();
  223. setTimeout(function(){
  224. pl=plR.getPlayer();
  225. patch();
  226. }, 0);
  227. return;
  228. }
  229. }
  230. }
  231. });
  232.  
  233. function init(){
  234. obs.observe(document.querySelector('ytd-app') || document.body, {childList: true, subtree: true});
  235. setTimeout(function(){obs.disconnect();},10000);
  236. f();
  237. }
  238.  
  239. function f(){
  240. plR=document.querySelector('ytd-player#ytd-player');
  241. if (plR) pl=plR.getPlayer();
  242.  
  243. if (!pl) {
  244. if (--c) setTimeout(f,100);
  245. return;
  246. }
  247. else patch();
  248. }
  249. if (document.readyState != 'loading') init();
  250. else document.addEventListener('DOMContentLoaded', init);
  251.  
  252. function patch() {
  253. if (done) return;
  254. if (pl) done=1;
  255. obs.disconnect();
  256. for (let i in pl) if (typeof pl[i] == 'function') {
  257. if ( !['cancelPlayback', 'pauseVideo', 'stopVideo', 'playVideo'].includes(i) ) continue;
  258. oldp[i]=pl[i];
  259. pl[i]=function() {
  260. let pass= (!hasPaused && i=='playVideo') ? true:false;
  261. let st=(new Error()).stack;
  262. if ( !pass && (st.search(/(\\.onFulfilled|_onFulfilled|scheduler\\.js:|handlePopupClose_)/)>0) && (st.search(/onYtStopOldPlayer/) ==-1) ) {
  263. if (i=='pauseVideo') hasPaused=1;
  264. return;
  265. }
  266. oldp[i].apply(this,arguments);
  267. }
  268. }
  269. }
  270. })();`;
  271.  
  272. r.appendChild(s);
  273. if (s.parentNode) s.parentNode.removeChild(s);
  274.  
  275. } // inject()
  276.  
  277. inject();
  278.  
  279. })();