Wplace Touch Util

wplace 모바일 환경에서 편하게 사용하기 위한 유틸

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==

// @name         Wplace Touch Util
// @namespace    http://tampermonkey.net/
// @version      1.1.2
// @description  wplace 모바일 환경에서 편하게 사용하기 위한 유틸
// @author       mome0320
// @match        *://*.wplace.live/*
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let pressTimer = null;
    let pressTarget = null;
    const pressDuration = 500; // 길게 누르는 기준 시간 (밀리초)
    const multiClickDuration = 1000; // 더블클릭 인식 기준 시간 (밀리초)
    const multiClickRange = 20; // 더블 클릭 유효범위
    let lastPos = {x: -1, y: -1}
    let lastTouchTime = 0;
    let touchCount = 0;

    function calcDistance(pos1,pos2){
    let x = pos1.x - pos2.x
    let y = pos1.y - pos2.y
    return Math.sqrt(x**2+y**2)
    }

    function startPressTimer(e) {
        if(e.touches.length !== 1) return;
        pressTimer = setTimeout(() => {
            handleLongPress(e);
        }, pressDuration);
       let tCount = detectMultipleClick(e)
       if(tCount == 3){
            handleTriplePress(e)
            resetMultipleClick()
        }
    }
    function resetMultipleClick(){
        touchCount = 0
        lastTouchTime = 0
        }
    function createFirstClick(pos){
      touchCount = 1
      lastPos = pos
      lastTouchTime = Date.now()
    }
    function detectMultipleClick(e){
     const pos = {x: e.touches[0].clientX,y: e.touches[0].clientY}
     const delay = Date.now() - lastTouchTime
     if(touchCount === 0 || delay > multiClickDuration){
         createFirstClick(pos);
         return touchCount;
     }
     const distence = calcDistance(lastPos,pos);
     console.log("distance",distence)
     if(distence <= multiClickRange){
      touchCount++
      return touchCount
     }else{
        createFirstClick(pos)
        return touchCount
     }
    }

    function cancelPressTimer(e) {
        if (pressTimer) {
            clearTimeout(pressTimer);
            pressTimer = null;
        }
    }

    function handleLongPress(e) {
        let path = null;
        path = document.querySelector(".btn.btn-square.not-touchscreen\\:hidden");
        if (!path||path.title == "Unlock") return;
        path.click();
    }

    function handleTriplePress(e){
    document.querySelector(".tooltip.ml-auto > button").click();
    }

    document.addEventListener('touchstart', startPressTimer);
    document.addEventListener('touchend', cancelPressTimer);
    document.addEventListener('touchcancel', cancelPressTimer);
    document.addEventListener('touchmove', (e) => {
        if (e.touches.length !== 1) resetMultipleClick()
        cancelPressTimer(e)
        });
})();