我的鼠标手势

一个简单的鼠标手势脚本

当前为 2014-11-21 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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.2
// @include            *
// @run-at             document-start
// @grant              GM_openInTab
// @noframes
// @namespace          https://greasyfork.org/users/4968
// ==/UserScript==


// --- Settings ---
var SENSITIVITY = 3; // 1 ~ 5
var TOLERANCE = 3; // 1 ~ 5
var 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();
	}
};
// ----------------

var s = 1 << ((7 - SENSITIVITY) << 1);
var t1 = Math.tan(0.15708 * TOLERANCE), t2 = 1 / t1;

var x, y, path;
var canvas, g;

function createCanvas() {
	canvas = document.createElement('canvas');
	canvas.style.position = 'fixed';
	canvas.style.zIndex = 255;
	canvas.style.top = 0;
	canvas.style.left = 0;
	document.body.appendChild(canvas);
	canvas.width = document.body.clientWidth;
	canvas.height = document.body.clientHeight;
	g = canvas.getContext('2d');
	g.lineWidth = 3;
	g.moveTo(x, y);
}

function tracer(e) {
	var 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.slice(-1) != direction) {
			path += direction;
		}
		x = cx;
		y = cy;
		g.lineTo(x, y);
		g.stroke();
	}
}

window.addEventListener('mousedown', function(e) {
	if (e.which == 3) {
		x = e.clientX;
		y = e.clientY;
		path = '';
		createCanvas();
		canvas.addEventListener('mousemove', tracer, false);
	}
}, false);

window.addEventListener('contextmenu', function(e) {
	if (path != '') {
		e.preventDefault();
		if (funcs.hasOwnProperty(path)) {
			funcs[path]();
		}
	}
}, false);

window.addEventListener('mouseup', function(e) {
	canvas.removeEventListener('mousemove', tracer, false);
	if (e.which == 3) {
		canvas.remove();
	}
}, false);