Raw Source
okj579 / Songlink for Google Music

// ==UserScript==
// @name         Songlink for Google Music
// @description  Replaces share links in Google Music, YouTube Music & Spotify with cross-provider links via song.link
// @author       okj579
// @license      MIT
// @icon         https://song.link/favicon.ico
// @source       https://openuserjs.org/scripts/okj579/Songlink_Universal_Share_Links
// @updateURL    https://openuserjs.org/meta/okj579/Songlink_Universal_Share_Links.meta.js
// @downloadURL  https://openuserjs.org/src/scripts/okj579/Songlink_Universal_Share_Links.user.js
// @version      1.2.1
// @match        https://play.google.com/music/listen*
// @match        https://open.spotify.com/*
// @match        https://music.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    if (window.location.origin == 'https://play.google.com') {
        document.addEventListener('iron-overlay-opened', function(e) {
            if (!e.target.classList.contains('get-link')) return;
            var link = document.querySelector('.link', e.target);
            link.value = link.value.replace(/^.*\/\/play.google.com\/music\/m\/(.*)\?.*$/, 'https://song.link/g/$1');
            document.querySelector('input.paper-input', link).select();
        });
    }

    if (window.location.origin == 'https://music.youtube.com') {
        document.addEventListener('iron-overlay-opened', function(e) {
            var input = document.querySelector('input#share-url', e.target);
            if (!input) return;
            input.value = input.value.replace(/^.*\/\/music.youtube.com\/watch\?v=([^&]*).*?$/, 'https://song.link/y/$1');
        });
    }

    if (window.location.origin == 'https://open.spotify.com') {
        document.addEventListener('click', function(e) {
            var textarea  = e.target.lastElementChild;
            if (e.target.classList.contains('react-contextmenu-item') && textarea && textarea.type === 'textarea') {
                textarea.value = textarea.value.replace('https://open.spotify.com/track/', 'https://song.link/s/');
            }
        });
    }
})();