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

Per 17-06-2023. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name         Pavlov mod.io maps integration with pavlovrcon.com
// @namespace    https://greasyfork.org/en/users/1103172-underpl
// @version      0.4
// @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
// ==/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;

        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");
        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 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 });

})();