Pavlov mod.io maps integration with pavlovrcon.com

Add extra buttons to mod.io pavlov map pages to allow switching to the map or adding it to the rotation of a server by using pavlovrcon.com

Verze ze dne 17. 06. 2023. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Pavlov mod.io maps integration with pavlovrcon.com
// @namespace    https://greasyfork.org/en/users/1103172-underpl
// @version      0.6
// @description  Add extra buttons to mod.io pavlov map pages to allow switching to the map or adding it to the rotation of a server by using pavlovrcon.com
// @author       UnderPL
// @match        https://mod.io/g/pavlov/m/*
// @grant        GM_xmlhttpRequest
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
    'use strict';
    var defaultMode = "INFECTION";
    var storedValue = GM_getValue("defaultSelectedMode", defaultMode);
    var defaultSelectedMode = storedValue || defaultMode;
    GM_setValue("defaultSelectedMode", defaultSelectedMode);

    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;

        newButton.addEventListener('mouseover', function() {
            newButton.querySelector('span div span').style.color = color;
        });

        newButton.addEventListener('mouseout', function() {
            newButton.querySelector('span div span').style.color = 'inherit';
        });

        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");
        playButton.style.marginBottom = "10px";
        addToMapRotationButton = createButton("addToMapRotationButton", "blue", "Add to map rotation");
        addToMapRotationButton.style.marginBottom = "10px";

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

        selectElement.addEventListener('change', function() {
            defaultSelectedMode = selectElement.value;
            GM_setValue("defaultSelectedMode", defaultSelectedMode);
        });

        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 mapName = document.querySelector('.tw-util-truncate-two-lines.tw-font-bold').textContent;
            var selectedData = document.getElementById('data').value;

            var confirmationMessage = 'Are you sure you want to add "' + mapName + '" to the map rotation?\n';
            confirmationMessage += 'MapRotation=(MapId="' + mapId + '",GameMode="' + selectedData + '")';

            if (window.confirm(confirmationMessage)) {
                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 });

})();