CurseForge Direct Download

Provide a direct download link for files in CurseForge

目前为 2023-07-07 提交的版本。查看 最新版本

// ==UserScript==
// @name         CurseForge Direct Download
// @namespace    FaustVXCurseForge
// @version      1.0
// @description  Provide a direct download link for files in CurseForge
// @author       FaustVX
// @match        https://legacy.curseforge.com/*/*/*/files/*
// @match        https://www.curseforge.com/*/*/*/files/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=curseforge.com
// @supportURL   https://gist.github.com/FaustVX/e5320dff2648abe3809403160628fa26#comments
// @grant        none
// @license      MIT
// ==/UserScript==

const run = function() {
    'use strict';

    function createURL(fileId, fileName)
    {
        return "https://mediafilez.forgecdn.net/files/" + Number(fileId.slice(0, 4)) + "/" + Number(fileId.slice(4)) + "/" + encodeURIComponent(fileName);
    }

    function changeTag(node, tag) {
        const clone = createElement(tag)
        for (const attr of node.attributes) {
            clone.setAttributeNS(null, attr.name, attr.value)
        }
        while (node.firstChild) {
            clone.appendChild(node.firstChild)
        }
        node.replaceWith(clone)
        return clone
    }

    function createElement(tag) {
        if (tag === 'svg') {
            return document.createElementNS('http://www.w3.org/2000/svg', 'svg')
        } else {
            return document.createElementNS('http://www.w3.org/1999/xhtml', tag)
        }
    }

    var fileId = window.location.href.split('/')[7];
    if (window.location.href.split('/')[2].startsWith("legacy")) {
        const column = document.getElementsByTagName("article")[0].children[1].firstElementChild;
        changeTag(column.lastElementChild, "a").href = createURL(fileId, column.lastElementChild.innerText);
    } else {
        const section = document.getElementsByClassName("section-file-name")[0];
        changeTag(section.lastElementChild, "a").href = createURL(fileId, section.lastElementChild.innerText);
    }
};

// Convenience function to execute your callback only after an element matching readySelector has been added to the page.
// Example: runWhenReady('.search-result', augmentSearchResults);
// Gives up after 1 minute.
// based on https://github.com/Tampermonkey/tampermonkey/issues/1279#issuecomment-875386821
function runWhenReady(readySelector, callback) {
    var numAttempts = 0;
    const tryNow = function() {
        const elem = document.querySelector(readySelector);
        if (elem) {
            callback(elem);
        } else {
            numAttempts++;
            if (numAttempts >= 34) {
                console.warn('Giving up after 34 attempts. Could not find: ' + readySelector);
            } else {
                setTimeout(tryNow, 250 * Math.pow(1.1, numAttempts));
            }
        }
    };
    tryNow();
}

runWhenReady(".section-file-name,article", run);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址