通过AI改良版的Spotify Downloader 油猴安装

// ==UserScript==
// @name         Spotify Downloader - Auto Paste on spotifydown.com
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  Automatically pastes the Spotify link and clicks the paste button on spotifydown.com. Hides the "Download" text on Spotify page.
// @author       Zertalious (Zert), Modified by Assistant
// @match        *://open.spotify.com/*
// @match        *://spotifydown.com/*
// @grant        none
// ==/UserScript==

const style = document.createElement('style');
style.innerText = `
[role='grid'] {
    margin-left: 50px;
}

[data-testid='tracklist-row'] {
    position: relative;
}

[role="presentation"] > * {
    contain: unset;
}

.btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 0;
    background-color: #1fdf64;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20"><path d="M17 12v5H3v-5H1v5a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-5z"/><path d="M10 15l5-6h-4V1H9v8H5l5 6z"/></svg>');
    background-position: center;
    background-repeat: no-repeat;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

.btn:hover {
    transform: scale(1.1);
    background-color: orange;
}

[data-testid='tracklist-row'] .btn {
    position: absolute;
    top: 50%;
    right: 100%;
    margin-top: -20px;
    margin-right: 10px;
}

.hide-text {
    color: transparent; /* 隐藏文本 */
    text-shadow: 0 0 0 #000; /* 确保空间保留 */
}
`;
document.body.appendChild(style);

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

function fillSpotifyLink(spotifyUrl) {
    if (window.location.hostname === 'spotifydown.com') {
        const inputField = document.querySelector('.searchInput[aria-label="Spotify Link"]');
        if (inputField) {
            inputField.value = spotifyUrl;

            // 强制触发 input 事件
            const inputEvent = new Event('input', { bubbles: true });
            inputField.dispatchEvent(inputEvent);

            // 找到并点击“粘贴”按钮
            const pasteButton = document.querySelector('button.flex.items-center.bg-zinc-500');
            if (pasteButton) {
                console.log("Clicking paste button...");
                pasteButton.click();
            } else {
                console.log("Paste button not found!");
            }
        }
    } else {
        // 如果是在 Spotify 页面,将链接复制到剪贴板并打开 spotifydown.com
        navigator.clipboard.writeText(spotifyUrl);
        window.open(`https://spotifydown.com/?link=${encodeURIComponent(spotifyUrl)}`, '_blank');
    }
}

async function handleSpotifyPage() {
    // 隐藏页面上的“Download”按钮
    const existingButtons = document.querySelectorAll('.btn');
    existingButtons.forEach(button => {
        button.classList.add('hide-text'); // 隐藏文本
        button.setAttribute('aria-label', 'Download'); // 确保功能正常
    });

    // 在 Spotify 页面运行时,每秒检查下载按钮是否存在,并绑定操作
    const tracks = document.querySelectorAll('[data-testid="tracklist-row"]');
    for (let i = 0; i < tracks.length; i++) {
        const track = tracks[i];
        if (!track.hasButton) {
            addButton(track).onclick = async function() {
                const spotifyUrl = window.location.href;
                fillSpotifyLink(spotifyUrl);
            };
        }
    }

    const actionBarRow = document.querySelector('[data-testid="action-bar-row"]:last-of-type');
    if (actionBarRow && !actionBarRow.hasButton) {
        addButton(actionBarRow).onclick = function() {
            const spotifyUrl = window.location.href;
            fillSpotifyLink(spotifyUrl);
        };
    }
}

function addButton(el) {
    const button = document.createElement('button');
    button.className = 'btn hide-text'; // 添加隐藏文本的类
    el.appendChild(button);
    el.hasButton = true;
    return button;
}

if (window.location.hostname === 'spotifydown.com') {
    // 适当延迟等待页面加载完成,然后处理粘贴功能
    window.onload = async function() {
        await sleep(2000); // 等待页面加载完成
        const spotifyUrl = new URLSearchParams(window.location.search).get('link');
        if (spotifyUrl) {
            fillSpotifyLink(spotifyUrl);
        }
    };
} else if (window.location.hostname === 'open.spotify.com') {
    // 在 Spotify 页面处理按钮添加
    setInterval(handleSpotifyPage, 1000);
}


通过AI改良版的Spotify Downloader 油猴安装
http://blog.021800.xyz/2024/09/11/spotify-downloader
作者
Administrator
发布于
2024年09月11日
更新于
2024年09月11日
许可协议