ApplMscCpyforF

For personal use

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Advertisement:

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

Advertisement:

// ==UserScript==
// @name         ApplMscCpyforF
// @namespace    https://greasyfork.org/ja/users/1582093-mxmoor
// @version      0.9
// @description  For personal use
// @author       SY
// @match        https://music.apple.com/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    //アルバムのリストにコピペのボタンを追加
    function initScript(){
        const elements = document.querySelectorAll('[data-row]');
        document.querySelectorAll('.my-custom-button').forEach(function(element) {
            element.remove();
        });
        elements.forEach(el => {
            const targetElement = el.querySelector('.songs-list-row__song-container');

            //ボタン要素を作成
            const newButton = document.createElement('button');
            newButton.textContent = '📋 ';
            newButton.style.fontSize = "20px";
            newButton.className = 'my-custom-button';
            //クリックイベント
            newButton.addEventListener('click', () => {
                //曲名
                const songTitle = el.querySelector('div[data-testid="track-title"]');
                const title = songTitle.textContent;
                //時間
                const songDuration = el.querySelector('time[data-testid="track-duration"]');
                const duration = songDuration.textContent;
                //アルバム名
                const songAlbum = document.querySelector('.headings__title');
                const album = songAlbum.textContent.slice(0, -1);//最後に半角スペースがあるのでスライス
                //アーティスト名
                const songArtist = document.querySelector('div[data-testid="product-subtitles"]');
                let artist = songArtist.textContent;
                //個別にアーティストの値がある場合は再代入
                if(el.querySelector('div[data-testid="track-title-by-line"]')){
                    artist = el.querySelector('div[data-testid="track-title-by-line"]').textContent;
                }
                //垂直タブ
                const tab = String.fromCharCode(9);

                navigator.clipboard.writeText(title + tab + artist + tab + album + tab + tab + duration);

                newButton.textContent = '📎 ';
                setTimeout(() => {
                    newButton.textContent = '📋 '
                }, 1000);
            });
            targetElement.appendChild(newButton);
        });
    };

    //プレイリストにコピペのボタンを追加
    function playlistCopy(){
        const elements = document.querySelectorAll('[data-testid]');
        document.querySelectorAll('.my-custom-button').forEach(function(element) {
            element.remove();
        });

            const targetElement = document.querySelector('.headings__title');

            //ボタン要素を作成
            const newButton = document.createElement('button');
            newButton.textContent = '📋 ';
            newButton.style.fontSize = "20px";
            newButton.className = 'my-custom-button';
            //クリックイベント
            newButton.addEventListener('click', () => {
                const a = [];
                const ent = String.fromCharCode(10);
                const tab = String.fromCharCode(9);
                const elements = document.querySelectorAll('[data-row]');
                elements.forEach(el => {
                    //曲名
                    const songTitle = el.querySelector('.songs-list-row__song-name');
                    const title = songTitle.textContent;
                    //時間
                    var duration = '';
                    if(el.querySelector('time[data-testid="track-duration"]')){
                        const songDuration = el.querySelector('time[data-testid="track-duration"]');
                        duration = songDuration.textContent;
                    }
                    //アルバム名
                    const songAlbum = el.querySelector('div[data-testid="track-column-tertiary"]');
                    const album = songAlbum.textContent;//.slice(0, -1);//最後に半角スペースがあるのでスライス
                    //アーティスト名
                    const songArtist = el.querySelector('div[data-testid="track-column-secondary"]');
                    const artist = songArtist.textContent;
                    var song = el.textContent;
                    a.push(title + tab + artist + tab + album + tab + tab + duration + ent);
                });

                navigator.clipboard.writeText(a.join(''));

                newButton.textContent = '📎 ';
                setTimeout(() => {
                    newButton.textContent = '📋 '
                }, 1000);
            });
            targetElement.appendChild(newButton);
    };

    // ページタイトルの変化を監視する
    const titleChange = document.querySelector('title');
    const config = {
        subtree: true,
        childList: true,
        //attributes: true,
        characterData: true
    };

    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.type === 'childList' || mutation.type === 'characterData') {
                checkUrl();
            }
        });
    });

    function checkUrl() {
        if (window.location.href.includes("https://music.apple.com/jp/album/")) {
            window.setTimeout(initScript, 2000);
        }else if(window.location.href.includes("https://music.apple.com/jp/playlist/")){
            window.setTimeout(playlistCopy, 2000);
        }
    }

    // ページロード時にチェック
    window.addEventListener('load', checkUrl);

    // 履歴操作や戻る/進むでチェック
    window.addEventListener("popstate", checkUrl);

    window.setTimeout(() => {
        observer.observe(titleChange,config);
    }, 3000);
})();