falinks-teambuilder-helper

A UserScript helps import teams to a Falinks Teambuilder room

目前為 2022-09-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name          falinks-teambuilder-helper
// @description   A UserScript helps import teams to a Falinks Teambuilder room
// @namespace     http://tampermonkey.net/
// @version       0.0.1
// @author        txfs19260817
// @icon          https://www.falinks-teambuilder.com/favicon.ico
// @source        https://github.com/txfs19260817/falinks-teambuilder-helper
// @license       WTFPL
// @match         http*://play.pokemonshowdown.com/*
// @match         http*://*.psim.us/*
// @match         http*://pokepast.es/*
// @require       https://unpkg.com/@pkmn/sets/build/production.min.js
// @connect       www.falinks-teambuilder.com
// @run-at        document-end
// ==/UserScript==


/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
var __webpack_exports__ = {};

;// CONCATENATED MODULE: external "window"
const external_window_namespaceObject = window;
;// CONCATENATED MODULE: ./src/index.ts

const pokepasteURL = "pokepast.es";
const createRoomButtonId = "falinks-new-room-btn";
function S4() {
    return ((1 + Math.random()) * 0x10000 | 0).toString(16).substring(1);
}
function falinksRoomEndpoint(packed) {
    return `//www.falinks-teambuilder.com/room/room_${S4()}${S4()}/?protocol=WebSocket&packed=${encodeURIComponent(packed)}`;
}
function pasteToPacked(paste) {
    var ref;
    // pkmnSets.Teams is available in dev, 
    // whereas `pkmnSets.PokemonTeams` is equivalent to `window.PokemonTeams` exported in prod
    //@ts-ignore
    const teams = external_window_namespaceObject.Teams ?? external_window_namespaceObject.PokemonTeams;
    return ((ref = teams.importTeam(paste)) === null || ref === void 0 ? void 0 : ref.pack()) ?? '';
}
function createRoomButton(packed) {
    const submitBtn = document.createElement("button");
    submitBtn.id = createRoomButtonId;
    submitBtn.textContent = "🤝 Open in a Falinks Teambuilder room";
    submitBtn.type = "button";
    submitBtn.onclick = ()=>{
        window.open(falinksRoomEndpoint(packed));
    };
    // pokepast.es potentially erased all css, including button
    Object.assign(submitBtn.style, {
        outline: "none",
        cursor: "pointer",
        textAlign: "center",
        textDecoration: "none",
        boxShadow: "0 1px 2px rgba(0,0,0,.2),inset 0 -1px 2px #fff",
        borderRadius: "5px",
        fontFamily: "Verdana,Helvetica,Arial,sans-serif",
        display: "inline-block",
        color: "#222",
        textShadow: "0 1px 0 #fff",
        border: "1px solid #aaa",
        background: "linear-gradient(to bottom,#f6f6f6,#e3e3e3)",
        fontSize: "9pt",
        padding: "3px 8px",
        marginLeft: "6px",
        marginBottom: "3px"
    });
    return submitBtn;
}
function getPasteAtPokepaste() {
    return [
        1,
        2,
        3,
        4,
        5,
        6
    ].map((i)=>{
        var ref;
        return ((ref = document.querySelector(`body > article:nth-child(${i}) > pre`)) === null || ref === void 0 ? void 0 : ref.textContent) ?? '';
    }).join('');
}
function getPackedAtShowdown() {
    var ref, ref1;
    // @ts-ignore
    return ((ref = unsafeWindow.room) === null || ref === void 0 ? void 0 : (ref1 = ref.curTeam) === null || ref1 === void 0 ? void 0 : ref1.team) ?? '';
}
function main() {
    const { host  } = window.location;
    if (host === pokepasteURL) {
        const paste = getPasteAtPokepaste();
        const packed = pasteToPacked(paste);
        const btn = createRoomButton(packed);
        document.querySelector("body > aside").append(btn);
    } else {
        const observer = new MutationObserver(function() {
            const pokepasteForm = document.getElementById("pokepasteForm");
            if (pokepasteForm && !document.getElementById(createRoomButtonId)) {
                const packed = getPackedAtShowdown();
                const btn = createRoomButton(packed);
                pokepasteForm.appendChild(btn);
            }
        });
        observer.observe(document.querySelector("body"), {
            subtree: true,
            childList: true
        });
    }
}
main();

/******/ })()
;