我的滑鼠手勢

一個簡單的滑鼠手勢腳本

目前為 2023-08-17 提交的版本,檢視 最新版本

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