Apple Music Context menu Replacer

Replaces the browser context menu with the apple music one

  1. // ==UserScript==
  2. // @name Apple Music Context menu Replacer
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Replaces the browser context menu with the apple music one
  6. // @author Cadentem
  7. // @match https://*.music.apple.com/*
  8. // @icon https://www.google.com/s2/favicons?domain=music.apple.com
  9. // @grant window.onurlchange
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. let simulateClick = function (element, clientX, clientY) {
  16. // calling .click() does not work
  17.  
  18. let event = new MouseEvent('click', {
  19. clientX: clientX,
  20. clientY: clientY
  21. });
  22.  
  23. element.dispatchEvent(event);
  24. };
  25.  
  26. let run = function() {
  27. // do a maximum of 10 tries
  28. let maxWait = 5000;
  29.  
  30. let waitFor = (...selectors) => new Promise(resolve => {
  31. let delay = 500
  32.  
  33. let resolver = () => {
  34. // make sure the required elements are loaded
  35. let songControlls = document.getElementsByClassName("songs-list-row");
  36.  
  37. if (songControlls.length === 0) {
  38. // Titles page
  39. songControlls = document.getElementsByClassName("library-track");
  40. }
  41.  
  42. if (songControlls.length > 0 || maxWait <= 0) {
  43. resolve(songControlls)
  44. } else {
  45. maxWait = maxWait - delay;
  46. setTimeout(resolver, delay)
  47. }
  48. }
  49.  
  50. resolver();
  51. });
  52.  
  53. waitFor().then((songControlls) => {
  54. for (let songControll of songControlls) {
  55. songControll.addEventListener('contextmenu', function(event) {
  56. event.preventDefault();
  57.  
  58. let controll = songControll.getElementsByClassName("context-menu__overflow ")[0];
  59.  
  60. if (controll) {
  61. simulateClick(controll, event.clientX, event.clientY);
  62. }
  63. });
  64. }
  65. });
  66. }
  67.  
  68. if (window.onurlchange === null) {
  69. window.addEventListener('urlchange', (info) => {
  70. run()
  71. });
  72. }
  73.  
  74. run();
  75. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址