AppleMusicToSpotify

append link to Spotify search at Apple Music

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         AppleMusicToSpotify
// @description  append link to Spotify search at Apple Music
// @version      0.2.1
// @namespace    https://github.com/to
// @match        https://music.apple.com/*/album/*
// @match        https://music.apple.com/*/playlist/*
// ==/UserScript==

// original
// https://qiita.com/embokoir/items/d667a6802105b842fb48

let playlist = !!location.href.match('/playlist/');
let artist = document.querySelector('.product-creator').textContent.trim();
let observer = new MutationObserver(records => {
  setTimeout(() => {
    [...document.getElementsByClassName('song-name')].forEach(elmName => {
      let name = elmName.innerText;
      if(playlist)
        artist = elmName.nextElementSibling.textContent.trim();
      
      let elmLink = document.createElement('a');
      elmLink.setAttribute('href', `https://open.spotify.com/search/${artist} ${name}`);
      elmLink.setAttribute('target', '_blank');
      elmLink.style.color = 'hsl(144, 73%, 41%)';
      elmLink.className = elmName.className;
      elmLink.innerText = name;

      elmName.parentNode.replaceChild(elmLink, elmName);
    });
  }, 0);
});

observer.observe(document.body, {
  childList: true
});