// ==UserScript== // @name My Mouse Gestures // @name:zh-CN 我的鼠标手势 // @name:zh-TW 我的滑鼠手勢 // @description A simple mouse gesture script // @description:zh-CN 一个简单的鼠标手势脚本 // @description:zh-TW 一個簡單的滑鼠手勢腳本 // @version 0.1.6 // @include * // @run-at document-start // @grant GM_openInTab // @grant window.close // @namespace https://greasyfork.org/users/4968 // @downloadURL none // ==/UserScript== // --- Settings --- const SENSITIVITY = 3; // 1 ~ 5 const TOLERANCE = 3; // 1 ~ 5 const funcs = { 'DR': function() { window.top.close(); }, 'U': function() { window.scrollTo(0, 0); }, 'D': function() { window.scrollTo(0, 1073741824); }, 'L': function() { window.history.back(); }, 'R': function() { window.history.forward(); }, 'RU': function() { GM_openInTab('about:newtab', false); }, 'UD': function() { window.location.reload(); } }; // ---------------- const s = 1 << ((7 - SENSITIVITY) << 1); const t1 = Math.tan(0.15708 * TOLERANCE), t2 = 1 / t1; let x, y, path; const tracer = function(e) { let cx = e.clientX, cy = e.clientY, deltaX = cx - x, deltaY = cy - y, slope = Math.abs(deltaY / deltaX), distance = deltaX * deltaX + deltaY * deltaY, direction = ''; if (distance > s) { if (slope > t1) { if (deltaY > 0) { direction = 'D'; } else { direction = 'U'; } } else if (slope <= t2) { if (deltaX > 0) { direction = 'R'; } else { direction = 'L'; } } if (path.charAt(path.length - 1) !== direction) { path += direction; } x = cx; y = cy; } }; window.addEventListener('mousedown', function(e) { if (e.which === 3) { x = e.clientX; y = e.clientY; path = ""; window.addEventListener('mousemove', tracer, false); } }, false); window.addEventListener('contextmenu', function(e) { window.removeEventListener('mousemove', tracer, false); if (path !== "") { e.preventDefault(); if (funcs.hasOwnProperty(path)) { funcs[path](); } } }, false);