Greasy Fork

Disable YouTube Video Ads

Disable YouTube video based ads at home page, and right before or in the middle of the main video playback. For new YouTube layout (Polymer) only.

当前为 2020-07-17 提交的版本,查看 最新版本

// ==UserScript==
// @name        Disable YouTube Video Ads
// @namespace   DisableYouTubeVideoAds
// @version     1.2.10
// @license     AGPLv3
// @author      jcunews
// @website     https://greasyfork.org/en/users/85671-jcunews
// @description Disable YouTube video based ads at home page, and right before or in the middle of the main video playback. For new YouTube layout (Polymer) only.
// @include     https://www.youtube.com/*
// @grant       unsafeWindow
// @run-at      document-start
// ==/UserScript==

((window, fn) => {

  fn = (a, et) => {

    if ((a = document.scripts[document.scripts.length - 1]) && (a.id === "dyvaUjs")) a.remove();

    et = window.InstallTrigger ? "beforescriptexecute" : "message"; //Firefox workaround

    window.XMLHttpRequest.prototype.open_dyva = window.XMLHttpRequest.prototype.open;
    window.XMLHttpRequest.prototype.open = function(mtd, url) {
      if (!(/get_midroll_info|get_video_info/).test(url)) return this.open_dyva.apply(this, arguments);
    };
    
    window.Node.prototype.appendChild_dyva = window.Node.prototype.appendChild;
    window.Node.prototype.appendChild = function(node) {
      var a;
      if ((node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) && Array.from(node.childNodes).some((n, i) => {
        if (n.id === "masthead-ad") {
          n.remove();
          return true;
        }
      })) window.Node.prototype.appendChild = window.Node.prototype.appendChild_dyva;
      return this.appendChild_dyva.apply(this, arguments);
    };
    
    function patchPlayerArgs(args) {
      if (args.ad_device) {
        args.ad_device = "0";
      }
      if (args.ad_flags) {
        args.ad_flags = 0;
      }
      if (args.ad_logging_flag) {
        args.ad_logging_flag = "0";
      }
      if (args.ad_preroll) {
        args.ad_preroll = "0";
      }
      if (args.ad_slots) {
        delete args.ad_slots;
      }
      if (args.ad_tag) {
        delete args.ad_tag;
      }
      if (args.ad3_module) {
        args.ad3_module = "0";
      }
      if (args.adsense_video_doc_id) {
        delete args.adsense_video_doc_id;
      }
      if (args.afv) {
        args.afv = false;
      }
      if (args.afv_ad_tag) {
        delete args.afv_ad_tag;
      }
      if (args.allow_html5_ads) {
        args.allow_html5_ads = 0;
      }
      if (args.csi_page_type) {
        args.csi_page_type = args.csi_page_type.replace(/watch7ad/, "watch7");
      }
      if (args.enable_csi) {
        args.enable_csi = "0";
      }
      if (args.pyv_ad_channel) {
        delete args.pyv_ad_channel;
      }
      if (args.show_pyv_in_related) {
        args.show_pyv_in_related = false;
      }
      if (args.vmap) {
        delete args.vmap;
      }
    }
    
    function do2() {
      if (window.ytplayer && window.ytplayer.config && window.ytplayer.config.args && !window.ytplayer.config.args.dvya) {
        window.ytplayer.config.args.dvya = 1;
        patchPlayerArgs(window.ytplayer.config.args);
        removeEventListener(et, do2);
      } else if (et === "message") postMessage({});
    }
    
    function do1() {
      if (window.yt && window.yt.player && window.yt.player.Application && window.yt.player.Application.create && !window.yt.player.Application.dyva) {
        window.yt.player.Application.dyva = 1;
        var ytPlayerApplicationCreate = window.yt.player.Application.create;
        window.yt.player.Application.create = function(id, ytPlayerConfig){
          if ((id === "player-api") && ytPlayerConfig && ytPlayerConfig.args && ytPlayerConfig.args.vmap) {
            delete ytPlayerConfig.args.vmap;
          }
          return ytPlayerApplicationCreate.apply(this, arguments);
        };
        var ytSetConfig = window.yt.setConfig;
        window.yt.setConfig = function(ytConfig){
          if (ytConfig) {
            if (ytConfig.ADS_DATA) {
              delete ytConfig.ADS_DATA;
            }
          }
          return ytSetConfig.apply(this, arguments);
        };
        var ytcfgSet = window.ytcfg.set;
        window.ytcfg.set = function(ytConfig){
          if (ytConfig) {
            if (ytConfig.SKIP_RELATED_ADS === false) {
              ytConfig.SKIP_RELATED_ADS = true;
            }
            if (ytConfig.TIMING_ACTION) {
              ytConfig.TIMING_ACTION = ytConfig.TIMING_ACTION.replace(/watch7ad/, "watch7");
            }
            if (ytConfig.TIMING_INFO) {
              if (ytConfig.TIMING_INFO.yt_ad) {
                ytConfig.TIMING_INFO.yt_ad = 0;
              }
              if (ytConfig.TIMING_INFO.yt_ad_an) {
                delete ytConfig.TIMING_INFO.yt_ad_an;
              }
              if (ytConfig.TIMING_INFO.yt_ad_pr) {
                ytConfig.TIMING_INFO.yt_ad_pr = 0;
              }
            }
          }
          return ytcfgSet.apply(this, arguments);
        };
        removeEventListener(et, do1);
        addEventListener(et, do2);
        do2();
      } else if (et === "message") postMessage({});
    }
    addEventListener(et, do1);
    if (et === "message") postMessage({});
    
    addEventListener("spfpartprocess", function(ev) { //old youtube
      if (ev.detail && ev.detail.part && ev.detail.part.data &&
          ev.detail.part.data.swfcfg && ev.detail.part.data.swfcfg.args) {
        patchPlayerArgs(ev.detail.part.data.swfcfg.args);
      }
    }, true);

  };
  if (this.GM_info && (this.GM_info.scriptHandler === "FireMonkey")) {
    //workaround for FireMonkey's partial compatibility to GreaseMonkey specification.
    let e = document.createElement("SCRIPT");
    e.id = "dyvaUjs";
    e.text = "(" + fn + ")()";
    document.documentElement.appendChild(e);
  } else fn();

})(unsafeWindow);