Youtube - dismiss sign-in

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

As of 2020-11-22. See the latest version.

// ==UserScript==
// @name         Youtube - dismiss sign-in
// @name:fr      Youtube - cacher "connectez-vous"
// @namespace    https://github.com/Procyon-b
// @version      0.5
// @description  Hide the "sign-in" and cookies dialogs. Prevent the dialogs from pausing the video.
// @description:fr  Cache le dialogue "connectez-vous" et le dialogue des cookies. Empêche ces popups de stopper la vidéo.
// @author       Achernar
// @match        https://*.youtube.com/*
// @exclude      https://www.youtube.com/embed/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function(){
"use strict";
function consent() {
  var e=document.querySelector('#introAgreeButton');
  e && e.click();
  }
if (location.href.startsWith('https://consent.youtube.com/')) {
  if (document.readyState != 'loading') consent();
  else document.addEventListener('DOMContentLoaded', consent);
  return;
  }

function cookies() {
  var r={}, a=document.cookie;
  a.split(';').forEach(function(e){
    var p=e.split('=');
    if (p[0]) r[p.shift().trim()]=p.join('=');
    });
  return r;
  }

var ck=cookies();
if (ck['APISID']) return;

function SImutF(mutL){
  for (let mut of mutL) {
    var t=mut.target;
    if ( (t.id=='dismiss-button') || (t.parentNode && (t.parentNode.id=='dismiss-button')) ) {
      if (t.classList.contains('yt-upsell-dialog-renderer') || t.classList.contains('ytd-mealbar-promo-renderer')) ;
      else if (t.classList.contains('yt-tooltip-renderer')) {
        t=t.querySelector('yt-button-renderer');
        if (!t) continue;
        }
      else continue;
      setTimeout(function(){t.click()}, 300);
      subObs.observe(t,{attributes: true, subtree: true});
      return;
      }
    }
  }
var obs=new MutationObserver(SImutF);
var subObs=new MutationObserver(SImutF);
var obs2=new MutationObserver(function(mutL){
  for (let mut of mutL) {
    for (let n of mut.addedNodes) {
      if (n.nodeName == 'YTD-POPUP-CONTAINER') {
        this.disconnect();
        setObs();
        return;
        }
      }
    }
  });
var obs3=new MutationObserver(function(mutL){
  for (let mut of mutL) {
    for (let n of mut.addedNodes) {
      if (n.nodeName == 'YTD-CONSENT-BUMP-LIGHTBOX') {
        this.disconnect();
        setTimeout(function(){
          let ck=cookies();
          if (ck['CONSENT'] && !ck['CONSENT'].startsWith('YES')) document.cookie='CONSENT=YES+;path=/;secure;domain=youtube.com;expires='+(new Date(Date.now()+567648000000)).toUTCString()+';';
          }, 5000);
        return;
        }
      }
    }
  });

function init() {
  var t;
  if (document.querySelector('ytm-app')) {
    new MutationObserver(function(mutL){
      for (let mut of mutL) {
        for (let n of mut.addedNodes) {
          if (n.classList.contains('upsell-dialog-lightbox') || n.classList.contains('consent-bump-lightbox') ) {
            if (t=document.querySelector('.upsell-dialog-dismiss-button button, .consent-bump-button-wrapper button')) {
              t.click();
              }
            }
          }
        }
      }).observe(document.body, {childList: true, subtree: false});
    }

  setObs();
  if (ck['CONSENT'] && !ck['CONSENT'].startsWith('YES')) {
    obs3.observe(document.body, {childList:true});
    setTimeout(function(){obs3.disconnect();},30000);
    }
  }

var c=1;
function setObs(){
  var r=document.querySelector('ytd-app ytd-popup-container');
  if (!r) {
    if (c--) obs2.observe(document.querySelector('ytd-app'), {childList:true});
    return;
    }
  obs.observe(r, {childList: true, subtree: true});
  }
if (document.readyState != 'loading') init();
else document.addEventListener('DOMContentLoaded', init);

var s=document.createElement('style');
(document.head || document.documentElement).appendChild(s);
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 ~ iron-overlay-backdrop{display:none;}";

s=document.createElement('script');
s.textContent= `(function(){var c=80, pl, plR, oldp={};
  function f(){
    plR=document.querySelector('ytd-player#ytd-player');
    if (plR) pl=plR.getPlayer();
    if (!pl) {
      if (--c) setTimeout(f,200);
      else if (plR) {
        var obs=new MutationObserver(function(mutL){
          for (let mut of mutL) {
            for (let n of mut.addedNodes) {
              if (n.id == 'movie_player') {
                this.disconnect();
                pl=plR.getPlayer();
                init();
                return;
                }
              }
            }
          }).observe(document.querySelector('ytd-app'), {childList: true, subtree: true});
        }
      return;
      }
    else init();
    }
  if (document.readyState != 'loading') f();
  else document.addEventListener('DOMContentLoaded', f);

  function init() {
    for (let i in pl) if (typeof pl[i] == 'function') {
      if ( !['cancelPlayback', 'pauseVideo', 'stopVideo', 'playVideo'].includes(i) ) continue;
      oldp[i]=pl[i];
      pl[i]=function() {
        let st=(new Error()).stack;
        if ( (st.search(/(\\.onFulfilled|scheduler\\.js:|handlePopupClose_)/)>0) && (st.search(/onYtStopOldPlayer_/) ==-1) ) return;
        oldp[i].apply(this,arguments);
        }
      }
    }
  })();`;

(document.head || document.documentElement).appendChild(s);
if (s.parentNode) s.parentNode.removeChild(s);
})();