Sandbird

Untitled

May 14th, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Spotify Ad Muter
  3. // @version 1.2
  4. // @namespace http://tampermonkey.net/
  5. // @description Detects and blocks ads on Spotify. Automatically mute Spotify ads. Turn sound on again after the ad.
  6. // @match https://*.spotify.com/*
  7. // @grant none
  8. // @run-at document-start
  9. // @downloadURL https://gist.github.com/marcbelmont/1ea63270867a4e8786dd5f172d8d4489/raw
  10. // @updateURL https://gist.github.com/marcbelmont/1ea63270867a4e8786dd5f172d8d4489/raw
  11. // ==/UserScript==
  12.  
  13. !async function () {
  14.  
  15. async function queryAsync(query) {
  16. return new Promise(resolve => {
  17. const interval = setInterval(() => {
  18. const element = document.querySelector(query);
  19. if (element) {
  20. clearInterval(interval);
  21. return resolve(element);
  22. }
  23. }, 250);
  24. });
  25. }
  26. const nowPlayingBar = await queryAsync('[data-testid=now-playing-widget]');
  27. const volumeButton = await queryAsync('button.volume-bar__icon-button');
  28. const adQuerySelector = '[data-testid=now-playing-widget] *[aria-label~=Advertisement]';
  29.  
  30. let playInterval;
  31. new MutationObserver(() => {
  32. if (document.querySelector(adQuerySelector) &&
  33. volumeButton.attributes['aria-label'].value.toLowerCase().indexOf('unmute') == -1) {
  34. volumeButton.click();
  35. if (!playInterval) {
  36. playInterval = setInterval(() => {
  37. if (!document.querySelector(adQuerySelector)) {
  38. clearInterval(playInterval);
  39. playInterval = null;
  40. volumeButton.click();
  41. }
  42. }, 500);
  43. }
  44. }
  45. }).observe(nowPlayingBar, {
  46. characterData: true,
  47. childList: true,
  48. attributes: true,
  49. subtree: true
  50. });
  51. }();
Advertisement
Add Comment
Please, Sign In to add comment