Greasy Fork

Greasy Fork is available in English.

Cool rate

Adds the ability to give ratings with any decimal place

当前为 2024-03-30 提交的版本,查看 最新版本

// ==UserScript==
// @name         Cool rate
// @namespace    http://tampermonkey.net/
// @version      2024-03-30
// @description  Adds the ability to give ratings with any decimal place
// @author       SuperG
// @match        https://scpfoundation.net/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=scpfoundation.net
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    let interceptedRequest = null;

    const originalFetch = window.fetch;

    window.fetch = function(input, init) {
        if (init && init.method && init.method.toUpperCase() === 'POST') {
            const requestData = JSON.parse(init.body);

            if (requestData.method === "rate") {
                interceptedRequest = { input, requestData };

                showTextField();

                return new Promise(() => {});
            }
        }

        return originalFetch.apply(this, arguments);
    };

    function showTextField() {
        const inputField = document.createElement('input');
        inputField.type = 'text';
        inputField.placeholder = 'Сколько десятых добавить?';
        inputField.style.cssText = `position:fixed;top:50%;left:50%;transform:translate(-50%, -50%);height:30px;width:200px;padding-top:5px;text-align:center;z-index:99999;`;

        document.body.appendChild(inputField);

        inputField.addEventListener('keydown', (event) => {
            if (event.key === 'Enter') {
                const newValue = parseFloat(inputField.value);
                if (!isNaN(newValue)) {
                    interceptedRequest.requestData.params.value += newValue/10;
                    console.log("Modified request data:", interceptedRequest.requestData);

                    sendModifiedRequest(interceptedRequest);
                } else {
                    console.log("Please enter a valid number.");
                }

                inputField.parentNode.removeChild(inputField);
            }
        });
    }

    function sendModifiedRequest(interceptedRequest) {
        originalFetch(interceptedRequest.input, {
            method: 'POST',
            body: JSON.stringify(interceptedRequest.requestData),
            headers: {
                'Content-Type': 'application/json'
            }
        })
        .then(response => {
            console.log('Response:', response);
        })
        .catch(error => {
            console.error('Error:', error);
        });
    }
})();