手机浏览器触摸手势

为手机浏览器添加触摸手势功能,例如↓↑回到顶部,↑↓回到底部,→←后退,←→前进,→↓关闭标签页,→↑恢复刚关闭的页面等。还有文字手势、图片手势和视频手势等更多功能。还可自定义你的手势功能。推荐使用Kiwi浏览器、Yandex浏览器和狐猴浏览器。

Verze ze dne 10. 11. 2023. Zobrazit nejnovější verzi.

  1. // ==UserScript==
  2. // @name 手机浏览器触摸手势
  3. // @name:en Mobile browser touch gestures
  4. // @description 为手机浏览器添加触摸手势功能,例如↓↑回到顶部,↑↓回到底部,→←后退,←→前进,→↓关闭标签页,→↑恢复刚关闭的页面等。还有文字手势、图片手势和视频手势等更多功能。还可自定义你的手势功能。推荐使用Kiwi浏览器、Yandex浏览器和狐猴浏览器。
  5. // @description:en Add touch gestures to mobile browsers. For example, ↓↑: go to the top, ↑↓: go to the bottom, →←: go back, ←→: go forward, →↓: closes the tab, →↑: restores just closed page, etc. There are more functions such as text gestures, picture gestures and video gestures. You can also customize your gestures. Recommend using Kiwi browser, Yandex browser and Lemur Browser.
  6. // @version 9.1.1
  7. // @author L.Xavier
  8. // @namespace https://greasyfork.org/zh-CN/users/128493
  9. // @match *://*/*
  10. // @license MIT
  11. // @grant window.close
  12. // @grant GM_setValue
  13. // @grant GM_getValue
  14. // @grant GM_openInTab
  15. // @grant GM_setClipboard
  16. // @grant GM_addValueChangeListener
  17. // @run-at document-start
  18. // ==/UserScript==
  19. // v9.1.1 2023-11-10 - 优化双指滑动避免触发手势等部分代码
  20. /*手势数据模块*/
  21. let gesture={
  22. '↑→↓←':'打开设置',
  23. '◆◆':'视频全屏',
  24. '●':'手势穿透',
  25. '→←':'后退',
  26. '←→':'前进',
  27. '↓↑':'回到顶部',
  28. '↑↓':'回到底部',
  29. '←↓':'刷新页面',
  30. '←↑':'新建页面',
  31. '→↓':'关闭页面',
  32. '→↑':'恢复页面',
  33. '↓↑●':'新页面打开',
  34. '↑↓●':'隐藏元素',
  35. '↓→':'复制页面',
  36. '→←→':'半屏模式',
  37. '→↓↑←':'视频解析',
  38. 'T→↑':'百度翻译',
  39. 'T←↑':'有道翻译',
  40. 'T◆◆':'双击搜索',
  41. 'I↓↑●':'打开图片',
  42. 'I→↑●':'百度搜图',
  43. 'V→':'前进10s',
  44. 'V←':'后退10s',
  45. 'V↑':'增加倍速',
  46. 'V↓':'减小倍速',
  47. 'V→●':'快进播放',
  48. 'V→○':'停止快进',
  49. 'V←●':'快退播放',
  50. 'V←○':'停止快退',
  51. 'V↑●':'增加音量',
  52. 'V↑○':'关闭增加音量',
  53. 'V↓●':'减少音量',
  54. 'V↓○':'关闭减少音量',
  55. 'V→▼':'右滑进度',
  56. 'V→▽':'关闭右滑进度',
  57. 'V←▼':'左滑进度',
  58. 'V←▽':'关闭左滑进度'
  59. },
  60. pathFn={
  61. '打开设置':'/*ONLY TOP*/openSet();',
  62. '视频全屏':'videoFullScreen();',
  63. '手势穿透':'setTimeout(()=>{if(/^[TIV]/.test(path)){path=(path.indexOf("I")>-1) ? "I" : "";return;}if(gestureData.touchEle.nodeName!=="IMG"){let imgs=gestureData.touchEle.parentNode.getElementsByTagName("img");for(let Ti=0,len=imgs.length;Ti<len;++Ti){let imgRect=imgs[Ti].getBoundingClientRect();if(gestureData.touchStart.clientX>imgRect.x && gestureData.touchStart.clientX<(imgRect.x+imgRect.width) && gestureData.touchStart.clientY>imgRect.y && gestureData.touchStart.clientY<(imgRect.y+imgRect.height)){gestureData.touchEle=imgs[Ti];break;}}}if(gestureData.touchEle.nodeName==="IMG" && settings["图片手势"]){path="I";}else if(gestureData.touchEle.style.backgroundImage && settings["图片手势"]){gestureData.touchEle.src=gestureData.touchEle.style.backgroundImage.split(\'"\')[1];path="I";}});',
  64. '后退':'/*ONLY TOP*/function pageBack(){if(gestureData.backTimer){history.go(-1);setTimeout(pageBack,20);}}gestureData.backTimer=setTimeout(()=>{gestureData.backTimer=0;window.close();},200);pageBack();',
  65. '前进':'/*ONLY TOP*/history.go(1);',
  66. '回到顶部':'/*WITH TOP*/let boxNode=gestureData.touchEle.parentNode;while(boxNode.nodeName!=="#document"){boxNode.scrollIntoView(true);if(boxNode.scrollTop){boxNode.scrollTo(0,0);}boxNode=boxNode.parentNode;}',
  67. '回到底部':'/*WITH TOP*/let boxNode=gestureData.touchEle.parentNode;while(boxNode.nodeName!=="#document"){if(getComputedStyle(boxNode).overflowY!=="hidden"){boxNode.scrollTo(0,boxNode.scrollHeight+999999);}boxNode=boxNode.parentNode;}',
  68. '刷新页面':'/*ONLY TOP*/document.documentElement.style.cssText="filter:grayscale(100%)";history.go(0);',
  69. '新建页面':'/*ONLY TOP*/GM_openInTab("//limestart.cn");',
  70. '关闭页面':'/*ONLY TOP*/window.close();',
  71. '恢复页面':'/*ONLY TOP*/GM_openInTab("chrome-native://recent-tabs");',
  72. '新页面打开':'let linkNode=gestureData.touchEle;while(true){if(linkNode.href){GM_openInTab(linkNode.href);break;}linkNode=linkNode.parentNode;if(linkNode.nodeName==="BODY"){gestureData.touchEle.click();break;}}',
  73. '隐藏元素':'let boxNode=gestureData.touchEle,area=boxNode.offsetWidth*boxNode.offsetHeight,area_p=boxNode.parentNode.offsetWidth*boxNode.parentNode.offsetHeight,area_s=screen.width*screen.height;while(boxNode.parentNode.nodeName!=="BODY" && area/area_p>0.2 && area_p/area_s<0.9){boxNode=boxNode.parentNode;area_p=boxNode.parentNode.offsetWidth*boxNode.parentNode.offsetHeight;}if(boxNode.nodeName!=="HTML"){boxNode.remove();}',
  74. '复制页面':'/*ONLY TOP*/GM_openInTab(location.href);',
  75. '半屏模式':'/*ONLY TOP*/if(gestureData.halfScreen){setTimeout(()=>{gestureData.halfScreen.remove();halfClose.remove();gestureData.halfScreen=null;document.documentElement.scrollTop=gestureData.scrollTop;},500);gestureData.scrollTop=document.body.scrollTop;let halfClose=addStyle("html{transform:translateY(0) !important;}");}else{gestureData.scrollTop=document.documentElement.scrollTop;gestureData.halfScreen=addStyle("html,body{height:43vh !important;overflow-y:auto !important;}html{transform:translateY(50vh) !important;transition:0.5s !important;overflow:hidden !important;}");document.body.scrollTop=gestureData.scrollTop;}',
  76. '视频解析':'/*ONLY TOP*/GM_openInTab("http://jx.bozrc.com:4433/player/?url="+location.href);',
  77. '百度翻译':'GM_openInTab("//fanyi.baidu.com/#auto/auto/"+encodeURIComponent(gestureData.selectWords));',
  78. '有道翻译':'GM_openInTab("//dict.youdao.com/w/eng/"+encodeURIComponent(gestureData.selectWords));',
  79. '双击搜索':'GM_setClipboard(gestureData.selectWords);let regURL=/^((https?:)?\\/\\/)?([\\w\\-]+\\.)+\\w{2,4}(:\\d{1,5})?(\\/\\S*)?$/;if(!regURL.test(gestureData.selectWords.trim())){gestureData.selectWords="//bing.com/search?q="+encodeURIComponent(gestureData.selectWords);}else if(!/^(https?:)?\\/\\//.test(gestureData.selectWords.trim())){gestureData.selectWords="//"+gestureData.selectWords.trim();}GM_openInTab(gestureData.selectWords.trim());',
  80. '打开图片':'GM_openInTab(gestureData.touchEle.src);',
  81. '百度搜图':'GM_openInTab("//graph.baidu.com/details?isfromtusoupc=1&tn=pc&carousel=0&promotion_name=pc_image_shituindex&extUiData%5bisLogoShow%5d=1&image="+gestureData.touchEle.src);',
  82. '前进10s':'videoPlayer.currentTime+=10;gestureData.tipBox.innerHTML="+10s ";gestureData.tipBox.style.display="block";setTimeout(()=>{gestureData.tipBox.style.display="none";},500);',
  83. '后退10s':'videoPlayer.currentTime-=10;gestureData.tipBox.innerHTML="-10s ";gestureData.tipBox.style.display="block";setTimeout(()=>{gestureData.tipBox.style.display="none";},500);',
  84. '增加倍速':'if(document.fullscreen){let playSpeed=videoPlayer.playbackRate;playSpeed+=(playSpeed<1.5) ? 0.25 : 0.5;gestureData.tipBox.innerHTML="×"+playSpeed+" ∞ ";gestureData.tipBox.style.display="block";videoPlayer.playbackRate=playSpeed;setTimeout(()=>{gestureData.tipBox.style.display="none";},500)}',
  85. '减小倍速':'if(document.fullscreen){let playSpeed=videoPlayer.playbackRate;playSpeed-=(playSpeed>1.5) ? 0.5 : (playSpeed>0.25 && 0.25);gestureData.tipBox.innerHTML="×"+playSpeed+" ∞ ";gestureData.tipBox.style.display="block";videoPlayer.playbackRate=playSpeed;setTimeout(()=>{gestureData.tipBox.style.display="none";},500)}',
  86. '快进播放':'gestureData.playSpeed=videoPlayer.playbackRate;videoPlayer.playbackRate=10;gestureData.tipBox.innerHTML="×10 ";gestureData.tipBox.style.display="block";',
  87. '停止快进':'videoPlayer.playbackRate=gestureData.playSpeed;gestureData.tipBox.style.display="none";',
  88. '快退播放':'gestureData.videoTimer=setInterval(()=>{--videoPlayer.currentTime;},100);gestureData.tipBox.innerHTML="- ×10 ";gestureData.tipBox.style.display="block";',
  89. '停止快退':'clearInterval(gestureData.videoTimer);gestureData.tipBox.style.display="none";',
  90. '增加音量':'if(document.fullscreen){videoPlayer.muted=false;gestureData.tipBox.innerHTML=(videoPlayer.volume*100|0)+"%";gestureData.tipBox.style.display="block";let lastY=gestureData.touchEnd.screenY;gestureData.videoTimer=setInterval(()=>{if(lastY-gestureData.touchEnd.screenY){let tempVolume=videoPlayer.volume+(lastY-gestureData.touchEnd.screenY)/100;videoPlayer.volume=+(tempVolume>1) || (+(tempVolume>0) && tempVolume);gestureData.tipBox.innerHTML=(videoPlayer.volume*100|0)+"%";lastY=gestureData.touchEnd.screenY;}},50);}',
  91. '关闭增加音量':'clearInterval(gestureData.videoTimer);gestureData.tipBox.style.display="none";',
  92. '减少音量':'if(document.fullscreen){videoPlayer.muted=false;gestureData.tipBox.innerHTML=(videoPlayer.volume*100|0)+"%";gestureData.tipBox.style.display="block";let lastY=gestureData.touchEnd.screenY;gestureData.videoTimer=setInterval(()=>{if(lastY-gestureData.touchEnd.screenY){let tempVolume=videoPlayer.volume+(lastY-gestureData.touchEnd.screenY)/100;videoPlayer.volume=+(tempVolume>1) || (+(tempVolume>0) && tempVolume);gestureData.tipBox.innerHTML=(videoPlayer.volume*100|0)+"%";lastY=gestureData.touchEnd.screenY;}},50);}',
  93. '关闭减少音量':'clearInterval(gestureData.videoTimer);gestureData.tipBox.style.display="none";',
  94. '右滑进度':'let lastX=gestureData.touchEnd.screenX,rem=videoPlayer.currentTime/60,mod=videoPlayer.currentTime%60;gestureData.tipBox.innerHTML=((rem<10) ? "0" : "")+(rem|0)+" : "+((mod<10) ? "0" : "")+(mod|0);gestureData.tipBox.style.display="block";gestureData.videoTimer=setInterval(()=>{if(gestureData.touchEnd.screenX-lastX){videoPlayer.currentTime+=(gestureData.touchEnd.screenX-lastX)*(1+Math.abs(gestureData.touchEnd.screenX-lastX)/20);lastX=gestureData.touchEnd.screenX;}rem=videoPlayer.currentTime/60;mod=videoPlayer.currentTime%60;gestureData.tipBox.innerHTML=((rem<10) ? "0" : "")+(rem|0)+" : "+((mod<10) ? "0" : "")+(mod|0);},50);',
  95. '关闭右滑进度':'clearInterval(gestureData.videoTimer);gestureData.tipBox.style.display="none";',
  96. '左滑进度':'let lastX=gestureData.touchEnd.screenX,rem=videoPlayer.currentTime/60,mod=videoPlayer.currentTime%60;gestureData.tipBox.innerHTML=((rem<10) ? "0" : "")+(rem|0)+" : "+((mod<10) ? "0" : "")+(mod|0);gestureData.tipBox.style.display="block";gestureData.videoTimer=setInterval(()=>{if(gestureData.touchEnd.screenX-lastX){videoPlayer.currentTime+=(gestureData.touchEnd.screenX-lastX)*(1+Math.abs(gestureData.touchEnd.screenX-lastX)/20);lastX=gestureData.touchEnd.screenX;}rem=videoPlayer.currentTime/60;mod=videoPlayer.currentTime%60;gestureData.tipBox.innerHTML=((rem<10) ? "0" : "")+(rem|0)+" : "+((mod<10) ? "0" : "")+(mod|0);},50);',
  97. '关闭左滑进度':'clearInterval(gestureData.videoTimer);gestureData.tipBox.style.display="none";'
  98. },
  99. settings={
  100. '滑动系数':[0.2,0,0.5,2],//[当前值,最小值,最大值,取值精度]
  101. '文字手势':true,
  102. '图片手势':true,
  103. '视频手势':true,
  104. '视频下载':false,
  105. '避免断触':false
  106. };
  107. //存储数据读取
  108. gesture=GM_getValue('gesture',gesture);
  109. pathFn=GM_getValue('pathFn',pathFn);
  110. settings=GM_getValue('settings',settings);
  111. //脚本常量
  112. const gestureData={},minSide=(screen.width>screen.height) ? screen.height : screen.width,limit=(minSide*settings['滑动系数'][0])**2,minLimit=settings['滑动系数'][0]/(settings['滑动系数'][0]+0.25)*minSide/20,attachShadow=Element.prototype.attachShadow,observer=new MutationObserver(()=>{if(checkTimer){return;}checkTimer=setTimeout(loadCheck,200);});
  113.  
  114. /*手势功能模块*/
  115. //手指功能变量
  116. let path='',startPoint=null,timeSpan=0,pressTime=0,raiseTime=0,slideTime=0,moveTime=0,lastTime=0,pathLen=0,slideLimit=0,fingersNum=0,gestureTimer=0,clickTimer=0,isAllow=0,isClick=0;
  117. //手势执行
  118. function runCode(code){
  119. try{eval(code);}catch(error){
  120. if((error+'').indexOf('unsafe-eval')>-1){
  121. if(!window._eval_){
  122. window._eval_=(()=>{
  123. let script=document.createElement('script');
  124. function thisParams(){
  125. this.window.close=window.close;
  126. this.GM_setValue=GM_setValue;
  127. this.GM_getValue=GM_getValue;
  128. this.GM_openInTab=GM_openInTab;
  129. this.GM_setClipboard=GM_setClipboard;
  130. this.runCode=runCode;
  131. this.runFrame=runFrame;
  132. this.runGesture=runGesture;
  133. this.videoFullScreen=videoFullScreen;
  134. this.findVideoBox=findVideoBox;
  135. this.addStyle=addStyle;
  136. this.openSet=openSet;
  137. this.gestureData=gestureData;
  138. this.path=path;
  139. this.videoPlayer=videoPlayer;
  140. }
  141. return (js)=>{
  142. thisParams();
  143. script.remove();
  144. script=document.createElement('script');
  145. script.innerHTML='try{'+js+'}catch(error){alert("“"+path+"” 手势执行脚本错误:\\n"+error+" !");}';
  146. document.body.append(script);
  147. }
  148. })();
  149. if(top===self){window._eval_('window.addEventListener("popstate",()=>{clearTimeout(gestureData.backTimer);gestureData.backTimer=0;},true);window.addEventListener("beforeunload",()=>{clearTimeout(gestureData.backTimer);gestureData.backTimer=0;},true);');}
  150. }
  151. window._eval_(code);
  152. }
  153. else{alert('“'+path+'” 手势执行脚本错误:\n'+error+' !');}
  154. }
  155. }
  156. function runFrame(runPath){
  157. let code=pathFn[gesture[runPath]];
  158. if(top===self || /^[TIV]/.test(runPath)){runCode(code);}
  159. else{
  160. if(code.indexOf('/*ONLY TOP*/')<0){runCode(code);}
  161. if(/\/\*(ONLY|WITH) TOP\*\//.test(code)){
  162. if(/[●▼]$/.test(runPath)){window._isPushing_=()=>{let _gestureData={};_gestureData.touchEnd=copyTouch(gestureData.touchEnd);top.postMessage({'type':'pushTouch','gestureData':_gestureData},'*');}}
  163. let _gestureData={};
  164. _gestureData.touchStart=copyTouch(gestureData.touchStart);
  165. _gestureData.touchEnd=copyTouch(gestureData.touchEnd);
  166. top.postMessage({'type':'runPath','runPath':path,'gestureData':_gestureData},'*');
  167. }
  168. }
  169. }
  170. function runGesture(newPath){
  171. raiseTime=0;
  172. if(gesture[path]){
  173. runFrame(path);
  174. if(gesture[newPath]){path=newPath;}
  175. }else if(gesture[path.slice(1)] && /^[TIV]/.test(path)){
  176. runFrame(path.slice(1));
  177. if(gesture[newPath?.slice(1)]){path=newPath;}
  178. }
  179. }
  180. //长按执行
  181. function longPress(){
  182. if(!/[●○▽]$/.test(path) && (isAllow || isClick)){
  183. isAllow=isClick=pathLen=0;
  184. startPoint=gestureData.touchEnd;
  185. let newPath=path+'○';path+='●';
  186. runGesture(newPath);
  187. }
  188. }
  189. //持续滑动执行
  190. function slidingRun(){
  191. moveTime=0;
  192. let newPath=path+'▽';path+='▼';
  193. runGesture(newPath);
  194. path=path.replace('▼','');
  195. }
  196. //手指按下
  197. function touchStart(e){
  198. clearTimeout(gestureTimer);
  199. if((fingersNum=e.touches.length)>1){return;}
  200. pressTime=Date.now();timeSpan=pressTime-raiseTime;
  201. let lineLen=raiseTime && (e.changedTouches[0].screenX-gestureData.touchEnd.screenX)**2+(e.changedTouches[0].screenY-gestureData.touchEnd.screenY)**2;
  202. if(timeSpan>50 || lineLen>limit){//断触判断
  203. startPoint=e.changedTouches[0];
  204. if(!raiseTime || lineLen>limit){
  205. path='';slideLimit=limit;
  206. gestureData.touchEle=e.target;
  207. gestureData.touchEnd=gestureData.touchStart=startPoint;
  208. gestureData.selectWords=window.getSelection()+'';
  209. if(gestureData.selectWords && settings['文字手势']){path='T';}
  210. else if(document.contains(videoPlayer) && settings['视频手势']){
  211. let videoRect=findVideoBox().getBoundingClientRect(),offsetY=fullsState>0 && videoRect.height/10;
  212. if(gestureData.touchStart.clientX>videoRect.x && gestureData.touchStart.clientX<(videoRect.x+videoRect.width) && gestureData.touchStart.clientY>(videoRect.y+offsetY) && gestureData.touchStart.clientY<(videoRect.y+videoRect.height-offsetY)){path='V';}
  213. }
  214. }else if(isClick){e.preventDefault();}
  215. slideTime=pressTime;isAllow=pathLen=0;isClick=1;
  216. }else if(isClick){clearTimeout(clickTimer);path=path.slice(0,-1);}
  217. gestureTimer=setTimeout(longPress,300+slideTime-pressTime);
  218. }
  219. //手指滑动
  220. function touchMove(e){
  221. let nowTime=Date.now();
  222. if(nowTime-lastTime<16 || fingersNum>1){return;}
  223. clearTimeout(gestureTimer);
  224. gestureData.touchEnd=e.changedTouches[0];
  225. let xLen=(gestureData.touchEnd.screenX-startPoint.screenX)**2,yLen=(gestureData.touchEnd.screenY-startPoint.screenY)**2,_pathLen=xLen+yLen,
  226. diffLen=(_pathLen>pathLen) ? _pathLen-pathLen : pathLen-_pathLen,lastIcon=path.slice(-1);
  227. lastTime=nowTime;pathLen=_pathLen;
  228. if(diffLen>minLimit && !/[○▽]/.test(lastIcon)){
  229. slideTime=nowTime;isClick=0;
  230. let direction=(xLen>yLen*1.42) ? ((gestureData.touchEnd.screenX>startPoint.screenX) ? '→' : '←') : ((gestureData.touchEnd.screenY>startPoint.screenY) ? '↓' : '↑');
  231. if(lastIcon===direction || _pathLen>slideLimit){
  232. if(lastIcon!==direction && (timeSpan<50 || 'TIV◆'.indexOf(lastIcon)>-1)){path+=direction;slideLimit*=(slideLimit<limit/2) || 0.64;moveTime=nowTime;isAllow=1;timeSpan=0;}
  233. startPoint=gestureData.touchEnd;pathLen=0;
  234. if(moveTime && nowTime-moveTime>400){setTimeout(slidingRun);}
  235. }else if(_pathLen>minLimit*16){moveTime=isAllow=0;}
  236. }
  237. gestureTimer=setTimeout(longPress,300+slideTime-nowTime);
  238. if(window._isPushing_){setTimeout(window._isPushing_);}
  239. }
  240. //手指抬起
  241. function touchEnd(e){
  242. clearTimeout(gestureTimer);
  243. if(--fingersNum>0){isClick=null;return;}
  244. if(window._isPushing_){window._isPushing_=null;}
  245. gestureData.touchEnd=e.changedTouches[0];
  246. raiseTime=Date.now();setTimeout(iframeLock);
  247. if(/[○▽]$/.test(path)){setTimeout(runGesture);return;}
  248. if(isClick){isAllow=1;path+='◆';if(/^V◆◆$|^T/.test(path)){e.stopPropagation();e.preventDefault();window.getSelection().empty();}}
  249. if(isAllow && isClick!==null){gestureTimer=setTimeout(runGesture,200);}
  250. }
  251. //延迟点击,避免断触触发点击
  252. function delayClick(e){
  253. if(e.isTrusted){
  254. e.stopPropagation();e.preventDefault();
  255. if(timeSpan<50){return;}
  256. let ev=new PointerEvent('click',{bubbles:true,cancelable:true,clientX:e.clientX,clientY:e.clientY,composed:true,detail:1,layerX:e.layerX,layerY:e.layerY,offsetX:e.offsetX,offsetY:e.offsetY,pageX:e.pageX,pageY:e.pageY,pointerId:e.pointerId,pointerType:e.pointerType,screenX:e.screenX,screenY:e.screenY,sourceCapabilities:e.sourceCapabilities,view:e.view,x:e.x,y:e.y});
  257. clickTimer=setTimeout(()=>{e.target.dispatchEvent(ev);},50);
  258. }
  259. }
  260.  
  261. /*视频功能模块*/
  262. //视频功能变量
  263. let videoEle=document.getElementsByTagName('video'),videoPlayer=null,oriLock=0,resizeTimer=0,fullsState=0;
  264. //videoPlayer赋值
  265. async function setVideo(player){
  266. let _videoPlayer=player.target || player;
  267. if(videoPlayer?.paused===false && _videoPlayer.muted===true){return;}
  268. videoPlayer=_videoPlayer;
  269. videoOriLock();
  270. videoPlayer.parentNode.append(gestureData.tipBox);
  271. if(settings['视频下载']){
  272. await findVideoBox()?.append(videoPlayer._downloadTip_);
  273. if(window._urlObjects_[videoPlayer.src]){
  274. videoPlayer._downloadTip_.innerHTML='正在捕获';
  275. videoPlayer._downloadTip_.buffers=window._urlObjects_[videoPlayer.src].sourceBuffers;
  276. window._urlObjects_[videoPlayer.src]._downloadTip_=videoPlayer._downloadTip_;
  277. delete window._urlObjects_[videoPlayer.src];
  278. }else if(videoPlayer._downloadTip_.innerHTML==='未加载'){
  279. if(!videoPlayer.src && videoPlayer.children.length){videoPlayer.src=videoPlayer.firstChild.src;}
  280. if(videoPlayer.src.indexOf('blob:') && videoPlayer.src){videoPlayer._downloadTip_.innerHTML='可下载';}
  281. }
  282. }
  283. }
  284. //video方向锁定
  285. function videoOriLock(){
  286. if(!videoPlayer.videoWidth){if(!videoPlayer.error && document.contains(videoPlayer)){setTimeout(videoOriLock,100);}oriLock=0;return;}
  287. oriLock=+(videoPlayer.videoWidth>videoPlayer.videoHeight);
  288. if(fullsState>0 && oriLock){top.postMessage({'type':'GYRO'},'*');}
  289. }
  290. //video框架锁定
  291. function iframeLock(){
  292. if(top!==self && !window._isShow_){GM_setValue('isShow',Date.now());}
  293. }
  294. //video全屏/退出全屏
  295. async function videoFullScreen(){
  296. if(resizeTimer){return;}
  297. if(document.fullscreen){await document.exitFullscreen()?.catch(Date);}
  298. else if(videoPlayer){await findVideoBox()?.requestFullscreen()?.catch(Date);}
  299. else if(iframeEle.length){GM_setValue('fullscreen',Date.now());}
  300. }
  301. //获取video全屏样式容器
  302. function findVideoBox(player=videoPlayer){
  303. if(!document.contains(player)){return null;}
  304. if(player._videoBox_?.contains(player) && (document.fullscreen || player._boxHeight_>=player._videoBox_.clientHeight)){return player._videoBox_;}
  305. player._videoBox_=player.parentNode;player.setAttribute('_videobox_','');
  306. let parentEle=player._videoBox_.parentNode,videoStyle=getComputedStyle(player),childStyle=getComputedStyle(player._videoBox_),childWidth=0,childHeight=0,_childWidth=0,_childHeight=0;
  307. if(player._videoBox_.offsetParent===parentEle){
  308. childWidth=Math.round(player.offsetWidth+(+videoStyle.marginLeft.slice(0,-2))+(+videoStyle.marginRight.slice(0,-2)));
  309. childHeight=Math.round(player.offsetHeight+(+videoStyle.marginTop.slice(0,-2))+(+videoStyle.marginBottom.slice(0,-2)));
  310. _childWidth=Math.round(player._videoBox_.offsetWidth+(+childStyle.marginLeft.slice(0,-2))+(+childStyle.marginRight.slice(0,-2)));
  311. _childHeight=Math.round(player._videoBox_.offsetHeight+(+childStyle.marginTop.slice(0,-2))+(+childStyle.marginBottom.slice(0,-2)));
  312. }else{
  313. childWidth=Math.round(player.offsetWidth+(+videoStyle.left.slice(0,-2) || 0)+(+videoStyle.marginLeft.slice(0,-2))+(+videoStyle.marginRight.slice(0,-2))+(+videoStyle.right.slice(0,-2) || 0));
  314. childHeight=Math.round(player.offsetHeight+(+videoStyle.top.slice(0,-2) || 0)+(+videoStyle.marginTop.slice(0,-2))+(+videoStyle.marginBottom.slice(0,-2))+(+videoStyle.bottom.slice(0,-2) || 0));
  315. _childWidth=Math.round(player._videoBox_.offsetWidth+(+childStyle.left.slice(0,-2) || 0)+(+childStyle.marginLeft.slice(0,-2))+(+childStyle.marginRight.slice(0,-2))+(+childStyle.right.slice(0,-2) || 0));
  316. _childHeight=Math.round(player._videoBox_.offsetHeight+(+childStyle.top.slice(0,-2) || 0)+(+childStyle.marginTop.slice(0,-2))+(+childStyle.marginBottom.slice(0,-2))+(+childStyle.bottom.slice(0,-2) || 0));
  317. }
  318. childWidth=(childWidth>_childWidth) ? childWidth : _childWidth;
  319. childHeight=(childHeight>_childHeight) ? childHeight : _childHeight;
  320. while(childWidth>=parentEle.clientWidth && parentEle.nodeName!='BODY'){
  321. if(childHeight<parentEle.clientHeight){
  322. let isBreak=1;
  323. for(let childEle of parentEle.children){
  324. childStyle=getComputedStyle(childEle);
  325. childHeight=Math.round(childEle.offsetHeight+(+childStyle.top.slice(0,-2) || 0)+(+childStyle.marginTop.slice(0,-2))+(+childStyle.marginBottom.slice(0,-2))+(+childStyle.bottom.slice(0,-2) || 0));
  326. if(childHeight===parentEle.clientHeight){isBreak=0;break;}
  327. }
  328. if(isBreak){break;}
  329. }
  330. player._videoBox_.setAttribute('_videobox_','');
  331. player._videoBox_=parentEle;player._boxHeight_=childHeight;
  332. childStyle=getComputedStyle(parentEle);
  333. if(parentEle.offsetParent===parentEle.parentNode){
  334. _childWidth=Math.round(parentEle.offsetWidth+(+childStyle.marginLeft.slice(0,-2))+(+childStyle.marginRight.slice(0,-2)));
  335. _childHeight=Math.round(parentEle.offsetHeight+(+childStyle.marginTop.slice(0,-2))+(+childStyle.marginBottom.slice(0,-2)));
  336. }else{
  337. _childWidth=Math.round(parentEle.offsetWidth+(+childStyle.left.slice(0,-2) || 0)+(+childStyle.marginLeft.slice(0,-2))+(+childStyle.marginRight.slice(0,-2))+(+childStyle.right.slice(0,-2) || 0));
  338. _childHeight=Math.round(parentEle.offsetHeight+(+childStyle.top.slice(0,-2) || 0)+(+childStyle.marginTop.slice(0,-2))+(+childStyle.marginBottom.slice(0,-2))+(+childStyle.bottom.slice(0,-2) || 0));
  339. }
  340. childWidth=(childWidth>_childWidth) ? childWidth : _childWidth;
  341. childHeight=(childHeight>_childHeight) ? childHeight : _childHeight;
  342. parentEle=parentEle.parentNode;
  343. }
  344. player._videoBox_.setAttribute('_videobox_','outer');
  345. return player._videoBox_;
  346. }
  347. //全屏检测事件
  348. function regRESIZE(){
  349. let videoCss=addStyle(''),stopResize=()=>{resizeTimer=0;};
  350. window.addEventListener('resize',()=>{
  351. clearTimeout(resizeTimer);resizeTimer=setTimeout(stopResize,200);
  352. if(document.fullscreen && !fullsState){
  353. fullsState=document.fullscreenElement;
  354. if(fullsState.nodeName==='IFRAME'){fullsState=-1;return;}
  355. let srcFindVideo=fullsState.getElementsByTagName('video'),srcVideo=(fullsState.nodeName==='VIDEO') ? fullsState : srcFindVideo[0];
  356. if(!fullsState.hasAttribute('_videobox_') && (!srcVideo || srcFindVideo.length>1 || (srcVideo._videoBox_.offsetWidth*srcVideo._videoBox_.offsetHeight/fullsState.offsetWidth/fullsState.offsetHeight)<0.9)){fullsState=-1;videoCss.innerHTML='';return;}
  357. if(srcVideo!==videoPlayer){videoPlayer?.pause();setVideo(srcVideo);}
  358. fullsState=1;if(oriLock){top.postMessage({'type':'GYRO'},'*');}
  359. videoCss.innerHTML='*[_videobox_]{inset:0 !important;margin:0 !important;padding:0 !important;transform:none !important;}*[_videobox_=""]{width:100% !important;height:100% !important;max-width:100% !important;max-height:100% !important;}video{position:fixed !important;object-fit:contain !important;}';
  360. }else if(fullsState && !document.fullscreen){fullsState=0;videoCss.innerHTML='';}
  361. },true);
  362. }
  363.  
  364. /*视频下载模块*/
  365. if(settings['视频下载']){
  366. //原始方法存储
  367. const createObjectURL=URL.createObjectURL,addSourceBuffer=MediaSource.prototype.addSourceBuffer,appendBuffer=SourceBuffer.prototype.appendBuffer,endOfStream=MediaSource.prototype.endOfStream;
  368. //初始化视频下载
  369. window._initDownload_=(player)=>{
  370. player._downloadTip_=document.createElement('div');
  371. player._downloadTip_.style.cssText='position:absolute;right:0;top:20px;background:#3498db;border-radius:20px 0 0 20px;text-align:center;padding:20px;line-height:0px;color:#fff;min-width:60px;font-size:16px;font-family:system-ui;z-index:2147483647;';
  372. player._downloadTip_.target=player;
  373. player._downloadTip_.innerHTML='未加载';
  374. if(window._urlObjects_[player.src]){
  375. player._downloadTip_.innerHTML='正在捕获';
  376. player._downloadTip_.buffers=window._urlObjects_[player.src].sourceBuffers;
  377. window._urlObjects_[player.src]._downloadTip_=player._downloadTip_;
  378. delete window._urlObjects_[player.src];
  379. }else{
  380. if(!player.src && player.children.length){player.src=player.firstChild.src;}
  381. if(player.src.indexOf('blob:') && player.src){player._downloadTip_.innerHTML='可下载';}
  382. }
  383. player._downloadTip_.onclick=window._downloadVideo_;
  384. player._videoBox_.append(player._downloadTip_);
  385. }
  386. //下载视频
  387. window._downloadVideo_=function(data){
  388. if(this.innerHTML==='未加载'){return;}
  389. if(data.target){data=this;data.src=this.target.src;}
  390. let buffers=data.buffers;
  391. if(top!==self){
  392. let _buffers=[];
  393. for(let Ti=0,len=buffers.length;Ti<len;++Ti){
  394. _buffers.push({'mime':buffers[Ti]._mime_,'bufferList':buffers[Ti]._bufferList_});
  395. }
  396. top.postMessage({'type':'download','buffers':_buffers,'src':data.src},'*');
  397. return;
  398. }
  399. let a=document.createElement('a');a.download=document.title;a.style.display='none';document.body.append(a);
  400. if(data.src.indexOf('blob:') && data.src){a.href=data.src;a.click();}
  401. else if(buffers.length){
  402. for(let Ti=0,len=buffers.length;Ti<len;++Ti){
  403. a.href=URL.createObjectURL(new Blob(buffers[Ti]._bufferList_,{'type':buffers[Ti]._mime_}));
  404. a.click();
  405. URL.revokeObjectURL(a.href);
  406. }
  407. }
  408. a.remove();
  409. }
  410. //存储MediaSource
  411. window._urlObjects_={};
  412. URL.createObjectURL=(obj)=>{
  413. let url=createObjectURL(obj);
  414. if(obj.sourceBuffers){window._urlObjects_[url]=obj;}
  415. return url;
  416. }
  417. //添加捕获
  418. MediaSource.prototype.addSourceBuffer=function(mime){
  419. let sourceBuffer=addSourceBuffer.call(this,mime);
  420. sourceBuffer._bufferList_=[];
  421. sourceBuffer._mime_=mime;
  422. sourceBuffer._mediaSource_=this;
  423. return sourceBuffer;
  424. }
  425. //捕获片段
  426. SourceBuffer.prototype.appendBuffer=function(buffer){
  427. this._bufferList_.push(buffer);
  428. if(this._mime_.indexOf('video')>-1 && this._mediaSource_._downloadTip_){this._mediaSource_._downloadTip_.innerHTML='已捕获'+this._bufferList_.length+'个片段';}
  429. appendBuffer.call(this,buffer);
  430. }
  431. //捕获完成
  432. MediaSource.prototype.endOfStream=function(){
  433. if(this._downloadTip_){this._downloadTip_.innerHTML='可下载';}
  434. endOfStream.call(this);
  435. }
  436. }
  437.  
  438. /*功能补充模块*/
  439. //功能补充变量
  440. let iframeEle=document.getElementsByTagName('iframe'),checkTimer=0;
  441. //修改Trusted-Types策略
  442. window.trustedTypes?.createPolicy('default',{createHTML:string=>string,createScript:string=>string,createScriptURL:string=>string});
  443. //设置shadow-root (open)
  444. Element.prototype.attachShadow=function(){
  445. if(!window._shadowList_){window._shadowList_=[];}
  446. let shadowRoot=attachShadow.call(this,{mode:'open'});
  447. window._shadowList_.push(shadowRoot);
  448. return shadowRoot;
  449. }
  450. //页面加载检测
  451. async function loadCheck(){
  452. //检测shadowRoot中的视频
  453. if(window._shadowList_){
  454. videoEle=[...document.getElementsByTagName('video')];
  455. for(let Ti=0,len=window._shadowList_.length;Ti<len;++Ti){
  456. videoEle.push(...window._shadowList_[Ti].querySelectorAll('video'));
  457. }
  458. }
  459. //video播放事件绑定
  460. if(videoEle.length){
  461. if(!gestureData.tipBox){
  462. //启动全屏检测
  463. regRESIZE();
  464. if(top!==self){top.postMessage({'type':'forceFullScreen'},'*');}
  465. //tip操作提示
  466. gestureData.tipBox=document.createElement('div');
  467. gestureData.tipBox.style.cssText='width:100px;height:50px;position:absolute;text-align:center;top:calc(50% - 25px);left:calc(50% - 50px);display:none;color:#1e87f0;font-size:22px;line-height:50px;background-color:rgba(0,0,0,0.6);border-radius:20px;font-family:system-ui;z-index:2147483647;';
  468. }
  469. for(let Ti=0,len=videoEle.length;Ti<len;++Ti){
  470. if(!videoEle[Ti]._videoBox_ && videoEle[Ti].offsetHeight){
  471. await findVideoBox(videoEle[Ti]);
  472. if(settings['视频下载']){await window._initDownload_(videoEle[Ti]);}
  473. if(!videoEle[Ti].paused){setVideo(videoEle[Ti]);}
  474. videoEle[Ti].addEventListener('playing',setVideo,true);
  475. }
  476. }
  477. }
  478. checkTimer=0;
  479. }
  480. //添加样式表
  481. function addStyle(css){
  482. let style=document.createElement('style');
  483. style.innerHTML=css;
  484. document.head.append(style);
  485. return style;
  486. }
  487. //复制坐标对象
  488. function copyTouch(oldObj){
  489. let newObj={};
  490. for(let Ti in oldObj){
  491. if(Ti==='target'){continue;}
  492. newObj[Ti]=oldObj[Ti];
  493. }
  494. return newObj;
  495. }
  496. //手势功能设置UI
  497. function openSet(){
  498. let gestureName='',gesturePath='',gestureBox=document.createElement('div'),pathEle=null,_clickTimer=0;
  499. //页面生成
  500. addStyle('*{overflow:hidden !important;}'+
  501. '#gestureBox{background-color:#fff;width:100%;height:100%;position:fixed;padding:0;margin:0;inset:0;overflow-y:auto !important;z-index:2147483647;}'+
  502. '#gestureBox *{font-family:system-ui;margin:0;padding:0;text-align:center;font-size:5vmin;line-height:12vmin;user-select:none !important;transform:none;text-indent:0;}'+
  503. '#gestureBox ::placeholder{color:#999;font-size:2.5vmin;line-height:6vmin;}'+
  504. '#gestureBox h1{width:60%;height:12vmin;color:#0074d9;background-color:#dee6ef;margin:3vmin auto;border-radius:12vmin;box-shadow:0.9vmin 0.9vmin 3vmin #dfdfdf;}'+
  505. '#gestureBox #addGesture{width:14vmin;height:14vmin;margin:3vmin auto;line-height:14vmin;background-color:#dee6ef;color:#032e58;font-size:7.5vmin;border-radius:15vmin;box-shadow:0.3vmin 0.3vmin 1.5vmin #dfdfdf;}'+
  506. '#gestureBox .gestureLi{height:18vmin;width:100%;border-bottom:0.3vmin solid #dfdfdf;}'+
  507. '#gestureBox .gestureLi p{margin:3vmin 0 0 1%;width:38%;height:12vmin;border-left:1.8vmin solid;color:#ffb400;background-color:#fff1cf;float:left;white-space:nowrap;text-overflow:ellipsis;text-shadow:0.3vmin 0.3vmin 3vmin #ffcb56;}'+
  508. '#gestureBox .gestureLi .gesturePath{margin:3vmin 0 0 3%;float:left;width:38%;height:12vmin;background-color:#f3f3f3;color:#000;box-shadow:0.3vmin 0.3vmin 1.5vmin #ccc9c9;border-radius:3vmin;white-space:nowrap;text-overflow:ellipsis;}'+
  509. '#gestureBox .gestureLi .delGesture{margin:3vmin 2% 0 0;width:15vmin;height:12vmin;float:right;color:#f00;text-decoration:line-through;}'+
  510. '#gestureBox #revisePath{background-color:rgba(0,0,0,0.7);width:100%;height:100%;position:fixed;inset:0;display:none;color:#000;}'+
  511. '#gestureBox #revisePath span{width:15vmin;height:15vmin;font-size:12.5vmin;line-height:15vmin;position:absolute;}'+
  512. '#gestureBox #revisePath div{color:#3339f9;position:absolute;width:30%;height:12vmin;font-size:10vmin;bottom:15%;}'+
  513. '#gestureBox #revisePath p{color:#3ba5d8;position:absolute;top:15%;font-size:10vmin;height:12vmin;width:100%;}'+
  514. '#gestureBox #revisePath #path{top:40%;color:#ffee03;height:100%;word-wrap:break-word;font-size:15vmin;line-height:18vmin;}'+
  515. '#gestureBox #editGesture{overflow-y:auto !important;background-color:#fff;width:100%;height:100%;position:fixed;inset:0;display:none;color:#000;}'+
  516. '#gestureBox #editGesture p{color:#3339f9;font-size:7.5vmin;text-align:left;margin:6vmin 0 0 9vmin;width:100%;height:9vmin;line-height:9vmin;}'+
  517. '#gestureBox #editGesture #gestureName{margin-top:6vmin;width:80%;height:12vmin;color:#000;border:0.3vmin solid #dadada;border-radius:3vmin;text-align:left;padding:0 3vmin;}'+
  518. '#gestureBox #editGesture .label_box>label{display:inline-block;margin-top:6vmin;position:relative;}'+
  519. '#gestureBox #editGesture .label_box>label>input{position:absolute;top:0;left:-6vmin;}'+
  520. '#gestureBox #editGesture .label_box>label>div{width:20vw;border:#ddd solid 0.3vmin;height:12vmin;color:#666;position:relative;}'+
  521. '#gestureBox #editGesture .label_box>label>input:checked + div{border:#d51917 solid 0.3vmin;color:#d51917;}'+
  522. '#gestureBox #editGesture .label_box>label>input + div:after{top:auto;left:auto;bottom:-3vmin;right:0;transition:none;}'+
  523. '#gestureBox #editGesture .label_box>label>input:checked + div:after{content:"";display:block;border:none;width:6vmin;height:6vmin;background-color:#d51917;transform:skewY(-45deg);position:absolute;}'+
  524. '#gestureBox #editGesture .label_box>label>input:checked + div:before{content:"";display:block;width:0.9vmin;height:2.4vmin;border-right:#fff solid 0.6vmin;border-bottom:#fff solid 0.6vmin;transform:rotate(35deg);position:absolute;bottom:0.6vmin;right:1.2vmin;z-index:1;}'+
  525. '#gestureBox #editGesture #pathFn{overflow-y:auto !important;width:80%;margin-top:6vmin;height:40%;text-align:left;line-height:6vmin;padding:3vmin;border:0.3vmin solid #dadada;border-radius:3vmin;}'+
  526. '#gestureBox #editGesture button{width:30vmin;height:15vmin;font-size:7.5vmin;line-height:15vmin;display:inline-block;color:#fff;background-color:#2866bd;margin:6vmin 3vmin 0 3vmin;border:none;}'+
  527. '#gestureBox #settingsBox{overflow-y:auto !important;background-color:#fff;width:100%;height:100%;position:fixed;inset:0;display:none;color:#000;}'+
  528. '#gestureBox #settingsBox p{color:#3339f9;text-align:left;margin:9vmin 0 0 9vmin;float:left;height:6vmin;line-height:6vmin;clear:both;}'+
  529. '#gestureBox #settingsBox .slideRail{overflow:initial !important;width:55%;background-color:#a8a8a8;float:left;margin:12vmin 0 0 3vmin;height:0.6vmin;position:relative;}'+
  530. '#gestureBox #settingsBox .slideRail .slideButton{line-height:9vmin;color:#fff;background-color:#2196f3;min-width:9vmin;height:9vmin;border-radius:9vmin;font-size:4vmin;position:absolute;top:-4.5vmin;box-shadow:0.3vmin 0.3vmin 1.8vmin #5e8aee;padding:0 1vmin;}'+
  531. '#gestureBox #settingsBox .switch{position:relative;display:inline-block;width:18vmin;height:9vmin;float:left;margin:7.5vmin 42% 0 3vmin;}'+
  532. '#gestureBox #settingsBox .switch input{display:none;}'+
  533. '#gestureBox #settingsBox .slider{border-radius:9vmin;position:absolute;cursor:pointer;inset:0;background-color:#ccc;transition:0.4s;}'+
  534. '#gestureBox #settingsBox .slider:before{border-radius:50%;position:absolute;content:"";height:7.5vmin;width:7.5vmin;left:0.6vmin;bottom:0.6vmin;background-color:white;transition:0.4s;}'+
  535. '#gestureBox #settingsBox input:checked + .slider{background-color:#2196F3;}'+
  536. '#gestureBox #settingsBox input:checked + .slider:before{transform:translateX(9vmin);}'+
  537. '#gestureBox #settingsBox #saveSettings{display:block;clear:both;width:30vmin;height:15vmin;font-size:7.5vmin;line-height:15vmin;color:#fff;background-color:#2866bd;border:none;margin:12vmin 0 0 calc(50% - 15vmin);float:left;}');
  538. gestureBox.id='gestureBox';
  539. document.body.append(gestureBox);
  540. gestureBox.innerHTML='<h1 id="openSettings">手势轨迹设置</h1><div id="addGesture">+</div><div id="gestureUL"></div>'+
  541. '<div id="revisePath"><span style="top:0;left:0;text-align:left;">┌</span><span style="top:0;right:0;text-align:right;">┐</span><span style="bottom:0;left:0;text-align:left;">└</span><span style="bottom:0;right:0;text-align:right;">┘</span>'+
  542. '<p>请滑动手指</p><p id="path"></p><div id="clearPath" style="left:10%;">清除</div><div id="cancleRevise" style="right:10%;">保存</div></div>'+
  543. '<div id="editGesture"><p>手势名称:</p><input type="text" id="gestureName" maxlength="12" placeholder="最大输入12个字符">'+
  544. '<p>手势类型:</p><div class="label_box"><label><input type="radio" id="GG" name="gestureType" value=""><div>一般</div></label><label><input type="radio" id="T" name="gestureType" value="T"><div>文字</div></label><label><input type="radio" id="I" name="gestureType" value="I"><div>图片</div></label><label><input type="radio" id="V" name="gestureType" value="V"><div>视频</div></label></div>'+
  545. '<p>手势执行脚本:</p><textarea id="pathFn" placeholder="可用变量说明↓\n gestureData:手势数据常量,如果你需要在不同手势间传递变量,你可以赋值gestureData.变量名=变量值;\n gestureData.touchEle:手指触摸的源元素;\n gestureData.selectWords:选中的文字;\n gestureData.touchStart:触摸开始坐标对象;\n gestureData.touchEnd:触摸最新坐标对象;\n path:滑动的路径;\n videoPlayer:正在播放的视频元素。'+
  546. '\n\n可用方法说明↓\n addStyle(CSS样式):将CSS样式添加到网页上;\n runGesture():以path为路径执行手势,你可以修改path后执行此方法;\n GM_openInTab(链接):打开链接;\n GM_setClipboard(文本):复制文本到剪切板;\n GM_setValue(变量名,变量值):在油猴中存储数据;\n GM_getValue(变量名,默认值):从油猴中取出数据,没有则使用默认值。'+
  547. '\n\n可识别代码注释说明(仅对一般手势生效)↓\n 默认情况:存在iframe时,所有手势只会在触发手势的页面对象执行!\n 添加/*ONLY TOP*/:手势只在顶级页面对象执行;\n 添加/*WITH TOP*/:手势同时在当前页面对象和顶级页面对象执行。"></textarea>'+
  548. '<div style="width:100%;height:0.3vmin;"></div><button id="saveGesture">保存</button><button id="closeEdit">关闭</button></div>'+
  549. '<div id="settingsBox"><h1>功能开关设置</h1><span id="settingList"></span><button id="saveSettings">保存</button></div>';
  550. pathEle=document.getElementById('path');
  551.  
  552. //编辑手势
  553. function editGesture(){
  554. gestureName=this.parentNode.getAttribute('name');
  555. if(['打开设置','视频全屏','手势穿透'].indexOf(gestureName)>-1){alert('该手势脚本无法修改!');return;}
  556. gesturePath=this.parentNode.getAttribute('path');
  557. let selectType=(/^[TIV]/.test(gesturePath)) ? gesturePath.slice(0,1) : 'GG';
  558. document.getElementById(selectType).click();
  559. document.getElementById('gestureName').value=gestureName;
  560. document.getElementById('pathFn').value=pathFn[gestureName];
  561. document.getElementById('editGesture').style.display='block';
  562. }
  563. //修改路径
  564. function revisePath(){
  565. gestureName=this.parentNode.getAttribute('name');
  566. gesturePath=this.parentNode.getAttribute('path');
  567. pathEle.innerHTML='';
  568. window.removeEventListener('touchmove',touchMove,true);
  569. document.getElementById('revisePath').style.display='block';
  570. }
  571. //删除手势
  572. function delGesture(){
  573. gestureName=this.parentNode.getAttribute('name');
  574. if(['打开设置','视频全屏','手势穿透'].indexOf(gestureName)>-1){alert('该手势无法删除!');return;}
  575. if(!confirm('确定删除"'+gestureName+'"手势')){return;}
  576. gesturePath=this.parentNode.getAttribute('path');
  577. delete pathFn[gestureName];
  578. delete gesture[gesturePath];
  579. GM_setValue('pathFn',pathFn);
  580. GM_setValue('gesture',gesture);
  581. init();
  582. }
  583. //滑动条
  584. function silideBar(e){
  585. e.preventDefault();fingersNum=2;
  586. let diffX=e.changedTouches[0].clientX-gestureData.touchStart.clientX,
  587. leftPX=(+this.style.left.slice(0,-2))+diffX,vmin=this.offsetWidth/2,setArr=settings[this.id];
  588. leftPX=(leftPX<-vmin) ? -vmin : ((leftPX>(diffX=this.parentNode.offsetWidth-vmin)) ? diffX : leftPX);
  589. this.style.left=leftPX+'px';
  590. this.innerHTML=((leftPX+vmin)/this.parentNode.offsetWidth*(setArr[2]-setArr[1])+setArr[1]).toFixed(setArr[3]);
  591. gestureData.touchStart=e.changedTouches[0];
  592. }
  593. //长按执行
  594. function _longPress(){if(!/[●○▼▽]$/.test(pathEle.innerHTML)){isClick=pathLen=0;startPoint=gestureData.touchEnd;pathEle.innerHTML+='●';}}
  595. //持续滑动执行
  596. function _slidingRun(){moveTime=0;pathEle.innerHTML+='▼';}
  597. //点击执行
  598. function _clickRun(){if(!/[○▼▽]$/.test(pathEle.innerHTML)){pathEle.innerHTML+='◆';}}
  599. //界面初始化
  600. function init(){
  601. document.getElementById('gestureUL').innerHTML='';
  602. for(gestureName in pathFn){
  603. gesturePath='';
  604. for(let Ti in gesture){
  605. if(gesture[Ti]===gestureName){gesturePath=Ti;break;}
  606. }
  607. document.getElementById('gestureUL').innerHTML+='<div class="gestureLi" name="'+gestureName+'" path="'+gesturePath+'"><p>'+gestureName+'</p><div class="gesturePath">'+gesturePath+'</div><div class="delGesture">删除</div></div>';
  608. }
  609. //操作绑定
  610. let gestureEle=document.querySelectorAll('#gestureBox .gestureLi p');
  611. for(let Ti=0,len=gestureEle.length;Ti<len;++Ti){
  612. gestureEle[Ti].addEventListener('click',editGesture,true);
  613. }
  614. gestureEle=document.querySelectorAll('#gestureBox .gestureLi .gesturePath');
  615. for(let Ti=0,len=gestureEle.length;Ti<len;++Ti){
  616. gestureEle[Ti].addEventListener('click',revisePath,true);
  617. }
  618. gestureEle=document.querySelectorAll('#gestureBox .gestureLi .delGesture');
  619. for(let Ti=0,len=gestureEle.length;Ti<len;++Ti){
  620. gestureEle[Ti].addEventListener('click',delGesture,true);
  621. }
  622. }
  623. init();
  624.  
  625. //.新建手势
  626. document.getElementById('addGesture').addEventListener('click',()=>{
  627. gestureName=gesturePath='';
  628. document.getElementById('GG').click();
  629. document.getElementById('gestureName').value='';
  630. document.getElementById('pathFn').value='';
  631. document.getElementById('editGesture').style.display='block';
  632. },true);
  633. //保存手势
  634. document.getElementById('saveGesture').addEventListener('click',()=>{
  635. if(!document.getElementById('gestureName').value){alert('请输入手势名称!');return;}
  636. if(pathFn[document.getElementById('gestureName').value] && gestureName!==document.getElementById('gestureName').value){alert('该手势名称已被占用!');return;}
  637. delete pathFn[gestureName];
  638. delete gesture[gesturePath];
  639. let typeEle=document.getElementsByName('gestureType');
  640. for(let Ti=0,len=typeEle.length;Ti<len;++Ti){
  641. if(typeEle[Ti].checked){
  642. gesturePath=typeEle[Ti].value+((gestureName && gesturePath.indexOf('[')<0) ? ((/^[TIV]/.test(gesturePath)) ? gesturePath.slice(1) : gesturePath) : ('['+document.getElementById('gestureName').value+']'));
  643. break;
  644. }
  645. }
  646. gesture[gesturePath]=document.getElementById('gestureName').value;
  647. pathFn[document.getElementById('gestureName').value]=document.getElementById('pathFn').value;
  648. GM_setValue('pathFn',pathFn);
  649. GM_setValue('gesture',gesture);
  650. init();
  651. document.getElementById('editGesture').style.display='none';
  652. },true);
  653. //关闭编辑
  654. document.getElementById('closeEdit').addEventListener('click',()=>{
  655. document.getElementById('editGesture').style.display='none';
  656. },true);
  657. //路径修改事件
  658. document.getElementById('revisePath').addEventListener('touchstart',()=>{
  659. if(fingersNum>1){return;}
  660. clearTimeout(gestureTimer);clearTimeout(_clickTimer);
  661. gestureTimer=setTimeout(_longPress,300+slideTime-pressTime);
  662. },true);
  663. document.getElementById('revisePath').addEventListener('touchmove',(e)=>{
  664. e.preventDefault();
  665. let nowTime=Date.now();
  666. if(nowTime-lastTime<16 || fingersNum>1){return;}
  667. clearTimeout(gestureTimer);
  668. gestureData.touchEnd=e.changedTouches[0];
  669. let xLen=(gestureData.touchEnd.screenX-startPoint.screenX)**2,yLen=(gestureData.touchEnd.screenY-startPoint.screenY)**2,_pathLen=xLen+yLen,
  670. diffLen=(_pathLen>pathLen) ? _pathLen-pathLen : pathLen-_pathLen,lastIcon=pathEle.innerHTML.slice(-1);
  671. lastTime=nowTime;pathLen=_pathLen;
  672. if(diffLen>minLimit && !/[○▼▽]/.test(lastIcon)){
  673. slideTime=nowTime;isClick=0;
  674. let direction=(xLen>yLen) ? ((gestureData.touchEnd.screenX>startPoint.screenX) ? '→' : '←') : ((gestureData.touchEnd.screenY>startPoint.screenY) ? '↓' : '↑');
  675. if(lastIcon===direction || _pathLen>limit){
  676. if(lastIcon!==direction){pathEle.innerHTML+=direction;moveTime=nowTime;}
  677. startPoint=gestureData.touchEnd;pathLen=0;
  678. if(moveTime && nowTime-moveTime>400){_slidingRun();}
  679. }else if(_pathLen>minLimit*16){moveTime=0;}
  680. }
  681. gestureTimer=setTimeout(_longPress,300+slideTime-nowTime);
  682. },true);
  683. document.getElementById('revisePath').addEventListener('touchend',(e)=>{
  684. if(!isClick || fingersNum>0){return;}
  685. if(path.indexOf('◆◆')>-1){path='';
  686. switch(pathEle.innerHTML.slice(-1)){
  687. case '●':{pathEle.innerHTML=pathEle.innerHTML.slice(0,-1)+'○';break;}
  688. case '○':{pathEle.innerHTML=pathEle.innerHTML.slice(0,-1)+'●';break;}
  689. case '▼':{pathEle.innerHTML=pathEle.innerHTML.slice(0,-1)+'▽';break;}
  690. case '▽':{pathEle.innerHTML=pathEle.innerHTML.slice(0,-1)+'▼';break;}
  691. default:{pathEle.innerHTML+='◆';setTimeout(_clickRun,100);break;}
  692. }
  693. }else{_clickTimer=setTimeout(_clickRun,200);}
  694. });
  695. //清除路径
  696. document.getElementById('clearPath').addEventListener('touchend',(e)=>{
  697. e.stopPropagation();
  698. if(!isClick || fingersNum>0){return;}
  699. if(path.indexOf('◆◆')>-1){path='';pathEle.innerHTML='';}
  700. else{pathEle.innerHTML=pathEle.innerHTML.slice(0,-1);}
  701. });
  702. //保存修改路径
  703. document.getElementById('cancleRevise').addEventListener('touchend',(e)=>{
  704. e.stopPropagation();e.preventDefault();
  705. if(!isClick || fingersNum>0){return;}
  706. if(pathEle.innerHTML){
  707. if(gestureName==='视频全屏' && pathEle.innerHTML.slice(-1)!=='◆'){alert('视频全屏需要以◆结尾!');return;}
  708. if(gesture[pathEle.innerHTML]==='手势穿透'){alert('路径与"手势穿透"功能冲突!');return;}
  709. if(/^[TIV]/.test(gesturePath)){pathEle.innerHTML=gesturePath.slice(0,1)+pathEle.innerHTML;}
  710. delete gesture[gesturePath];
  711. if(gesture[pathEle.innerHTML]){
  712. let pathTXT=((/^[TIV]/.test(gesturePath)) ? gesturePath.slice(0,1) : '')+'['+gesture[pathEle.innerHTML]+']';
  713. gesture[pathTXT]=gesture[pathEle.innerHTML];
  714. }
  715. gesture[pathEle.innerHTML]=gestureName;
  716. GM_setValue('gesture',gesture);
  717. init();
  718. }
  719. window.addEventListener('touchmove',touchMove,{capture:true,passive:true});
  720. document.getElementById('revisePath').style.display='none';
  721. });
  722. //打开功能开关设置
  723. document.getElementById('openSettings').addEventListener('click',()=>{
  724. gestureBox.style.cssText='overflow-y:hidden !important';
  725. document.getElementById('settingsBox').style.display='block';
  726. let settingList=document.getElementById('settingList');
  727. settingList.innerHTML='';
  728. for(let Ti in settings){
  729. settingList.innerHTML+='<p>'+Ti+':</p>';
  730. if(typeof(settings[Ti])==='boolean'){
  731. settingList.innerHTML+='<label class="switch"><input type="checkbox" id="'+Ti+'" '+((settings[Ti]) ? 'checked' : '')+'><div class="slider"></div></label>';
  732. }else if(typeof(settings[Ti])==='object'){
  733. settingList.innerHTML+='<div class="slideRail"><div class="slideButton" id="'+Ti+'"></div></div>';
  734. let slideButton=document.getElementById(Ti),
  735. leftPX=slideButton.parentNode.offsetWidth*(settings[Ti][0]-settings[Ti][1])/(settings[Ti][2]-settings[Ti][1])-slideButton.offsetWidth/2;
  736. slideButton.style.left=leftPX+'px';
  737. slideButton.innerHTML=settings[Ti][0].toFixed(settings[Ti][3]);
  738. }
  739. }
  740. let slideList=document.getElementsByClassName('slideButton');
  741. for(let Ti=0,len=slideList.length;Ti<len;++Ti){
  742. slideList[Ti].addEventListener('touchmove',silideBar,true);
  743. }
  744. },true);
  745. //保存功能开关设置
  746. document.getElementById('saveSettings').addEventListener('click',()=>{
  747. gestureBox.style.cssText='';
  748. for(let Ti in settings){
  749. if(typeof(settings[Ti])==='boolean'){
  750. settings[Ti]=document.getElementById(Ti).checked;
  751. }else if(typeof(settings[Ti])==='object'){
  752. settings[Ti][0]=+document.getElementById(Ti).innerHTML;
  753. }
  754. }
  755. GM_setValue('settings',settings);
  756. document.getElementById('settingsBox').style.display='none';
  757. },true);
  758. }
  759.  
  760. /*事件注册模块*/
  761. (function(){
  762. if(top===self){
  763. //清除后退定时器
  764. window.addEventListener('popstate',()=>{clearTimeout(gestureData.backTimer);gestureData.backTimer=0;},true);
  765. window.addEventListener('beforeunload',()=>{clearTimeout(gestureData.backTimer);gestureData.backTimer=0;},true);
  766. //接收iframe数据
  767. window.addEventListener('message',async (e)=>{
  768. let data=e.data;
  769. switch(data.type){
  770. case 'GYRO':{//锁定横屏模式
  771. await screen.orientation.lock('landscape')?.catch(Date);
  772. break;}
  773. case 'forceFullScreen':{//iframe强制可全屏
  774. for(let Ti=0,len=iframeEle.length;Ti<len;++Ti){
  775. if(iframeEle[Ti].contentWindow===e.source){
  776. if(!iframeEle[Ti].allowFullscreen){
  777. iframeEle[Ti].allowFullscreen=true;
  778. if(iframeEle[Ti].getAttribute('src') && /^((https?:)?\/\/)?([\w\-]+\.)+\w{2,4}(:\d{1,5})?(\/\S*)?$/.test(iframeEle[Ti].src)){
  779. iframeEle[Ti].src=iframeEle[Ti].src;
  780. }
  781. }
  782. break;
  783. }
  784. }
  785. break;}
  786. case 'runPath':{//iframe手势在顶级页面执行
  787. for(let Ti=0,len=iframeEle.length;Ti<len;++Ti){
  788. if(iframeEle[Ti].contentWindow===e.source){
  789. let ifrRect=iframeEle[Ti].getBoundingClientRect();
  790. gestureData.touchStart=data.gestureData.touchStart;gestureData.touchEnd=data.gestureData.touchEnd;
  791. gestureData.touchStart.target=gestureData.touchEnd.target=gestureData.touchEle=iframeEle[Ti];
  792. gestureData.touchStart.pageX=gestureData.touchStart.clientX+=ifrRect.x;
  793. gestureData.touchStart.pageY=gestureData.touchStart.clientY+=ifrRect.y;
  794. gestureData.touchEnd.pageX=gestureData.touchEnd.clientX+=ifrRect.x;
  795. gestureData.touchEnd.pageY=gestureData.touchEnd.clientY+=ifrRect.y;
  796. break;
  797. }
  798. }
  799. path=data.runPath;setTimeout(runGesture);
  800. break;}
  801. case 'pushTouch':{//iframe手势坐标传递
  802. let ifrRect=gestureData.touchEle.getBoundingClientRect();
  803. gestureData.touchEnd=data.gestureData.touchEnd;
  804. gestureData.touchEnd.target=gestureData.touchEle;
  805. gestureData.touchEnd.pageX=gestureData.touchEnd.clientX+=ifrRect.x;
  806. gestureData.touchEnd.pageY=gestureData.touchEnd.clientY+=ifrRect.y;
  807. break;}
  808. case 'download':{//iframe视频下载
  809. window._downloadVideo_(data);
  810. break;}
  811. }
  812. },true);
  813. }else{
  814. //iframe视频全屏
  815. GM_addValueChangeListener('fullscreen',async (name,old_value,new_value,remote)=>{
  816. if(!document.hidden && window._isShow_){
  817. await findVideoBox()?.requestFullscreen()?.catch(Date);
  818. }
  819. });
  820. //iframe锁定
  821. GM_addValueChangeListener('isShow',(name,old_value,new_value,remote)=>{
  822. if(!document.hidden){window._isShow_=!remote;}
  823. });
  824. }
  825. //解除选中限制
  826. addStyle('*{user-select:text !important;touch-action:manipulation;}');
  827. //加载检测
  828. checkTimer=setTimeout(loadCheck,200);
  829. observer.observe(document,{childList:true,subtree:true});
  830. //手势事件注册
  831. window.addEventListener('touchstart',touchStart,{capture:true,passive:false});
  832. window.addEventListener('touchmove',touchMove,{capture:true,passive:true});
  833. window.addEventListener('touchend',touchEnd,{capture:true,passive:false});
  834. window.addEventListener('touchcancel',touchEnd,{capture:true,passive:false});
  835. window.addEventListener('contextmenu',(e)=>{if(path.indexOf("I")>-1){e.preventDefault();}},true);//长按图片时禁止弹出菜单
  836. if(settings['避免断触']){window.addEventListener('click',delayClick,true);}
  837. })();