我的鼠标手势

一个简单的鼠标手势脚本

目前为 2017-01-22 提交的版本。查看 最新版本

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