Greasemonkey Mouse Gestures

Just a Mouse Gestures script

  1. // ==UserScript==
  2. // @name Greasemonkey Mouse Gestures
  3. // @name:zh-CN 油猴鼠标手势
  4. // @name:zh-TW 油猴滑鼠手勢
  5. // @namespace hoothin
  6. // @version 0.70
  7. // @description Just a Mouse Gestures script
  8. // @description:zh-CN 就是个鼠标手势脚本
  9. // @description:zh-TW 就是個滑鼠手勢脚本
  10. // @author hoothin
  11. // @include *
  12. // @grant GM_openInTab
  13. // @grant GM_setValue
  14. // @grant GM_getValue
  15. // @grant GM_registerMenuCommand
  16. // @grant unsafeWindow
  17. // @grant GM_info
  18. // @license MIT License
  19. // @compatible chrome
  20. // @compatible firefox
  21. // ==/UserScript==
  22.  
  23. var lastX,lastY,lastSign,afterGestures,gesturesWords,gesturesContent,gestures,signs;
  24. const defaultFun={
  25. close:()=>{unsafeWindow.opener=null;unsafeWindow.open('', '_self', '');unsafeWindow.close();},
  26. openNew:()=>{GM_openInTab('about:newtab', false)},
  27. scrollToTop:()=>{unsafeWindow.scrollTo(0, 0)},
  28. scrollToBottom:()=>{unsafeWindow.scrollTo(0, 1073741824)},
  29. back:()=>{unsafeWindow.history.back()},
  30. forward:()=>{unsafeWindow.history.forward()},
  31. reload:()=>{unsafeWindow.location.reload()}
  32. };
  33. function initEventListener(start,move,end,tracer,clientX,clientY,startBool){
  34. var isMouse=start=="mousedown";
  35. var moveFun=function(e){
  36. tracer(clientX(e),clientY(e),isMouse);
  37. gesturesWords.innerHTML=signs;
  38. var gesturesWidth=signs.length*51+40;
  39. gesturesContent.style.width=gesturesWidth+"px";
  40. gesturesContent.style.marginLeft=-gesturesWidth/2+"px";
  41. };
  42. document.addEventListener(start, function(e) {
  43. if(!startBool || startBool(e)){
  44. lastX=clientX(e);
  45. lastY=clientY(e);
  46. lastSign=signs="";
  47. document.addEventListener(move, moveFun, false);
  48. }
  49. }, false);
  50. var endFun=function(e) {
  51. document.removeEventListener(move, moveFun, false);
  52. setTimeout(function(){if(gesturesContent.parentNode)gesturesContent.parentNode.removeChild(gesturesContent);},500);
  53. if(signs){
  54. if(afterGestures)afterGestures();
  55. for(var g of gestures){
  56. var gSign=g.gesture;
  57. if(signs==gSign){
  58. if(!isMouse)document.body.appendChild(gesturesContent);
  59. var fun=defaultFun[g.fun];
  60. if(fun===undefined || !fun){
  61. eval(g.fun);
  62. }else {
  63. fun();
  64. }
  65. e.stopPropagation();
  66. e.preventDefault();
  67. e.returnValue=false;
  68. e.cancelBubble=true;
  69. return false;
  70. }
  71. }
  72. signs="";
  73. }
  74. };
  75. document.addEventListener(end, endFun, false);
  76. document.addEventListener("mouseup", endFun, false);
  77. }
  78. (function() {
  79. 'use strict';
  80. var i18n;
  81. var lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
  82. const minLength=256,tg=0.5;
  83. switch (lang){
  84. case "zh-CN":
  85. i18n={
  86. close:"关闭页面",
  87. openNew:"新建标签页",
  88. scrollToTop:"滚动至最上",
  89. scrollToBottom:"滚动至最下",
  90. back:"后退",
  91. forward:"前进",
  92. reload:"刷新",
  93. addListener:"点击监听鼠标手势",
  94. listening:"正在监听鼠标手势",
  95. bind:"绑定功能",
  96. custom:"自定义代码",
  97. ok:"确定",
  98. del:"删除",
  99. saved:"已设定的手势",
  100. alert1:"请先监听手势",
  101. alert2:"还没有绑定功能",
  102. alert3:"请输入自定义代码",
  103. configure:"鼠标手势设置",
  104. update:"鼠标手势脚本已更新,是否覆盖脚本设置?"
  105. };
  106. break;
  107. default:
  108. i18n={
  109. close:"Close tab",
  110. openNew:"Open new tab",
  111. scrollToTop:"Scroll to top",
  112. scrollToBottom:"Scroll to bottom",
  113. back:"Back",
  114. forward:"Forward",
  115. reload:"Reload",
  116. addListener:"Click to add gesture listener",
  117. listening:"Listening",
  118. bind:"Bind function",
  119. custom:"Custom code",
  120. ok:"Ok",
  121. del:"Delete",
  122. saved:"Saved gestures",
  123. alert1:"Please add gesture first",
  124. alert2:"Nothing bind",
  125. alert3:"Input custom code please",
  126. configure:"Mouse Gestures - Configure",
  127. update:"Greasemonkey Mouse Gestures has updated, recover config?"
  128. };
  129. break;
  130. }
  131. gestures=GM_getValue("gestures");
  132. if(GM_info.script.version != GM_getValue("gmMouseGestureVersion")){
  133. if(!gestures || window.confirm(i18n.update))
  134. gestures=[{gesture:"↓→",fun:"close"},
  135. {gesture:"→↑",fun:"openNew"},
  136. {gesture:"←↑",fun:"scrollToTop"},
  137. {gesture:"←↓",fun:"scrollToBottom"},
  138. {gesture:"→↑←",fun:"back"},
  139. {gesture:"←↑→",fun:"forward"},
  140. {gesture:"↑↓",fun:"reload"},
  141. {gesture:"↓↑↓",fun:"var t=((unsafeWindow.getSelection&&unsafeWindow.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));var e=(document.charset||document.characterSet);if(t!=''){GM_openInTab('http://translate.google.cn/?text='+t+'&hl=zh-CN&langpair=auto|zh-CN&tbb=1&ie='+e,false);}else{GM_openInTab('http://translate.google.cn/translate?u='+encodeURIComponent(location.href)+'&hl=zh-CN&langpair=auto|zh-CN&tbb=1&ie='+e,false);}"},
  142. {gesture:"↓↑↓←",fun:'var d=document,b=d.body;with(b.onselectstart=b.oncopy=b.onpaste=b.onkeydown=b.oncontextmenu=b.onmousemove=b.ondragstart=d.oncopy=d.onpaste=null,d.onselectstart=d.oncontextmenu=d.onmousedown=d.onkeydown=function(){return!0},d.wrappedJSObject||d)onmouseup=null,onmousedown=null,oncontextmenu=null;for(var a=d.getElementsByTagName("*"),i=a.length-1;i>=0;i--){var o=a[i];with(o.wrappedJSObject||o)onmouseup=null,onmousedown=null}var h=d.getElementsByTagName("head")[0];if(h){var s=d.createElement("style");s.innerHTML="html,*{user-select:text!important;-moz-user-select:text!important;-webkit-user-select:text!important;-webkit-user-drag:text!important;-khtml-user-select:text!important;-khtml-user-drag:text!important;pointer-events:auto!important;}",h.appendChild(s)}unsafeWindow.Event.prototype.preventDefault=function(){};'},
  143. {gesture:"↓↑↓↑",fun:"var d = document, e = d.getElementById('wappalyzer-container') ; if ( e !== null ) { d.body.removeChild(e); } var u = 'https://wappalyzer.com/', t = new Date().getTime(), c = d.createElement('div'), p = d.createElement('div'), l = d.createElement('link'), s = d.createElement('script') ; c.setAttribute('id', 'wappalyzer-container'); l.setAttribute('rel', 'stylesheet'); l.setAttribute('href', u + 'css/bookmarklet.css'); d.head.appendChild(l); p.setAttribute('id', 'wappalyzer-pending'); p.setAttribute('style', 'background-image: url(' + u + 'images/spinner.gif) !important'); c.appendChild(p); s.setAttribute('src', u + 'bookmarklet/wappalyzer.js'); s.onload = function() { s = d.createElement('script'); s.setAttribute('src', u + 'bookmarklet/apps.js'); s.onload = function() { s = d.createElement('script'); s.setAttribute('src', u + 'bookmarklet/driver.js'); c.appendChild(s); }; c.appendChild(s); }; c.appendChild(s); d.body.appendChild(c);"}
  144. ];
  145. GM_setValue("gestures",gestures);
  146. GM_setValue("gmMouseGestureVersion",GM_info.script.version);
  147. }
  148. function tracer(curX,curY,showSign) {
  149. let distanceX=curX-lastX,distanceY=curY-lastY;
  150. let distance=distanceX*distanceX+distanceY*distanceY;
  151. if (distance>minLength) {
  152. lastX=curX;
  153. lastY=curY;
  154. let direction="";
  155. let slope=Math.abs(distanceY/distanceX);
  156. if(slope>tg){
  157. if(distanceY>0) {
  158. direction="↓";
  159. }else{
  160. direction="↑";
  161. }
  162. }else if(slope<=1/tg) {
  163. if(distanceX>0) {
  164. direction="→";
  165. }else{
  166. direction="←";
  167. }
  168. }
  169. if(lastSign!=direction) {
  170. signs+=direction;
  171. lastSign=direction;
  172. if(showSign)document.body.appendChild(gesturesContent);
  173. }
  174. }
  175. }
  176. gesturesContent=document.createElement("div");
  177. gesturesContent.id="gesturesContent";
  178. gesturesContent.style.cssText="width:300px;height:70px;position:fixed;left:50%;top:50%;margin-top:-25px;margin-left:-150px;z-index:999999999;background-color:#000;border:1px solid;border-radius:10px;opacity:0.65;filter:alpha(opacity=65);box-shadow:5px 5px 20px 0px #000;";
  179. gesturesContent.innerHTML='<div id="gesturesWords" style="position:absolute;left:20px;top:5px;font:bold 50px \'黑体\';color:#ffffff"></div>';
  180. gesturesWords=gesturesContent.querySelector("#gesturesWords");
  181. initEventListener("touchstart","touchmove","touchend",tracer,e=>{return e.changedTouches[0].clientX},e=>{return e.changedTouches[0].clientY});
  182. initEventListener("mousedown","mousemove","contextmenu",tracer,e=>{return e.clientX},e=>{return e.clientY},e=>{return e.which === 3});
  183. if(location.href=="https://github.com/hoothin/UserScripts/tree/master/Mouse%20Gestures"){
  184. var entryContent=document.querySelector("article.entry-content"),mobile=false;
  185. if(!entryContent){
  186. mobile=true;
  187. entryContent=document.querySelector(".files-list");
  188. }
  189. var configBody=document.createElement("div");
  190. configBody.id="configBody";
  191. configBody.style.cssText="opacity:0.65;filter:alpha(opacity=65);box-shadow:5px 5px 20px 0px #000;height:160px";
  192. configBody.innerHTML='<div id="configContent" style="position:relative;left:20px;top:5px;"></div>';
  193. var configContent=configBody.querySelector("#configContent");
  194. var newOrEdit=document.createElement("div");
  195. configContent.appendChild(newOrEdit);
  196. var newOrEditSign=document.createElement("div");
  197. newOrEditSign.innerHTML="";
  198. newOrEditSign.style.float="left";
  199. newOrEdit.appendChild(newOrEditSign);
  200. var newOrEditBtn=document.createElement("input");
  201. newOrEditBtn.type="button";
  202. newOrEditBtn.value=i18n.addListener;
  203. newOrEdit.appendChild(newOrEditBtn);
  204. var functionArea=document.createElement("p");
  205. newOrEdit.appendChild(functionArea);
  206. var functionSelect=document.createElement("select");
  207. functionSelect.innerHTML="<option>"+i18n.bind+"</option><option value='custom'>"+i18n.custom+"</option>";
  208. for(var key in defaultFun){
  209. var optionStr="<option value='"+key+"'>"+i18n[key]+"</option>";
  210. functionSelect.innerHTML+=optionStr;
  211. }
  212. functionArea.appendChild(functionSelect);
  213. var newOrEditEval=document.createElement("input");
  214. newOrEditEval.style.width=(mobile?"230":"750")+"px";
  215. newOrEditEval.style.display="none";
  216. functionArea.appendChild(newOrEditEval);
  217. var newOrEditOkBtn=document.createElement("input");
  218. newOrEditOkBtn.type="button";
  219. newOrEditOkBtn.value=i18n.ok;
  220. newOrEdit.appendChild(newOrEditOkBtn);
  221. var gesturesHas=document.createElement("p");
  222. newOrEdit.appendChild(gesturesHas);
  223. var gesturesSelect=document.createElement("select");
  224. var refreshGestures=function(){
  225. gesturesSelect.innerHTML="<option>"+i18n.saved+"</option>";
  226. gestures.forEach(function(item){
  227. var optionStr="<option value='"+item.gesture+"'>"+item.gesture+"</option>";
  228. gesturesSelect.innerHTML+=optionStr;
  229. });
  230. };
  231. refreshGestures();
  232. gesturesHas.appendChild(gesturesSelect);
  233. var newOrEditDelBtn=document.createElement("input");
  234. newOrEditDelBtn.type="button";
  235. newOrEditDelBtn.value=i18n.del;
  236. gesturesHas.appendChild(newOrEditDelBtn);
  237. entryContent.insertBefore(configBody,entryContent.firstChild);
  238. functionSelect.onchange=function(){
  239. if(functionSelect.options.selectedIndex===0)return;
  240. var value=functionSelect.options[functionSelect.options.selectedIndex].value;
  241. newOrEditEval.style.display=(value=="custom"?"initial":"none");
  242. };
  243. gesturesSelect.onchange=function(){
  244. if(gesturesSelect.options.selectedIndex===0)return;
  245. var value=gesturesSelect.options[gesturesSelect.options.selectedIndex].value;
  246. newOrEditSign.innerHTML=value;
  247. gestures.every(function(item){
  248. if(item.gesture==value){
  249. functionSelect.options[1].selected = true;
  250. for (var i=2;i<functionSelect.options.length;i++) {
  251. if (functionSelect.options[i].value == item.fun) {
  252. functionSelect.options[i].selected = true;
  253. break;
  254. }
  255. }
  256. functionSelect.onchange();
  257. newOrEditEval.value=item.fun;
  258. return false;
  259. }
  260. return true;
  261. });
  262. };
  263. newOrEditBtn.onclick=function(e){
  264. newOrEditBtn.value=i18n.listening;
  265. newOrEditBtn.setAttribute("disabled","disabled");
  266. afterGestures=function(){
  267. newOrEditBtn.removeAttribute("disabled");
  268. afterGestures=null;
  269. newOrEditSign.innerHTML=signs;
  270. newOrEditBtn.value=i18n.addListener;
  271. };
  272. };
  273. newOrEditOkBtn.onclick=function(e){
  274. if(!newOrEditSign.innerHTML){
  275. alert(i18n.alert1);
  276. }else if(functionSelect.options.selectedIndex===0){
  277. alert(i18n.alert2);
  278. }else {
  279. var op=functionSelect.options;
  280. var selIndex=op.selectedIndex;
  281. if(selIndex===1 && !newOrEditEval.value){
  282. alert(i18n.alert3);
  283. }else{
  284. var code=op[selIndex].value;
  285. if(selIndex===1){
  286. code=newOrEditEval.value;
  287. }
  288. for(var index in gestures){
  289. if(gestures[index].gesture==newOrEditSign.innerHTML){
  290. gestures.splice(index,1);
  291. break;
  292. }
  293. }
  294. gestures.push({gesture:newOrEditSign.innerHTML,fun:code});
  295. GM_setValue("gestures",gestures);
  296. refreshGestures();
  297. }
  298. }
  299. };
  300. newOrEditDelBtn.onclick=function(e){
  301. var value=gesturesSelect.options[gesturesSelect.options.selectedIndex].value;
  302. for(var index in gestures){
  303. if(gestures[index].gesture==value){
  304. gestures.splice(index,1);
  305. GM_setValue("gestures",gestures);
  306. break;
  307. }
  308. }
  309. refreshGestures();
  310. };
  311. }
  312. function goSetting(){
  313. GM_openInTab("https://github.com/hoothin/UserScripts/tree/master/Mouse%20Gestures", false);
  314. }
  315. GM_registerMenuCommand(i18n.configure, goSetting);
  316. })();