Extra mod.io (Play/AddToMapRotation) Buttons for Pavlov maps with pavlovr.com integration

Add a extra buttons

当前为 2023-06-17 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Extra mod.io (Play/AddToMapRotation) Buttons for Pavlov maps with pavlovr.com 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 });

})();