Pluto.tv auto mute'n'hide ads

mutes and hides embedded/"hardcoded"/non skippable ad breaks on Pluto tv live channels

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name Pluto.tv auto mute'n'hide ads
// @namespace Violentmonkey Scripts
// @version 2.5
// @locale en
// @author       mihau
// @supportURL   https://greasyfork.org/en/scripts/535805-pluto-tv-auto-mute-n-hide-ads
// @license MIT
// @description mutes and hides embedded/"hardcoded"/non skippable ad breaks on Pluto tv live channels
// @match https://pluto.tv/gsa/live-tv*
// @grant none
// ==/UserScript==

// if you don't want to *hide* the ads while playing, change this from 1 to 0 :
let hidevid = 1;
// if you don't want to the audio to be unmuted and video to maximized (to full vieport width), change this from 1 to 0 :
let maximized = 1;

// best don't edit below this line

addEventListener("DOMContentLoaded", (event) => {
  
// via https://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists
function waitforit(selector) {
    return new Promise(resolve => {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector));
        }

        const observer = new MutationObserver(mutations => {
            if (document.querySelector(selector)) {
                observer.disconnect();
                resolve(document.querySelector(selector));
            }
        });

        // If you get "parameter 1 is not of type 'Node'" error, see https://stackoverflow.com/a/77855838/492336
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    });
}
  
waitforit('.avia-container').then((elm) => {
    go();
});

function go() {
    
    const t = document.querySelector('meta[property="og:title"]').content.replace("Watch ","").replace(": Live TV Channel for Free | Pluto TV","")

    const noads = document.createElement("div");

    noads.style.display = "none";
    noads.style.width = "100%";
    noads.style.height = "100%";
    noads.style.top = "0";
    noads.style.left = "0";
    noads.style.right = "0";
    noads.style.bottom = "0";
    noads.style.fontSize = "xx-large"; 
    noads.style.position = "fixed";
    noads.id = "noads";

    noads.innerHTML  = '<div style="position: relative;  text-align: center;  color: white;"><img src="' + document.querySelector('meta[property="og:image"]').content + '" style="width:100%;height: 100%"/>';
    noads.innerHTML += '<div style="position: absolute; top: 15%; left: 15%; color: white; font: bold 150px arial,sans-serif;">AD<br />BREAK</div></div>';
    
    document.body.appendChild(noads);

    const observer = new MutationObserver(function(mutations) {

      mutations.forEach(function(mutation) {
        let v = document.getElementsByTagName("video")[0];
        
        let x = document.getElementById("root");
        let y = document.getElementById("modal-root");

        if (document.querySelectorAll("[class^='adPodProgressIndicator']")[0].innerHTML != "") {
          if (hidevid == 1) {
            x.style.visibility="hidden";
            y.style.visibility="hidden";
            noads.style.display="block";
          }
          v.muted = true;
          document.title = "AD BREAK: " + t;
        } else {
          if (hidevid == 1) {
            x.style.visibility="visible";
            y.style.visibility="visible";
            noads.style.display="none";
          }
          v.muted = false;
          document.title = t;
        }
      });

    });
    
    observer.observe(document.querySelector(".upper-right-stacked"), { childList: true, subtree: true });

    setTimeout(function() {

        if (maximized == 1) {

            document.getElementsByClassName("fullbrowser-btn-atc")[0].click();
            document.getElementsByClassName("unmute-btn-atc")[0].click();

        }
      
        document.title = t;

    }, 1250);

  }

})