Skip to content

Instantly share code, notes, and snippets.

@marcbelmont
Last active April 22, 2024 10:28
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcbelmont/1ea63270867a4e8786dd5f172d8d4489 to your computer and use it in GitHub Desktop.
Save marcbelmont/1ea63270867a4e8786dd5f172d8d4489 to your computer and use it in GitHub Desktop.
Spotify Ad Muter. Automatically mute (block) Spotify ads. Turn sound on again after the ad.
// ==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
});
}();
@marcbelmont
Copy link
Author

marcbelmont commented May 5, 2020

📦 Installation

  • Install Tampermonkey or similar
  • In Tampermonkey, click "Create a new script".
  • Copy and paste the content of the script above.

Based on this script that no longer works.

@champolot
Copy link

Any help how to import the file? I've tried to drop it in the opened spotify web app. It say Your file was not foundIt may have been moved or deleted.
ERR_FILE_NOT_FOUND

@marcbelmont
Copy link
Author

@champolot see my previous comment for how to install.

@obasilakis
Copy link

Works like a charm, thanks man

@RoguedBear
Copy link

RoguedBear commented Dec 30, 2020

Running the script, the right-click menu on Spotify was disabled/not showing up.
I commented out lines 54-62 since I don't mind upgrade to premium banners (that is if it even shows up 😅 ). The right-click menu now shows up. I think its due to the CSS change made in those lines that right-click menu was not showing up

@marcbelmont
Copy link
Author

@RoguedBear thanks for your message. I've updated the gist.

@Maarsz
Copy link

Maarsz commented Mar 27, 2021

Needs an update again unfortunately

@marcbelmont
Copy link
Author

@Maarsz I've fixed it.

@Raz-js
Copy link

Raz-js commented Mar 18, 2022

Rather just max the "playbackRate" while the ad plays

Docs: https://www.w3schools.com/tags/av_prop_playbackrate.asp

@deeelwy
Copy link

deeelwy commented Dec 9, 2022

It seems to be broken again. It also seems to repeat the same ads over and over again. I upgraded Tampermonkey and chromium-ungoogled, but it still seems broken. Thanks.

@deeelwy
Copy link

deeelwy commented Dec 17, 2022

Yeah, still broken. It hilariously seems to cause Spotify to only play ads never any music at all. lol.

I guess Spotify can detect the muting with javascript, though I have no clue if they bother.

Maybe there's a way to mute the ads at a higher level in the browser that Spotify can't detect that doesn't cause the mute/unmute button to change, for example. I don't know. Maybe you'd need to trigger an external tool (ex: cmdline tool) to mute it at the OS level instead???

@deeelwy
Copy link

deeelwy commented Dec 17, 2022

When I mute my OS manually using the volume thingy in the bottom bar, it isn't detected by the javascript stuff in the browser window.

So, maybe an external tool is an option, but I don't know if Chrome would let you do that. Old firefox probably would, but maybe not new firefox, because it's a copy and paste of chrome now.

@marcbelmont
Copy link
Author

@deeelwy thanks for the feedback. I'll see what I can do.

@RoguedBear
Copy link

spotify recently updated their UI, so the old element selector for now playing bar is not working.

I have updated the selector for nowPlayingBar and adQuerySelector and can confirm from my side the script is muting the ads and unmuting them afterwards

-    const nowPlayingBar = await queryAsync('.Root__now-playing-bar');
+    const nowPlayingBar = await queryAsync('[data-testid=now-playing-widget]');
     const volumeButton = await queryAsync('button.volume-bar__icon-button');
-    const adQuerySelector = '.Root__now-playing-bar *[aria-label~=Advertisement]';
+    const adQuerySelector = '[data-testid=now-playing-widget] *[aria-label~=Advertisement]';

@marcbelmont
Copy link
Author

@RoguedBear thanks for feedback. I'm updating it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment