ApplMscCpyforF

For personal use

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

Advertisement:

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

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