Skip to content

Instantly share code, notes, and snippets.

@abrahamjuliot
Last active July 8, 2017 17:47
Show Gist options
  • Save abrahamjuliot/008254f178ae23caf5ecfd62c879614b to your computer and use it in GitHub Desktop.
Save abrahamjuliot/008254f178ae23caf5ecfd62c879614b to your computer and use it in GitHub Desktop.
blocks mp3 Ads on spotify web player (2017)
(function() {
// VARIABLES
const
// dom terms
dom = document,
query = 'querySelector',
parent = 'parentNode',
next = 'nextElementSibling',
attr = 'getAttribute',
on = 'addEventListener',
html = 'innerHTML';
// helpers
getNextTrack = (active) => {
if (active !== null) {
// if there is no next track
if (active[parent][next] === null) {
// return the first track
return active[parent][parent][query](
'.react-contextmenu-wrapper:first-of-type'
);
} else {
// return the next track
return active[parent][next];
}
}
},
getCurrentTrack = () => dom[query](
'.now-playing .track-info__name a'
),
href = (el) => el[attr]('href'),
trackName = (el) => el[query]('.track-name')[html],
message = (delay) => {
setTimeout(() => {
// set active track
activeTrack = dom[query]('li.active');
// set reference to next track
nextTrack = getNextTrack(activeTrack);
if (activeTrack !== null) {
console.log(
'"'+trackName(activeTrack)+'"'+
" is active and "+
'"'+trackName(nextTrack)+'"'+
" is next"
);
} else {
console.warn('Sorry, Ads are only blocked in playlist & album views.');
}
}, delay);
};
let
// elements
nextTrack = null,
activeTrack = dom[query]('li.active'),
nowPlaying = null,
// strings
album = "",
track = "",
// numbers
countOfTrackChanges = 0;
// PROGRAM
message(5000);
// delay 7s
setTimeout(() => {
// set now playing track
nowPlaying = getCurrentTrack();
track = nowPlaying[html];
// Listen for changes to track name
setInterval(() => {
// when track name changes
if (track !== nowPlaying[html]) {
track = nowPlaying[html];
nowPlaying = getCurrentTrack();
message(7000);
console.log('('+(++countOfTrackChanges)
+') Detected track change...'
+' playing "'+track+'"'
);
// when the album changes
if (album !== href(nowPlaying)) {
album = href(nowPlaying);
console.log(
"***New album in view ["
+album+"]***"
);
}
// when the album link contains an Ad link
if (nowPlaying[attr]('href').indexOf("shrt") > -1) {
console.log('Spotify Ad detected!');
nextTrack[query]('li .play-pause').click();
console.log(
'Skipped to '+
'"'+trackName(nextTrack)+'"'
);
countOfTrackChanges = 0; // reset
}
}
}, 300);
}, 7000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment