mmdfans1click

Download videos from MMDFans with Author, Date, Title, VideoID with one click, heavily based off wzj042's script, modified with copilot
 avatar
unknown
javascript
a year ago
7.1 kB
257
No Index
// ==UserScript==
// @name         MMD Fans 1-Click-DL
// @namespace    http://tampermonkey.net/
// @version      1.0.4
// @description  Download videos from MMDFans with Author, Date, Title, VideoID with one click, heavily based off wzj042's script, modified with copilot
// @author       wzj042
// @match        https://mmdfans.net/mmd/*
// @match        https://cdn.mmdlibrary.eu.org/*
// @match        https://cdn.mmdlibrary0.eu.org/*
// @match        https://cdn.mmdlibrary1.eu.org/*
// @match        https://cdn.mmdlibrary2.eu.org/*
// @match        https://cdn.mmdlibrary3.eu.org/*
// @match        https://cdn.bakabakaxi.eu.org/*
// @match        https://cdn.baka6.eu.org/*
// @match        https://cdn.baka7.eu.org/*
// @match        https://cdn.baka8.eu.org/*
// @match        https://cdn.baka9.eu.org/*
// @match        https://cirno.baka9.eu.org/*
// @license      MIT
// @grant        none
// @downloadURL  https://update.greasyfork.org/scripts/476557/mmdfans%E4%B8%80%E9%94%AE%E4%B8%8B%E8%BD%BD.user.js
// @updateURL    https://update.greasyfork.org/scripts/476557/mmdfans%E4%B8%80%E9%94%AE%E4%B8%8B%E8%BD%BD.meta.js
// ==/UserScript==

(async function () {
    'use strict';

    // add new cdn links here, and also add @match above
    const cdn_list = [
        'https://cdn.mmdlibrary.eu.org',
        'https://cdn.mmdlibrary0.eu.org',
        'https://cdn.mmdlibrary1.eu.org',
        'https://cdn.mmdlibrary2.eu.org',
        'https://cdn.mmdlibrary3.eu.org',
        'https://cdn.bakabakaxi.eu.org',
        'https://cdn.baka6.eu.org',
        'https://cdn.baka7.eu.org',
        'https://cdn.baka8.eu.org',
        'https://cdn.baka9.eu.org',
        'https://cirno.baka9.eu.org'
    ];

    let downloadFlag = false;

    const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

    function getVideoSource() {
        const sourceElement = document.querySelector('video source');
        return sourceElement ? sourceElement.getAttribute('src') : '';
    }

    function createButton(text, classNames, onClick) {
        const button = document.createElement('button');
        button.innerHTML = text;
        button.classList.add(...classNames);
        button.onclick = onClick;
        return button;
    }

    // Get video upload date
    function extractPostAtDate() {
        const postAtButton = [...document.querySelectorAll('.tag-container button')]
            .find(button => button.textContent.trim() === 'PostAt');
        if (postAtButton) {
            const dateButton = postAtButton.nextElementSibling;
            if (dateButton) {
                const dateTime = dateButton.textContent.trim();
                const datePart = dateTime.split(' ')[0]; // Extract the date part (YYYY-MM-DD)
                return datePart;
            }
        }
        return 'NoDate';
    }

    // Get video ID
    function extractVideoId() {
        const sourceButton = [...document.querySelectorAll('.tag-container button')]
            .find(button => button.textContent.trim() === 'Source');
        if (sourceButton) {
            const linkElement = sourceButton.nextElementSibling;
            if (linkElement && linkElement.tagName === 'A') {
                const url = new URL(linkElement.href);
                return url.pathname.split('/').pop();
            }
        }
        return 'NoID';
    }

    // Get video author
    function extractAuthor() {
        const authorButton = [...document.querySelectorAll('.tag-container button')]
            .find(button => button.textContent.trim() === 'Author');
        if (authorButton) {
            const authorLink = authorButton.nextElementSibling;
            if (authorLink && authorLink.tagName === 'A') {
                const authorNameButton = authorLink.querySelector('button');
                if (authorNameButton) {
                    return authorNameButton.textContent.trim();
                }
            }
        }
        return 'NoAuthor';
    }

    // Get video title
    function extractTitle() {
        const titleElement = document.querySelector('.mdui-typo h2.title');
        return titleElement ? titleElement.textContent.trim() : 'NoTitle';
    }

    async function checkVideoTag() {
        while (true) {
            const videoTag = document.querySelector('.mdui-video-fluid');
            const downloadLink = getVideoSource();
            const title = extractTitle();
            const authorName = extractAuthor();
            const date = extractPostAtDate();
            const videoId = extractVideoId();

            if (videoTag) {
                const downloadButton = createButton('DL Video', ['mdui-btn', 'mdui-color-theme-accent', 'mdui-ripple'], () => {
                    if (!isCdnLink(downloadLink)) {
                        alert('The current link is not a cdn link, please add the cdn manually');
                        return;
                    }
                    download(downloadLink + '?title=' + title + '&author=' + authorName + '&date=' + date + '&id=' + videoId, title, authorName);
                });

                const copyCdnButton = createButton('Copy cdn link', ['mdui-btn', 'mdui-color-theme-accent', 'mdui-ripple'], () => {
                    if (!isCdnLink(downloadLink)) {
                        alert('The current link is not a cdn link, please add the cdn manually');
                        return;
                    }
                    const url = new URL(downloadLink);
                    navigator.clipboard.writeText(url.origin);
                    alert('cdn Copied');
                });

                const tagContainer = document.querySelector('.tag-container');
                tagContainer.appendChild(downloadButton);
                tagContainer.appendChild(document.createTextNode('\u00A0\u00A0'));
                tagContainer.appendChild(copyCdnButton);

                break;
            } else if (!downloadFlag && isCdnLink(window.location.href)) {
                const curUrl = window.location.href;
                const url = new URL(curUrl);
                const title = url.searchParams.get('title') || 'NoTitle';
                const author = url.searchParams.get('author') || 'NoAuthor';
                const date = url.searchParams.get('date') || 'NoDate';
                const id = url.searchParams.get('id') || 'NoID';

                download(curUrl, title, author, date, id);
            }

            await delay(1000);
        }
    }

    function isCdnLink(url) {
        return cdn_list.some(cdn => url.startsWith(cdn));
    }

    function download(url, title, author, date, id) {
        const a = document.createElement("a");
        a.download = `[${author}] ${date} ${title} [${id}].mp4`;
        a.href = url;
        a.click();
        a.remove();
        setTimeout(() => {
            if (isCdnLink(window.location.href) && downloadFlag) {
                window.history.back();
            }
            downloadFlag = false;
        }, 1100);

        downloadFlag = true;
    }

    checkVideoTag();
})();
Editor is loading...
Leave a Comment