2v2.io Edit-on-Release

Automatically confirms edits on release in 2v2.io, supports right mouse button

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         2v2.io Edit-on-Release
// @description  Automatically confirms edits on release in 2v2.io, supports right mouse button
// @author       myrrr
// @match        *://*2v2.io/*
// @grant        none
// @version 0.0.1.20250804150231
// @namespace https://greasyfork.org/users/1330310
// ==/UserScript==

(function () {
    'use strict';

    /**********************
     * Set your edit key here
     **********************/
    const KEY_EDIT = 'e'; // 'e' for keyboard, 'rightmouse' for right-click

    /**********************
     * Don't touch anything below
     **********************/
    let editing = false;

    const pressKey = (key) => {
        const code = "Key" + key.toUpperCase();
        const keyCode = key.toUpperCase().charCodeAt(0);

        ["keydown", "keyup"].forEach(type => {
            const evt = new KeyboardEvent(type, {
                key: key,
                code,
                keyCode,
                which: keyCode,
                bubbles: true
            });
            window.dispatchEvent(evt);
        });
    };

    function simulateRightClick(target) {
        const canvas = target || document.querySelector('canvas');
        if (!canvas) return;

        const rect = canvas.getBoundingClientRect();
        const x = rect.left + rect.width / 2;
        const y = rect.top + rect.height / 2;

        const optionsDown = {
            bubbles: true,
            cancelable: true,
            pointerId: 1,
            pointerType: "mouse",
            isPrimary: true,
            button: 2,
            buttons: 2,
            clientX: x,
            clientY: y
        };

        const optionsUp = {
            ...optionsDown,
            buttons: 0
        };

        ["pointerdown", "mousedown"].forEach(type => {
            canvas.dispatchEvent(new PointerEvent(type, optionsDown));
        });

        ["pointerup", "mouseup", "click", "contextmenu"].forEach(type => {
            canvas.dispatchEvent(new PointerEvent(type, optionsUp));
        });
    }

    document.addEventListener("keydown", (e) => {
        if (KEY_EDIT !== 'rightmouse' && e.key.toLowerCase() === KEY_EDIT) {
            editing = true;
        }
    });

    document.addEventListener("mousedown", (e) => {
        if (KEY_EDIT === 'rightmouse' && e.button === 2) {
            editing = true;
        }
        if (editing && e.button === 0) {
            document.addEventListener("mouseup", handleRelease, { once: true });
        }
    });

    function handleRelease() {
        if (KEY_EDIT === 'rightmouse') {
            simulateRightClick();
        } else {
            pressKey(KEY_EDIT);
        }
        editing = false;
    }
})();