// ==UserScript== // @name Disable YouTube Video Ads // @namespace DisableYouTubeVideoAds // @version 1.2.13 // @license AGPLv3 // @author jcunews // @website https://greasyfork.org/en/users/85671-jcunews // @description Disable YouTube video & screen 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 // @downloadURL none // ==/UserScript== ((window, fn, ipse, haia, hca) => { 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/).test(url)) { this.url_dyva = url; return this.open_dyva.apply(this, arguments); } }; window.XMLHttpRequest.prototype.addEventListener_dyva = window.XMLHttpRequest.prototype.addEventListener; window.XMLHttpRequest.prototype.addEventListener = function(typ, fn) { if (typ === "readystatechange") { var a, f = fn, m, z; fn = function() { if ((this.readyState === 4) && (/\/watch\?|get_video_info/).test(this.url_dyva)) { try { a = JSON.parse(this.responseText); a.forEach((p, a) => { if (p.player && p.player.args && p.player.args.player_response) { a = JSON.parse(p.player_response); patchPlayerResponse(a); p.player_response = JSON.stringify(a); } else if (p = p.playerResponse) { patchPlayerResponse(p); } }); Object.defineProperty(this, "responseText", {writable: false, value: JSON.stringify(a)}); } catch(z) {} if (!a && (/get_video_info/).test(this.url_dyva)) try { m = this.responseText.match(/^(.*player_response=)([^&]+)(.*)/); a = JSON.parse(decodeURIComponent(m[2])); patchPlayerResponse(a); a = m[1] + encodeURIComponent(JSON.stringify(a)) + m[3]; Object.defineProperty(this, "responseText", {writable: false, value: a}); } catch(z) {} } return f.apply(this, arguments); }; } return this.addEventListener_dyva.apply(this, arguments); }; window.Node.prototype.appendChild_dyva = window.Node.prototype.appendChild; window.Node.prototype.appendChild = function(node) { var a; if (!ipse && (a = document.querySelector('ytd-watch-flexy')) && (a = a.constructor.prototype) && a.isPlaShelfEnabled_) { a.isPlaShelfEnabled_ = () => false; ipse = true; } if ((!hca || !haia) && (a = document.querySelector('ytd-watch-next-secondary-results-renderer')) && (a = a.constructor.prototype)) { if (a.hasAllowedInstreamAd_ && !haia) { a.hasAllowedInstreamAd_ = () => false; haia = true; } if (a.hasCompanionAds_ && !hca) { a.hasCompanionAds_ = () => false; hca = true; } } 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 patchPlayerResponse(playerResponse) { if (playerResponse.adPlacements) playerResponse.adPlacements = []; if (playerResponse.playerAds) playerResponse.playerAds = []; } function patchPlayerArgs(args, a) { 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; if (args.player_response) { a = JSON.parse(args.player_response); patchPlayerResponse(a); args.player_response = JSON.stringify(a); } } function patchSpf() { if (window.spf && !spf.request_dyva) { spf.request_dyva = spf.request; spf.request = function(a, b) { if (b && b.onDone) { var onDone_ = b.onDone; b.onDone = function(response) { var a = response; if (a && (/\/watch\?/).test(a.url) && (a = a.response) && (a = a.parts)) { a.forEach((p, a) => { if (p.player && p.player.args && p.player.args.player_response) { a = JSON.parse((p = p.player.args).player_response); patchPlayerResponse(a); p.player_response = JSON.stringify(a); } else if (p.playerResponse) { patchPlayerResponse(p.playerResponse); } }); } return onDone_.apply(this, arguments); }; } return this.request_dyva.apply(this, arguments); }; return; } } 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 ytPlayerApplicationCreateAlternate = window.yt.player.Application.createAlternate; window.yt.player.Application.createAlternate = function(id, ytPlayerConfig) { if ((id === "player-api") && ytPlayerConfig && ytPlayerConfig.args && ytPlayerConfig.args.vmap) { delete ytPlayerConfig.args.vmap; } return ytPlayerApplicationCreateAlternate.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 (window.ytInitialPlayerResponse) patchPlayerResponse(window.ytInitialPlayerResponse); patchSpf(); 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); addEventListener("load", a => { if (!(a = window.ayvp_cssOverride)) { a = document.createElement("STYLE"); a.id = "ayvp_cssOverride"; a.innerHTML = `\/* .video-ads{display:none!important} .ytp-ad-overlay-open .caption-window.ytp-caption-window-bottom{margin-bottom:4em} .ytp-autohide .caption-window.ytp-caption-window-bottom, .ytp-hide-controls .caption-window.ytp-caption-window-bottom{margin-bottom:0!important}`; document.documentElement.appendChild(a); } }); }; 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);