Ad Skipper for Spotify Web Player

Automatically skip ads on Spotify.

// ==UserScript==
// @name         Ad Skipper for Spotify Web Player
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  Automatically skip ads on Spotify.
// @author       Tomoyuki Kawao
// @match        https://open.spotify.com/*
// @license      MIT
// @grant        none
// @run-at       document-start
// ==/UserScript==

document.createElement = function(originalCreateElement) {
    return function() {
        var element = originalCreateElement.apply(this, arguments);
        if (element instanceof HTMLMediaElement) {
            element.addEventListener("play", (event) => {
                if (document.querySelector("[data-testadtype=\"ad-type-ad\"]")) {
                    event.currentTarget.currentTime = Number.isFinite(event.currentTarget.duration) ? event.currentTarget.duration : 30.0;
                }
            });
        }
        return element;
    };
}(document.createElement);