|
| 1 | +// ==UserScript== |
| 2 | +// @name Greasemonkey Mouse Gestures |
| 3 | +// @name:zh-CN 鼠标手势 |
| 4 | +// @name:zh-TW 滑鼠手勢 |
| 5 | +// @namespace hoothin |
| 6 | +// @version 0.1 |
| 7 | +// @description Just a Mouse Gestures |
| 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 | +// ==/UserScript== |
| 17 | + |
| 18 | +(function() { |
| 19 | + 'use strict'; |
| 20 | + var lastX, lastY, signs, lastSign, gestures, i18n; |
| 21 | + var lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage; |
| 22 | + const minLength=256,tg=0.5, |
| 23 | + defaultFun={ |
| 24 | + close:"window.top.close()", |
| 25 | + openNew:"GM_openInTab('about:newtab', false)", |
| 26 | + scrollToTop:"window.scrollTo(0, 0)", |
| 27 | + scrollToBottom:"window.scrollTo(0, 1073741824)", |
| 28 | + back:"window.history.back()", |
| 29 | + forward:"window.history.forward()", |
| 30 | + reload:"window.location.reload()" |
| 31 | + }; |
| 32 | + switch (lang){ |
| 33 | + case "zh-CN": |
| 34 | + i18n={ |
| 35 | + close:"关闭页面", |
| 36 | + openNew:"新建标签页", |
| 37 | + scrollToTop:"滚动至最上", |
| 38 | + scrollToBottom:"滚动至最下", |
| 39 | + back:"后退", |
| 40 | + forward:"前进", |
| 41 | + reload:"刷新", |
| 42 | + addListener:"点击监听鼠标手势", |
| 43 | + listening:"正在监听鼠标手势", |
| 44 | + bind:"绑定功能", |
| 45 | + custom:"自定义代码", |
| 46 | + ok:"确定", |
| 47 | + del:"删除", |
| 48 | + saved:"已设定的手势", |
| 49 | + alert1:"请先监听手势", |
| 50 | + alert2:"还没有绑定功能", |
| 51 | + alert3:"请输入自定义代码", |
| 52 | + configure:"鼠标手势设置" |
| 53 | + }; |
| 54 | + break; |
| 55 | + default: |
| 56 | + i18n={ |
| 57 | + close:"Close tab", |
| 58 | + openNew:"Open new tab", |
| 59 | + scrollToTop:"Scroll to top", |
| 60 | + scrollToBottom:"Scroll to bottom", |
| 61 | + back:"Back", |
| 62 | + forward:"Forward", |
| 63 | + reload:"Reload", |
| 64 | + addListener:"Click to add gesture listener", |
| 65 | + listening:"Listening", |
| 66 | + bind:"Bind function", |
| 67 | + custom:"Custom code", |
| 68 | + ok:"Ok", |
| 69 | + del:"Delete", |
| 70 | + saved:"Saved gestures", |
| 71 | + alert1:"Please add gesture first", |
| 72 | + alert2:"Nothing bind", |
| 73 | + alert3:"Input custom code please", |
| 74 | + configure:"Mouse Gestures - Configure" |
| 75 | + }; |
| 76 | + break; |
| 77 | + } |
| 78 | + gestures=GM_getValue("gestures"); |
| 79 | + if(!gestures){ |
| 80 | + gestures=[{gesture:"↓→",fun:"close"},{gesture:"→↑",fun:"openNew"}]; |
| 81 | + GM_setValue("gestures",gestures); |
| 82 | + } |
| 83 | + function tracer(curX,curY) { |
| 84 | + let distanceX=curX-lastX,distanceY=curY-lastY; |
| 85 | + let distance=distanceX*distanceX+distanceY*distanceY; |
| 86 | + if (distance>minLength) { |
| 87 | + lastX=curX; |
| 88 | + lastY=curY; |
| 89 | + let direction=""; |
| 90 | + let slope=Math.abs(distanceY/distanceX); |
| 91 | + if(slope>tg){ |
| 92 | + if(distanceY>0) { |
| 93 | + direction="↓"; |
| 94 | + }else{ |
| 95 | + direction="↑"; |
| 96 | + } |
| 97 | + }else if(slope<=1/tg) { |
| 98 | + if(distanceX>0) { |
| 99 | + direction="→"; |
| 100 | + }else{ |
| 101 | + direction="←"; |
| 102 | + } |
| 103 | + } |
| 104 | + if(lastSign!=direction) { |
| 105 | + signs+=direction; |
| 106 | + lastSign=direction; |
| 107 | + document.body.appendChild(gesturesContent); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + function initEventListener(start,move,end,clientX,clientY,startBool){ |
| 112 | + var moveFun=function(e){ |
| 113 | + tracer(eval(clientX),eval(clientY)); |
| 114 | + gesturesWords.innerHTML=signs; |
| 115 | + var gesturesWidth=signs.length*51+40; |
| 116 | + gesturesContent.style.width=gesturesWidth+"px"; |
| 117 | + gesturesContent.style.marginLeft=-gesturesWidth/2+"px"; |
| 118 | + }; |
| 119 | + document.addEventListener(start, function(e) { |
| 120 | + if(!startBool || eval(startBool)){ |
| 121 | + lastX=eval(clientX); |
| 122 | + lastY=eval(clientY); |
| 123 | + lastSign=signs=""; |
| 124 | + document.addEventListener(move, moveFun, false); |
| 125 | + } |
| 126 | + }, false); |
| 127 | + document.addEventListener(end, function(e) { |
| 128 | + document.removeEventListener(move, moveFun, false); |
| 129 | + setTimeout(function(){if(gesturesContent.parentNode)gesturesContent.parentNode.removeChild(gesturesContent);},500); |
| 130 | + if(afterGestures)afterGestures(); |
| 131 | + for(var g of gestures){ |
| 132 | + var gSign=g.gesture; |
| 133 | + if(signs==gSign){ |
| 134 | + var fun=defaultFun[g.fun]; |
| 135 | + if(!fun){ |
| 136 | + eval(g.fun); |
| 137 | + } |
| 138 | + eval(fun); |
| 139 | + e.stopPropagation(); |
| 140 | + e.preventDefault(); |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + }, false); |
| 145 | + } |
| 146 | + initEventListener("touchstart","touchmove","touchend","e.changedTouches[0].clientX","e.changedTouches[0].clientY"); |
| 147 | + initEventListener("mousedown","mousemove","contextmenu","e.clientX","e.clientY","e.which === 3"); |
| 148 | + var afterGestures; |
| 149 | + var gesturesContent=document.createElement("div"); |
| 150 | + gesturesContent.id="gesturesContent"; |
| 151 | + gesturesContent.style.cssText="width:300px;height:70px;position:fixed;left:50%;top:50%;margin-top:-25px;margin-left:-150px;z-index:200000;background-color:#000;border:1px solid;border-radius:10px;opacity:0.65;filter:alpha(opacity=65);box-shadow:5px 5px 20px 0px #000;"; |
| 152 | + gesturesContent.innerHTML='<div id="gesturesWords" style="position:absolute;left:20px;top:5px;font:bold 50px \'黑体\';color:#ffffff"></div>'; |
| 153 | + var gesturesWords=gesturesContent.querySelector("#gesturesWords"); |
| 154 | + if(location.href=="https://github.com/hoothin/UserScripts/tree/master/Mouse%20Gestures"){ |
| 155 | + var entryContent=document.querySelector("article.entry-content"); |
| 156 | + var configBody=document.createElement("div"); |
| 157 | + configBody.id="configBody"; |
| 158 | + configBody.style.cssText="opacity:0.65;filter:alpha(opacity=65);box-shadow:5px 5px 20px 0px #000;height:160px"; |
| 159 | + configBody.innerHTML='<div id="configContent" style="position:relative;left:20px;top:5px;"></div>'; |
| 160 | + var configContent=configBody.querySelector("#configContent"); |
| 161 | + var newOrEdit=document.createElement("div"); |
| 162 | + configContent.appendChild(newOrEdit); |
| 163 | + var newOrEditSign=document.createElement("div"); |
| 164 | + newOrEditSign.innerHTML=""; |
| 165 | + newOrEditSign.style.float="left"; |
| 166 | + newOrEdit.appendChild(newOrEditSign); |
| 167 | + var newOrEditBtn=document.createElement("input"); |
| 168 | + newOrEditBtn.type="button"; |
| 169 | + newOrEditBtn.value=i18n.addListener; |
| 170 | + newOrEdit.appendChild(newOrEditBtn); |
| 171 | + var functionArea=document.createElement("p"); |
| 172 | + newOrEdit.appendChild(functionArea); |
| 173 | + var functionSelect=document.createElement("select"); |
| 174 | + functionSelect.innerHTML="<option>"+i18n.bind+"</option><option value='custom'>"+i18n.custom+"</option>"; |
| 175 | + for(var key in defaultFun){ |
| 176 | + var optionStr="<option value='"+key+"'>"+i18n[key]+"</option>"; |
| 177 | + functionSelect.innerHTML+=optionStr; |
| 178 | + } |
| 179 | + functionArea.appendChild(functionSelect); |
| 180 | + var newOrEditEval=document.createElement("input"); |
| 181 | + newOrEditEval.style.width="600px"; |
| 182 | + newOrEditEval.style.display="none"; |
| 183 | + functionArea.appendChild(newOrEditEval); |
| 184 | + var newOrEditOkBtn=document.createElement("input"); |
| 185 | + newOrEditOkBtn.type="button"; |
| 186 | + newOrEditOkBtn.value=i18n.ok; |
| 187 | + newOrEdit.appendChild(newOrEditOkBtn); |
| 188 | + var gesturesHas=document.createElement("p"); |
| 189 | + newOrEdit.appendChild(gesturesHas); |
| 190 | + var gesturesSelect=document.createElement("select"); |
| 191 | + var refreshGestures=function(){ |
| 192 | + gesturesSelect.innerHTML="<option>"+i18n.saved+"</option>"; |
| 193 | + gestures.forEach(function(item){ |
| 194 | + var optionStr="<option value='"+item.gesture+"'>"+item.gesture+"</option>"; |
| 195 | + gesturesSelect.innerHTML+=optionStr; |
| 196 | + }); |
| 197 | + }; |
| 198 | + refreshGestures(); |
| 199 | + gesturesHas.appendChild(gesturesSelect); |
| 200 | + var newOrEditDelBtn=document.createElement("input"); |
| 201 | + newOrEditDelBtn.type="button"; |
| 202 | + newOrEditDelBtn.value=i18n.del; |
| 203 | + gesturesHas.appendChild(newOrEditDelBtn); |
| 204 | + entryContent.insertBefore(configBody,entryContent.firstChild); |
| 205 | + functionSelect.onchange=function(){ |
| 206 | + if(functionSelect.options.selectedIndex===0)return; |
| 207 | + var value=functionSelect.options[functionSelect.options.selectedIndex].value; |
| 208 | + newOrEditEval.style.display=(value=="custom"?"initial":"none"); |
| 209 | + }; |
| 210 | + gesturesSelect.onchange=function(){ |
| 211 | + if(gesturesSelect.options.selectedIndex===0)return; |
| 212 | + var value=gesturesSelect.options[gesturesSelect.options.selectedIndex].value; |
| 213 | + newOrEditSign.innerHTML=value; |
| 214 | + gestures.every(function(item){ |
| 215 | + if(item.gesture==value){ |
| 216 | + functionSelect.options[1].selected = true; |
| 217 | + for (var i=2;i<functionSelect.options.length;i++) { |
| 218 | + if (functionSelect.options[i].value == item.fun) { |
| 219 | + functionSelect.options[i].selected = true; |
| 220 | + break; |
| 221 | + } |
| 222 | + } |
| 223 | + functionSelect.onchange(); |
| 224 | + newOrEditEval.value=item.fun; |
| 225 | + return false; |
| 226 | + } |
| 227 | + return true; |
| 228 | + }); |
| 229 | + }; |
| 230 | + newOrEditBtn.onclick=function(e){ |
| 231 | + newOrEditBtn.value=i18n.listening; |
| 232 | + newOrEditBtn.setAttribute("disabled","disabled"); |
| 233 | + afterGestures=function(){ |
| 234 | + newOrEditBtn.removeAttribute("disabled"); |
| 235 | + afterGestures=null; |
| 236 | + newOrEditSign.innerHTML=signs; |
| 237 | + newOrEditBtn.value=i18n.addListener; |
| 238 | + }; |
| 239 | + }; |
| 240 | + newOrEditOkBtn.onclick=function(e){ |
| 241 | + if(!newOrEditSign.innerHTML){ |
| 242 | + alert(i18n.alert1); |
| 243 | + }else if(functionSelect.options.selectedIndex===0){ |
| 244 | + alert(i18n.alert2); |
| 245 | + }else { |
| 246 | + var op=functionSelect.options; |
| 247 | + var selIndex=op.selectedIndex; |
| 248 | + if(selIndex===1 && !newOrEditEval.value){ |
| 249 | + alert(i18n.alert3); |
| 250 | + }else{ |
| 251 | + var code=op[selIndex].value; |
| 252 | + if(selIndex===1){ |
| 253 | + code=newOrEditEval.value; |
| 254 | + } |
| 255 | + for(var index in gestures){ |
| 256 | + if(gestures[index].gesture==newOrEditSign.innerHTML){ |
| 257 | + gestures.splice(index,1); |
| 258 | + break; |
| 259 | + } |
| 260 | + } |
| 261 | + gestures.push({gesture:newOrEditSign.innerHTML,fun:code}); |
| 262 | + GM_setValue("gestures",gestures); |
| 263 | + refreshGestures(); |
| 264 | + } |
| 265 | + } |
| 266 | + }; |
| 267 | + newOrEditDelBtn.onclick=function(e){ |
| 268 | + var value=gesturesSelect.options[gesturesSelect.options.selectedIndex].value; |
| 269 | + for(var index in gestures){ |
| 270 | + if(gestures[index].gesture==value){ |
| 271 | + gestures.splice(index,1); |
| 272 | + break; |
| 273 | + } |
| 274 | + } |
| 275 | + refreshGestures(); |
| 276 | + }; |
| 277 | + } |
| 278 | + function goSetting(){ |
| 279 | + location.href="https://github.com/hoothin/UserScripts/tree/master/Mouse%20Gestures"; |
| 280 | + } |
| 281 | + GM_registerMenuCommand(i18n.configure, goSetting); |
| 282 | +})(); |
0 commit comments