ApplMscCpyforF

For personal use

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

Advertisement:

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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