Created
April 1, 2009 08:04
-
-
Save os0x/88609 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name simple_guestures | |
| // @namespace http://looxu.blogspot.com/ | |
| // @description opera like mouse guesture for google chrome | |
| // @author Gomita / Arc Cosine | |
| // @version 1.1 | |
| // ==/UserScript== | |
| // This code is inspired by Gomita's Code | |
| // Look => http://www.xuldev.org/misc/script/MouseGestures.uc.js | |
| // license http://0-oo.net/pryn/MIT_license.txt (The MIT license) | |
| // based http://github.com/ArcCosine/userscript/tree/master | |
| (function(){ | |
| var DEBUG = true; | |
| const TOLERANCE = 10; | |
| var gmouse = { | |
| _lastX: 0, | |
| _lastY: 0, | |
| _directionChain : "", | |
| _isMousedown : false, | |
| _suppressContext : false, | |
| init : function(){ | |
| window.addEventListener("mousedown", gmouse.handleEvent, false); | |
| window.addEventListener("mousemove", gmouse.handleEvent, false); | |
| window.addEventListener("mouseup", gmouse.handleEvent, false); | |
| document.addEventListener("contextmenu", gmouse.handleEvent, false); | |
| }, | |
| handleEvent : function(e){ | |
| switch(e.type){ | |
| case "mousedown": | |
| if( e.button == 2){ | |
| gmouse._isMousedown = true; | |
| gmouse._startGuesture(e); | |
| } | |
| break; | |
| case "mousemove": | |
| if( gmouse._isMousedown ){ | |
| gmouse._progressGesture(e); | |
| } | |
| break; | |
| case "mouseup": | |
| if( gmouse._isMousedown ){ | |
| gmouse._isMousedown = false; | |
| gmouse._suppressContext = !gmouse._directionChain; | |
| } | |
| break; | |
| case "contextmenu": | |
| if( gmouse._directionChain.length > 0 ){ //if not mouse guesture | |
| e.preventDefault(); | |
| e.stopPropagation(); | |
| gmouse._stopGuesture(e); | |
| }; | |
| break; | |
| } | |
| }, | |
| _startGuesture : function(e){ | |
| gmouse._lastX = e.screenX; | |
| gmouse._lastY = e.screenY; | |
| gmouse._directionChain = ""; | |
| //add canvas | |
| var canvas = gmouse.canvas = document.createElement("canvas"); | |
| canvas.style.position = "fixed"; | |
| canvas.addEventListener('click',function(e){ | |
| document.body.removeChild(canvas); | |
| },false); | |
| gmouse.ctx = canvas.getContext("2d"); | |
| document.body.appendChild(canvas); | |
| canvas.style.left = 0 + "px"; | |
| canvas.style.top = 0 + "px"; | |
| canvas.width = Math.max(document.documentElement.clientWidth , document.body.clientWidth); | |
| canvas.height = Math.max(document.documentElement.clientHeight, document.body.clientHeight); | |
| gmouse.ctx.strokeStyle = "rgba(18,89,199,1)"; //like delicious link color | |
| gmouse.startX = e.pageX; | |
| gmouse.startY = e.pageY; | |
| }, | |
| _progressGesture : function(e){ | |
| var x = e.screenX; | |
| var y = e.screenY; | |
| var dx = Math.abs(x-gmouse._lastX); | |
| var dy = Math.abs(y-gmouse._lastY); | |
| if( dx < TOLERANCE && dy < TOLERANCE ) return; | |
| var direction; | |
| if( dx > dy ){ | |
| direction = x < gmouse._lastX ? "L" : "R"; | |
| }else{ | |
| direction = y < gmouse._lastY ? "U" : "D"; | |
| } | |
| var lastDirection = gmouse._directionChain.charAt(gmouse._directionChain.length - 1); | |
| if( direction != lastDirection ){ | |
| gmouse._directionChain += direction; | |
| window.status = gmouse._directionChain; | |
| } | |
| gmouse._lastX = x; | |
| gmouse._lastY = y; | |
| gmouse.ctx.moveTo(gmouse.startX, gmouse.startY); | |
| gmouse.startX = e.pageX; | |
| gmouse.startY = e.pageY; | |
| gmouse.ctx.lineTo(gmouse.startX, gmouse.startY); | |
| gmouse.ctx.stroke(); | |
| }, | |
| _stopGuesture : function(e){ | |
| try{ | |
| if( gmouse._directionChain ){ | |
| gmouse._performAction(e); | |
| } | |
| }catch(ex){ | |
| } | |
| gmouse._directionChain = ""; | |
| if (gmouse.canvas.parentNode) | |
| document.body.removeChild(gmouse.canvas); | |
| }, | |
| _performAction : function(e){ | |
| switch(gmouse._directionChain){ | |
| case "L" : history.back(); break; | |
| case "R" : history.forward(); break; | |
| case "D" : gmouse.open_tab(); break; | |
| case "DR" : gmouse.close_tab(); break; | |
| case "UD" : location.reload(); break; | |
| } | |
| }, | |
| open_tab : function(){ | |
| if (typeof GM_openInTab === 'function') { | |
| GM_openInTab('about:blank'); | |
| } else { | |
| window.open(); | |
| } | |
| }, | |
| close_tab : function(){// Firefox3 not work | |
| window.opener = window; | |
| var win = window.open(location.href, "_self"); | |
| win.close(); | |
| } | |
| }; | |
| gmouse.init(); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment