Apple Music Context menu Replacer

Replaces the browser context menu with the apple music one

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Apple Music Context menu Replacer
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Replaces the browser context menu with the apple music one
// @author       Cadentem
// @match        https://*.music.apple.com/*
// @icon         https://www.google.com/s2/favicons?domain=music.apple.com
// @grant        window.onurlchange
// ==/UserScript==

(function() {
    'use strict';

    let simulateClick = function (element, clientX, clientY) {
        // calling .click() does not work

        let event = new MouseEvent('click', {
            clientX: clientX,
            clientY: clientY
        });

        element.dispatchEvent(event);
    };

    let run = function() {
        // do a maximum of 10 tries
        let maxWait = 5000;

        let waitFor = (...selectors) => new Promise(resolve => {
            let delay = 500

            let resolver = () => {
                // make sure the required elements are loaded
                let songControlls = document.getElementsByClassName("songs-list-row");

                if (songControlls.length === 0) {
                    // Titles page
                    songControlls = document.getElementsByClassName("library-track");
                }

                if (songControlls.length > 0 || maxWait <= 0) {
                    resolve(songControlls)
                } else {
                    maxWait = maxWait - delay;
                    setTimeout(resolver, delay)
                }
            }

            resolver();
        });

        waitFor().then((songControlls) => {
            for (let songControll of songControlls) {
                songControll.addEventListener('contextmenu', function(event) {
                    event.preventDefault();

                    let controll = songControll.getElementsByClassName("context-menu__overflow ")[0];

                    if (controll) {
                        simulateClick(controll, event.clientX, event.clientY);
                    }
                });
            }
        });
    }

    if (window.onurlchange === null) {
        window.addEventListener('urlchange', (info) => {
            run()
        });
    }

    run();
})();