ApplMscCpyforF

For personal use

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         ApplMscCpyforF
// @namespace    https://greatest.deepsurf.us/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);
})();