Advertisement
Hdyzen

Twitch Adblock by ChatGPT

Feb 14th, 2023 (edited)
3,532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Twitch Ad Blocker
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.0
  5. // @description  Block Twitch ads
  6. // @author       Your Name
  7. // @match        https://www.twitch.tv/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.  
  14.     // Remove Twitch ads
  15.     function removeTwitchAds() {
  16.         // Remove the main ad container
  17.         const adContainer = document.querySelector('.video-player__ad-container');
  18.         if (adContainer) {
  19.             adContainer.remove();
  20.         }
  21.  
  22.         // Remove individual ads (pre-roll, mid-roll, etc.)
  23.         const ads = document.querySelectorAll('.player-ad-overlay');
  24.         for (const ad of ads) {
  25.             ad.remove();
  26.         }
  27.     }
  28.  
  29.     // Watch for changes to the DOM and remove ads in real time
  30.     const observer = new MutationObserver(mutations => {
  31.         for (const mutation of mutations) {
  32.             const addedNodes = mutation.addedNodes;
  33.             for (const node of addedNodes) {
  34.                 if (node.nodeName === 'DIV' && node.classList.contains('video-player__ad-container')) {
  35.                     requestAnimationFrame(removeTwitchAds);
  36.                 }
  37.             }
  38.         }
  39.     });
  40.  
  41.     // Start observing the body for changes
  42.     const body = document.querySelector('body');
  43.     observer.observe(body, { childList: true, subtree: true });
  44.  
  45.     // Wait for the player to be ready before removing ads
  46.     function waitForPlayer() {
  47.         const player = document.querySelector('.video-player__container');
  48.         if (player) {
  49.             player.addEventListener('play', removeTwitchAds);
  50.         } else {
  51.             setTimeout(waitForPlayer, 500);
  52.         }
  53.     }
  54.  
  55.     waitForPlayer();
  56. })();
  57. s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement