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

Add a extra buttons

目前為 2023-06-17 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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 });

})();