我的鼠标手势

一个简单的鼠标手势脚本

目前为 2014-11-21 提交的版本。查看 最新版本

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