Add Extra (Play/AddToMapRotation) Buttons in mod.io for Pavlov maps/mods with RCON integration

Add a extra buttons

2023-06-17 기준 버전입니다. 최신 버전을 확인하세요.

// ==UserScript==
// @name         Add Extra (Play/AddToMapRotation) Buttons in mod.io for Pavlov maps/mods with RCON integration
// @namespace    https://greasyfork.org/en/users/1103172-underpl
// @version      0.1
// @description  Add a extra buttons
// @author       UnderPL
// @match        https://mod.io/g/pavlov/m/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    var createButton = function(id, color, text) {
        var subscribeButton = document.querySelector('button.tw-button-transition.tw-outline-none.tw-shrink-0.tw-items-center.tw-justify-center.tw-space-x-2.tw-font-bold.tw-bg-theme-1--hover.tw-text-md.tw-leading-normal.tw-global--border-radius.tw-border-2.tw-cursor-pointer.tw-input--height-large.tw-w-full.tw-border-primary[id^="input"]');
        var newButton = subscribeButton.cloneNode(true);
        newButton.id = id;
        newButton.querySelector('span div span').textContent = text;
        newButton.style.borderColor = color;
        return newButton;
    };

    var addExtraButtons = function() {
        var subscribeButton = document.querySelector('button.tw-button-transition.tw-outline-none.tw-shrink-0.tw-items-center.tw-justify-center.tw-space-x-2.tw-font-bold.tw-bg-theme-1--hover.tw-text-md.tw-leading-normal.tw-global--border-radius.tw-border-2.tw-cursor-pointer.tw-input--height-large.tw-w-full.tw-border-primary[id^="input"]');
        var playButton = document.getElementById('playButton');
        var addToMapRotationButton = document.getElementById('addToMapRotationButton');

        if (!subscribeButton || playButton || addToMapRotationButton) {
            return;
        }

        playButton = createButton("playButton", "green", "Play");
        addToMapRotationButton = createButton("addToMapRotationButton", "blue", "Add to map rotation");

        var selectElement = document.createElement('select');
        selectElement.id = "data";
        selectElement.classList.add("form-select");
        selectElement.style.color = 'black';
        selectElement.style.fontSize = '150%';
        selectElement.innerHTML = `
        <option value="SND">SND</option>
        <option value="TDM">TDM</option>
        <option value="DM">DM</option>
        <option value="GUN">GUN</option>
        <option value="ZWV">ZWV</option>
        <option value="WW2GUN">WW2GUN</option>
        <option value="TANKTDM">TANKTDM</option>
        <option value="KOTH">KOTH</option>
        <option value="TTT">TTT</option>
        <option value="OITC">OITC</option>
        <option value="INFECTION" selected="selected">INFECTION</option>
        <option value="HIDE">HIDE</option>
        <option value="PUSH">PUSH</option>
        <option value="PH">PH</option>
        <option value="CUSTOM">CUSTOM</option>`;
        selectElement.style.display = 'block';
        selectElement.style.margin = '0 auto';

        subscribeButton.parentNode.insertBefore(selectElement, subscribeButton);
        subscribeButton.parentNode.insertBefore(document.createElement('br'), subscribeButton);
        subscribeButton.parentNode.insertBefore(playButton, subscribeButton);
        subscribeButton.parentNode.insertBefore(addToMapRotationButton, playButton.nextElementSibling);

        playButton.addEventListener('click', function(e) {
            e.stopPropagation();
            var mapId = document.querySelector('div.tw-flex:nth-child(5) > span:nth-child(2) > span:nth-child(1)').textContent;
            var selectedData = document.getElementById('data').value;

            var body = JSON.stringify({
                data: selectedData,
                "Map Name": "UGC" + mapId,
                uid: null
            });

            GM_xmlhttpRequest({
                method: "POST",
                url: "https://pavlovrcon.com/api/switch_map/0",
                headers: {
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0",
                    "Accept": "*/*",
                    "Accept-Language": "en-US,en;q=0.5",
                    "Content-Type": "application/json"
                },
                data: body,
                onload: function(response) {
                    console.log('GM_xmlhttpRequest response:', response);
                }
            });
        });

        addToMapRotationButton.addEventListener('click', function(e) {
            e.stopPropagation();
            var mapId = document.querySelector('div.tw-flex:nth-child(5) > span:nth-child(2) > span:nth-child(1)').textContent;
            var selectedData = document.getElementById('data').value;

            var body = JSON.stringify({
                data: selectedData,
                "Map Name": "UGC" + mapId,
                uid: null
            });

            GM_xmlhttpRequest({
                method: "POST",
                url: "https://pavlovrcon.com/api/addmaprotation/0",
                headers: {
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0",
                    "Accept": "*/*",
                    "Accept-Language": "en-US,en;q=0.5",
                    "Content-Type": "application/json",
                    "Sec-Fetch-Dest": "empty",
                    "Sec-Fetch-Mode": "cors",
                    "Sec-Fetch-Site": "same-origin"
                },
                data: body,
                onload: function(response) {
                    console.log('GM_xmlhttpRequest response:', response);
                }
            });
        });
    };

    var observer = new MutationObserver(function(mutationsList) {
        for (var mutation of mutationsList) {
            if (mutation.type === 'childList') {
                addExtraButtons();
                break;
            }
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });

})();