TP Maptest U-M button

Adds a button to the tagpro-test map test page to test a map from a U-M ID.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         TP Maptest U-M button
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds a button to the tagpro-test map test page to test a map from a U-M ID.
// @author       E. Lek-Tro
// @match        http://tagpro-maptest.koalabeast.com/testmap
// @match        http://tagpro-maptest.koalabeast.com/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    $(".form").parent().append("<button id='umIDBtn' class='btn btn-primary'>U-M ID</button>")

    $("#umIDBtn").click(e => {
        changeMapFromID(prompt("Map ID:")).then(() => {
            $(".form .form-group:last-child button[type='submit']").click();
        });
    });

    $("#play-now").attr("href", "http://tagpro-maptest.koalabeast.com/testmap");

    function changeMapFromID(id){
        return new Promise(async (resolve, reject) => {
            console.log("id:", id);
            if(isNaN(Number(id)) || id === "") id = Math.floor(Math.random() * 60000);
            let pngBlob = await fetch(`https://parretlabs.xyz:8006/proxy?isBlob=1&type=image/png&link=http://unfortunate-maps.jukejuice.com/static/maps/${id}.png`)
            .then(res => res.blob());
            let jsonBlob = await fetch(`https://parretlabs.xyz:8006/proxy?type=application/json&link=http://unfortunate-maps.jukejuice.com/static/maps/${id}.json`)
            .then(res => res.blob());

            const pngTransfer = new ClipboardEvent('').clipboardData || new DataTransfer();
            const jsonTransfer = new ClipboardEvent('').clipboardData || new DataTransfer();
            pngTransfer.items.add(new File([pngBlob], 'map.png', {type: "image/png"}));
            jsonTransfer.items.add(new File([jsonBlob], 'map.json', {type: "application/json"}));

            document.querySelector("input[name='layout']").files = pngTransfer.files;
            document.querySelector("input[name='logic']").files = jsonTransfer.files;

            console.log(pngBlob, jsonBlob)

            setTimeout(function(){
                resolve();
            }, 100);
        });
    }

    // Your code here...
})();