Spotify Web - Copy track info to clipboard

Adds an entry in the context menu that copies the selected song name and artist to the clipboard

当前为 2017-12-27 提交的版本,查看 最新版本

// ==UserScript==
// @name          Spotify Web - Copy track info to clipboard
// @description   Adds an entry in the context menu that copies the selected song name and artist to the clipboard
// @namespace     https://openuserjs.org/users/cuzi
// @version       1
// @license       MIT
// @copyright     2017, cuzi (https://openuserjs.org/users/cuzi)
// @require       https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @grant         GM.setClipboard
// @grant         GM_setClipboard
// @include       https://open.spotify.com/*
// ==/UserScript==

// ==OpenUserJS==
// @author       cuzi
// ==/OpenUserJS==

"use strict";

(function () {

  let populateContextMenu = function (ev) {
    let $this = $(this);

    let menu = $(".react-contextmenu--visible")
    let title = $this.find(".tracklist-name");
    let artist = $this.find(".artists-album span");

    if (title && artist && menu[0]) {
      let title_text = title.text();
      let artist_text = artist.not((i, e) => e.className).text();

      // Create context menu entry
      let entry = menu.find(".gmcopytrackinfo");
      if (!entry[0]) {
        entry = $('<div class="react-contextmenu-item gmcopytrackinfo" role="menuitem" tabindex="-1" aria-disabled="false">Copy track info</div>').appendTo(menu);
      }

      entry.unbind(".gmcopytrackinfo").bind("click.gmcopytrackinfo", function (ev) {
        // Copy string to clipboard
        if (typeof GM_setClipboard !== 'undefined') {
          GM_setClipboard(artist_text + " - " + title_text);
        }
        else if (GM.setClipboard) {
          GM.setClipboard(artist_text + " - " + title_text);
        }
        alert("Copied:\n" + artist_text + " - " + title_text);
      });
    }
  };

  let onContextMenu = function (ev) {
    // Wait for the React context menu to open
    let t = this;
    window.setTimeout(function () {
      populateContextMenu.call(t, ev);
    }, 200);
  };

  let bindEvents = function () {
    // Remove all events and then reattach them
    $(".react-contextmenu-wrapper").unbind(".gmcopytrackinfo").bind("contextmenu.gmcopytrackinfo", onContextMenu);

  };

  window.setInterval(bindEvents, 3000);

})();