Greasy Fork

来自缓存

Greasy Fork is available in English.

触屏转鼠标事件

将触摸事件转换为鼠标事件

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         触屏转鼠标事件
// @version      1.0
// @description  将触摸事件转换为鼠标事件
// @author       ChatGPT
// @match        *://*/*
// @grant        none
// @run-at       document-end
// @namespace http://greasyfork.icu/users/452911
// ==/UserScript==

(function() {
    'use strict';

    // 创建鼠标事件
    function createMouseEvent(type, touchEvent) {
        return new MouseEvent(type, {
            bubbles: true, // 事件是否应该冒泡
            cancelable: true, // 事件是否可以被取消
            view: window, // 事件的视图
            clientX: touchEvent.clientX, // 触摸点的水平坐标
            clientY: touchEvent.clientY // 触摸点的垂直坐标
        });
    }

    // 触摸事件转换为鼠标事件
    function touchToMouse(e) {
        const touches = e.changedTouches;
        for (let i = 0; i < touches.length; i++) {
            const touch = touches[i];
            const mouseEvent = createMouseEvent(
                e.type === 'touchstart' ? 'mousedown' :
                e.type === 'touchend' ? 'mouseup' :
                'mousemove', touch
            );
            touch.target.dispatchEvent(mouseEvent);
        }
    }

    // 监听触摸事件并转换为鼠标事件
    document.addEventListener('touchstart', touchToMouse, false);
    document.addEventListener('touchend', touchToMouse, false);
    document.addEventListener('touchmove', touchToMouse, false);
})();