Created
March 10, 2023 01:24
-
-
Save 0x53/6a57eba7d2b9e25fff3839638852d6a2 to your computer and use it in GitHub Desktop.
Mute Spotify Ads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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