Skip to content

Instantly share code, notes, and snippets.

@0x53
Created March 10, 2023 01:24
Show Gist options
  • Save 0x53/6a57eba7d2b9e25fff3839638852d6a2 to your computer and use it in GitHub Desktop.
Save 0x53/6a57eba7d2b9e25fff3839638852d6a2 to your computer and use it in GitHub Desktop.
Mute Spotify Ads
function adPlaying(){
// Determine if add is playing
var skipButton = document.querySelector('[aria-label="Next"]')
if(skipButton.disabled == true){
console.log("An ad is playing");
return true;
}else{
return false;
}
}
function muteAudio(action){
var clickButton = null;
if(action){
// Mute the audio
console.log("Clicking the mute button")
clickButton = document.querySelector('[aria-label="Mute"]');
}else{
// Unmute the audio
console.log("Clicking the unmute button")
clickButton = document.querySelector('[aria-label="Unmute"]');
}
// click the button
if (clickButton != null){
clickButton.click()
}
}
// See if the title has changed
const target = document.querySelector('title');
// set config for observer
const config = { attributes: true, childList: true, subtree: true, characterData: true };
// Callback function
const callback = function(mutations, observer) {
// check if ad is playing
if(adPlaying()){
muteAudio(true);
}else{
muteAudio(false);
}
};
var observer = new MutationObserver(callback);
observer.observe(target, config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment