2v2.io Edit-on-Release

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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;
    }
})();