ApplMscCpyforF

For personal use

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Advertisement:

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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