Spotify AdBlocker

Comprehensive Spotify AdBlocker (audio, banners, premium prompts).

  1. // ==UserScript==
  2. // @name Spotify AdBlocker
  3. // @namespace http://tampermonkey.net/
  4. // @version 3.0
  5. // @description Comprehensive Spotify AdBlocker (audio, banners, premium prompts).
  6. // @author Plancy
  7. // @match https://open.spotify.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (async function () {
  13. 'use strict';
  14.  
  15. const queryAsync = (query, interval = 250) => new Promise(resolve => {
  16. const checkInterval = setInterval(() => {
  17. const element = document.querySelector(query);
  18. if (element) {
  19. clearInterval(checkInterval);
  20. resolve(element);
  21. }
  22. }, interval);
  23. });
  24.  
  25. const removeElements = selector => {
  26. document.querySelectorAll(selector).forEach(el => el.remove());
  27. };
  28.  
  29. const handleAudioAds = () => {
  30. const audioAd = document.querySelector('audio[src*="spotify.com/ad"]');
  31. if (audioAd) {
  32. audioAd.src = "";
  33. audioAd.pause();
  34. }
  35. };
  36.  
  37. const inject = ({ ctx, fn, transform }) => {
  38. const original = ctx[fn];
  39. ctx[fn] = function () {
  40. const result = original.apply(this, arguments);
  41. return transform ? transform.call(this, result, ...arguments) : result;
  42. };
  43. };
  44.  
  45. const observer = new MutationObserver(() => {
  46. removeElements('[data-testid="ad-slot-container"], [class*="ad-"]');
  47. handleAudioAds();
  48. removeElements('.ButtonInner-sc-14ud5tc-0.fcsOIN');
  49. });
  50.  
  51. observer.observe(document.body, {
  52. childList: true,
  53. subtree: true,
  54. });
  55.  
  56. const adRemovalInterval = setInterval(() => {
  57. removeElements('[data-testid="ad-slot-container"], [class*="ad-"]');
  58. handleAudioAds();
  59. removeElements('.ButtonInner-sc-14ud5tc-0.fcsOIN');
  60. }, 1000);
  61.  
  62. const nowPlayingBar = await queryAsync(".now-playing-bar");
  63. const playButton = await queryAsync("button[title=Play], button[title=Pause]");
  64. let audio;
  65.  
  66. inject({
  67. ctx: document,
  68. fn: "createElement",
  69. transform(result, type) {
  70. if (type === "audio") {
  71. audio = result;
  72. }
  73. return result;
  74. },
  75. });
  76.  
  77. new MutationObserver(() => {
  78. if (audio && playButton && document.querySelector(".now-playing > a")) {
  79. audio.src = "";
  80. playButton.click();
  81. }
  82. }).observe(nowPlayingBar, {
  83. childList: true,
  84. subtree: true,
  85. });
  86.  
  87. window.addEventListener('beforeunload', () => {
  88. observer.disconnect();
  89. clearInterval(adRemovalInterval);
  90. });
  91.  
  92. console.log("Spotify AdBlocker Ultimate is active");
  93. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址