AppleMusicToSpotify

append link to Spotify search at Apple Music

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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         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
});