My Mouse Gestures

A simple mouse gesture script

目前为 2014-10-17 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name My Mouse Gestures
  3. // @description A simple mouse gesture script
  4. // @version 0.1.1
  5. // @include *
  6. // @run-at document-start
  7. // @grant GM_openInTab
  8. // @noframes
  9. // @namespace https://greasyfork.org/users/4968
  10. // ==/UserScript==
  11.  
  12.  
  13. // --- Settings ---
  14.  
  15. var SENSITIVITY = 3; // 1 ~ 5
  16. var TOLERANCE = 3; // 1 ~ 5
  17.  
  18. var funcs = {
  19. 'DR': function() {
  20. window.top.close();
  21. },
  22. 'U': function() {
  23. window.scrollTo(0, 0);
  24. },
  25. 'D': function() {
  26. window.scrollTo(0, 1073741824);
  27. },
  28. 'L': function() {
  29. window.history.back();
  30. },
  31. 'R': function() {
  32. window.history.forward();
  33. },
  34. 'RU': function() {
  35. GM_openInTab('about:newtab', false);
  36. },
  37. 'UD': function() {
  38. window.location.reload();
  39. }
  40. };
  41.  
  42. // ----------------
  43.  
  44.  
  45. var s = 1 << ((7 - SENSITIVITY) << 1);
  46. var t1 = Math.tan(0.15708 * TOLERANCE),
  47. t2 = 1 / t1;
  48.  
  49. var x, y, path;
  50.  
  51. function tracer(e) {
  52. var cx = e.clientX,
  53. cy = e.clientY,
  54. deltaX = cx - x,
  55. deltaY = cy - y,
  56. slope = Math.abs(deltaY / deltaX),
  57. distance = deltaX * deltaX + deltaY * deltaY,
  58. direction = '';
  59. if (distance > s) {
  60. if (slope > t1) {
  61. if (deltaY > 0) {
  62. direction = 'D';
  63. } else {
  64. direction = 'U';
  65. }
  66. } else if (slope <= t2) {
  67. if (deltaX > 0) {
  68. direction = 'R';
  69. } else {
  70. direction = 'L';
  71. }
  72. }
  73. if (path.slice(-1) != direction) {
  74. path += direction;
  75. }
  76. x = cx;
  77. y = cy;
  78. }
  79. }
  80.  
  81. window.addEventListener('mousedown', function(e) {
  82. if (e.which == 3) {
  83. x = e.clientX;
  84. y = e.clientY;
  85. path = "";
  86. window.addEventListener('mousemove', tracer, false);
  87. }
  88. }, false);
  89.  
  90. window.addEventListener('contextmenu', function(e) {
  91. window.removeEventListener('mousemove', tracer, false);
  92. if (path != "") {
  93. e.preventDefault();
  94. if (funcs.hasOwnProperty(path)) {
  95. funcs[path]();
  96. }
  97. }
  98. }, false);