Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Spotify Ad Muter
- // @version 1.2
- // @namespace http://tampermonkey.net/
- // @description Detects and blocks ads on Spotify. Automatically mute Spotify ads. Turn sound on again after the ad.
- // @match https://*.spotify.com/*
- // @grant none
- // @run-at document-start
- // @downloadURL https://gist.github.com/marcbelmont/1ea63270867a4e8786dd5f172d8d4489/raw
- // @updateURL https://gist.github.com/marcbelmont/1ea63270867a4e8786dd5f172d8d4489/raw
- // ==/UserScript==
- !async function () {
- async function queryAsync(query) {
- return new Promise(resolve => {
- const interval = setInterval(() => {
- const element = document.querySelector(query);
- if (element) {
- clearInterval(interval);
- return resolve(element);
- }
- }, 250);
- });
- }
- const nowPlayingBar = await queryAsync('[data-testid=now-playing-widget]');
- const volumeButton = await queryAsync('button.volume-bar__icon-button');
- const adQuerySelector = '[data-testid=now-playing-widget] *[aria-label~=Advertisement]';
- let playInterval;
- new MutationObserver(() => {
- if (document.querySelector(adQuerySelector) &&
- volumeButton.attributes['aria-label'].value.toLowerCase().indexOf('unmute') == -1) {
- volumeButton.click();
- if (!playInterval) {
- playInterval = setInterval(() => {
- if (!document.querySelector(adQuerySelector)) {
- clearInterval(playInterval);
- playInterval = null;
- volumeButton.click();
- }
- }, 500);
- }
- }
- }).observe(nowPlayingBar, {
- characterData: true,
- childList: true,
- attributes: true,
- subtree: true
- });
- }();
Advertisement
Add Comment
Please, Sign In to add comment