Skip to content

Userscript that allows getting modules with their categories/ids #17

Description

@Argavyon

Figured this could be handy

// ==UserScript==
// @name         Modules downloader
// @namespace    http://tampermonkey.net/
// @version      2025-01-10
// @description  Get'em modules
// @author       Argavyon
// @match        https://www.perfecttower2.com/db/modules
// @match        https://www.perfecttower2.com/db/modules/*
// @icon         https://www.perfecttower2.com/images/android-icon-96x96.png
// @grant        none
// @run-at       document-idle
// ==/UserScript==

function downloadModules() {
    // Make a module dictionary from the global variable "modules"
    const modDict = Object.fromEntries(
        window.modules.map(
            (module) => [ module.id, module ]
        )
    );

    // Convert from JSON dictionary syntax to lua table syntax
    const luaTable = JSON.stringify(modDict, null, 4).replaceAll(/(".*"):/g, "[$1] =");

    const luaCode = `modules = ${luaTable}`;

    const donwload = document.createElement('a');
    donwload.href = `data:text/plain;charset=utf-8,${encodeURIComponent(luaCode)}`;
    donwload.download = 'modules.lua';
    donwload.style.display = 'none';

    document.body.appendChild(donwload);
    donwload.click();
    document.body.removeChild(donwload);
}

function main() {
    const downloader = document.createElement('button');
    downloader.textContent = 'Download Modules';
    downloader.setAttribute('class', 'btn btn-info');
    downloader.setAttribute('type', 'button');
    downloader.onclick = (event) => {
        downloadModules();
        event.target.onclick = null;
    };

    document.querySelector('.section .container').appendChild(downloader);
}

main();

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions