触摸屏视频优化

触摸屏视频播放手势支持,上下滑调整音量,左右滑调整进度

目前为 2022-04-16 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 触摸屏视频优化
  3. // @namespace https://github.com/HeroChan0330
  4. // @version 2.15
  5. // @description 触摸屏视频播放手势支持,上下滑调整音量,左右滑调整进度
  6. // @author HeroChanSysu
  7. // @match https://*/*
  8. // @match http://*/*
  9. // @match ftp://*/*
  10. // @grant GM_addStyle
  11. // ==/UserScript==
  12.  
  13. var TouchGesture={forbidScroll:false,orientationLocked:false,ismobile:false};
  14. var TouchGestureWhiteList={
  15. "www.bilibili.com":{
  16. "bilibili-player-video":"bilibili-player-area",
  17. forbidScrollList:["bilibili-player-dm-tip-wrap"]
  18. },
  19. "m.bilibili.com":{
  20. forbidScrollList:["player-mobile-display","mplayer-display"]
  21. },
  22. "weibo.com":{
  23. forbidScrollList:["wbpv-tech","wbpv-open-layer-button"]
  24. },
  25. "www.youtube.com":{
  26. "html5-video-container":"ytd-player",
  27. forbidScrollList:["video-stream"]
  28. },
  29. "m.youtube.com":{
  30. "html5-video-container":"player-container",
  31. forbidScrollList:["animation-enabled","player-controls-background"]
  32. },
  33. "v.youku.com":{
  34. "youku-film-player":"youku-film-player",
  35. "video-layer":"youku-player",
  36. forbidScrollList:["yk-trigger-layer","kui-dashboard-display-panel","kui-message-information"]
  37. },
  38. "www.facebook.com":{
  39. forbidScrollList:["i09qtzwb"]
  40. }
  41.  
  42. };
  43.  
  44.  
  45. var CustomizedVideoTAG = {
  46. "www.bilibili.com":["bwp-video"]
  47. };
  48.  
  49. var TouchGestureBlackList=[
  50. "v.qq.com"
  51. ];
  52.  
  53. var forbidScrollList=[];
  54.  
  55. TouchGesture.VideoGesture=function(videoElement){
  56. this.touchDownPt=null; //触摸按下时得位置
  57. this.touchStartPt=null; //顺着一定方向滑动时并触发功能,开始计算的点
  58.  
  59. this.sweepDir=0; //0:no sweep 1:up 2:down 3:left 4:right
  60. this.startTouchVideoTime;
  61. this.startTouchVideoVolume;
  62. this.touchResult=0; //触摸结果暂存
  63. this.startTouchFingers=0; //在触发功能前触摸点数
  64. this.originalPlayrate=1; //视频原本的播放速率
  65. //this.bodyPosition="";
  66. this.videoBrightness=1;
  67. this.startTouchBrightness=1;
  68. this.longTouch=false;
  69.  
  70. this._videoSrcStore=null;
  71. this._videoElement=videoElement;//对象video标签
  72. this._videoElementAbLeft=0; //video标签相对页面的left
  73. this._elementFrame=null;//文字显示的框架
  74. this._toastText=null; //文字显示
  75. this._unlockBtn = null; //解锁按钮
  76.  
  77. this._containElement=null; //_elementFrame的父级
  78. this._eventListenElement=null;//监听触摸动作的元素
  79.  
  80. this._fullScreenNow=tg_IsFullscreen();
  81. if(this._videoElement.style.filter=="" || this._videoElement.style.filter==null)
  82. this._videoElement.style.filter = "brightness(1)";
  83. // console.log("_fullScreenNow:"+this._fullScreenNow);
  84.  
  85. // console.log(TouchGestureWhiteList);
  86. // console.log(TouchGestureWhiteList["www.bilibili.com"]!=null);
  87. this.createDom();
  88. this.findBestRoot();
  89. this.applyDom();
  90.  
  91.  
  92. // video内地址更改时,重新设置页面(针对bilibili连续播放)
  93. var self = this;
  94. this._videoElement.addEventListener('play', function () { //播放开始执行的函数
  95. if(self._videoSrcStore == null){
  96. // alert("first play");
  97. self._videoSrcStore=self._videoElement.src;
  98.  
  99. }else if(self._videoElement.src!=self._videoSrcStore){
  100. // alert("replay");
  101. self.restoreDom();
  102. self.findBestRoot();
  103. self.applyDom();
  104. }
  105. });
  106.  
  107. // this._videoElement.addEventListener('canplay', function () {
  108. // self._videoWidth = this.videoWidth;
  109. // self._videoHeight = this.videoHeight;
  110. // });
  111.  
  112. };
  113.  
  114. // 产生显示元素
  115. TouchGesture.VideoGesture.prototype.createDom=function(parentElement){
  116.  
  117. var toastDiv=document.createElement("div");
  118. var toastText=document.createElement("span");
  119. var unlockBtn = document.createElement("div");
  120.  
  121. toastDiv.appendChild(toastText);
  122. toastDiv.classList.add("TouchGesture_Toast");
  123. toastText.classList.add("TouchGesture_ToastText");
  124. unlockBtn.classList.add("TouchGesture_UnlockBtn");
  125. toastDiv.style.display="none";
  126. unlockBtn.style.display="none";
  127. unlockBtn.innerHTML = "🔒";
  128. this._elementFrame=toastDiv;
  129. this._toastText=toastText;
  130. this._unlockBtn = unlockBtn;
  131.  
  132. this._elementFrame.classList.add("TouchGestureForbidScroll");
  133. this._toastText.classList.add("TouchGestureForbidScroll");
  134. this._unlockBtn.classList.add("TouchGestureForbidScroll");
  135.  
  136. this._touchStartHandler=this.onTouchStart.bind(this);
  137. this._touchEndHandler=this.onTouchEnd.bind(this);
  138. this._touchMoveHandler=this.onTouchMove.bind(this);
  139. this._windowResizeHandeler=this.fullScreenDetect.bind(this);
  140. window.addEventListener("resize",this._windowResizeHandeler,"false");
  141. // this._orientationChange = this.onOrientationChange;
  142. // window.addEventListener("orientationchange",this._orientationChange,"false");
  143.  
  144. var self = this;
  145. this._unlockBtn.onclick=function(event){
  146. // console.log("unlock cancel");
  147. screen.orientation.unlock();
  148. event.stopPropagation();
  149. };
  150.  
  151. this._unlockBtn.addEventListener("transitionend",function(){
  152. self._unlockBtn.style.display = "none";
  153. });
  154.  
  155. };
  156.  
  157. // 找到显示元素最佳的parent及监听元素
  158. TouchGesture.VideoGesture.prototype.findBestRoot=function(){
  159. var self=this;
  160. var targetElement=this._videoElement;
  161. var hostDomain=window.location.host;
  162. var defaultSetting=TouchGestureWhiteList[hostDomain];
  163. var defaultSettingSuccess=false;
  164. if(defaultSetting!=null){
  165. // console.log("defaultSetting!=null");
  166. var parentElement=this._videoElement.parentElement;
  167. // console.log(parentElement);
  168. var inList=false;
  169. var targetFrameClass="";
  170. //vertify whether the video parentelement`s classname is in the whitelist
  171. if(parentElement.classList){
  172. parentElement.classList.forEach(element => {
  173. if(defaultSetting[element]!=null){
  174. targetFrameClass=defaultSetting[element];
  175. inList=true;
  176. // console.log(targetFrameClass+" INLIST");
  177.  
  178. // break;
  179. return;
  180. }
  181. });
  182. }
  183. if(inList==true){
  184. var finded=false;
  185. var temp=parentElement;
  186. while(finded==false){
  187. if(temp.classList.contains(targetFrameClass)){
  188. finded=true;
  189. defaultSettingSuccess=true;
  190. self._containElement=temp;
  191. self._eventListenElement=temp;
  192. break;
  193. // console.log("FIND");
  194. }
  195. temp=temp.parentElement;
  196. if(temp.tagName.toLowerCase()=="body"){
  197. break;
  198. }
  199. }
  200. }
  201. }
  202.  
  203. if(defaultSettingSuccess==false){
  204. if(this._fullScreenNow==false){
  205. targetElement=targetElement.parentElement;
  206. var topest=false;
  207. while(!topest){
  208. // targetElement.classList.add("TouchGestureForbidScroll");
  209. var temp=targetElement.parentElement;
  210. var size1=targetElement.offsetWidth*targetElement.offsetHeight;
  211. var size2=temp.offsetWidth*temp.offsetHeight;
  212. if(temp.offsetWidth>=targetElement.offsetWidth&&temp.offsetHeight>=targetElement.offsetHeight&&size2/size1<=1.2){
  213. targetElement=temp;
  214. }else{
  215. topest=true;
  216. }
  217. if(temp.tagName.toLowerCase()=="body"){
  218. break;
  219. }
  220. }
  221. self._containElement=targetElement;
  222. self._eventListenElement=targetElement;
  223. }else{
  224. // alert("FULLSCREEN!");
  225. if(document.fullscreenElement!=null){
  226. self._containElement=document.fullscreenElement;
  227. self._eventListenElement=document.fullscreenElement;
  228. }
  229. }
  230. }
  231.  
  232. };
  233.  
  234. // TouchGesture.findFullScreenRoot=function(){
  235.  
  236. // };
  237.  
  238. TouchGesture.VideoGesture.prototype.simMouseMoveDock=function(){
  239. if(this.touchDownPt==null)
  240. return;
  241. var self=this;
  242. var event = new MouseEvent('mousemove', {
  243. view: document.defaultView,
  244. bubbles: true,
  245. cancelable: false,
  246. clientX:self._videoElement.clientWidth/2+Math.floor(Math.random()*20),
  247. clientY:self._videoElement.clientHeight*1.5
  248. });
  249. self._videoElement.dispatchEvent(event);
  250. setTimeout(() => {
  251. self.simMouseMoveDock();
  252. }, 1000);
  253. };
  254.  
  255.  
  256. TouchGesture.VideoGesture.prototype.simMouseMoveCenter=function(){
  257. var self=this;
  258. var event = new MouseEvent('mousemove', {
  259. view: document.defaultView,
  260. bubbles: true,
  261. cancelable: false,
  262. clientX:self._videoElement.clientWidth/2,
  263. clientY:self._videoElement.clientHeight/2
  264. });
  265. self._videoElement.dispatchEvent(event);
  266. };
  267.  
  268. // 触摸开始
  269. TouchGesture.VideoGesture.prototype.onTouchStart=function(e){
  270. if(this._videoElement.src.length<=2){
  271. // 视频的src长度太低,视频基本无效。
  272. return;
  273. }
  274. if(this.sweepDir!=0){
  275. // 当滑动一定距离后,不再响应touchdown
  276. if(e.touches.length!=this.startTouchFingers){
  277. // 触摸点数变化,取消当前的触摸结果
  278. this.cancelTouch();
  279. }
  280. return;
  281. }
  282. // this.forbidScroll();
  283. // console.log(e);
  284. this.setElementLayout();
  285. // console.log(e);
  286. this.startTouchFingers=e.touches.length;
  287. if(this.startTouchFingers>0){
  288. this.originalPlayrate=this._videoElement.playbackRate;
  289. if(this.startTouchFingers==2){
  290. var dis=Math.sqrt((e.touches[0].clientX-e.touches[1].clientX)*(e.touches[0].clientX-e.touches[1].clientX)+(e.touches[0].clientY-e.touches[1].clientY)*(e.touches[0].clientY-e.touches[1].clientY));
  291. var longside = (document.body.clientWidth>document.body.clientHeight)?document.body.clientWidth:document.body.clientHeight;
  292. if(dis>longside/4){
  293. // 两个触摸到相隔太远,取消触摸结果
  294. this.touchDownPt=null;
  295. this.startTouchFingers=0;
  296. this.cancelTouch();
  297. return;
  298. }else{
  299. // 记录原本的播放速率,并且2倍速播放
  300. this._videoElement.playbackRate=4.0;
  301. this.setToast("4倍速播放");
  302. }
  303. }
  304. else if(this.startTouchFingers==1){
  305. var self = this;
  306. setTimeout(() => {
  307. if(self.touchDownPt != null && self.touchResult==0 && self.startTouchFingers==1){
  308. var str = seconds2TimeStr(Math.floor(self._videoElement.currentTime)) + " / " + seconds2TimeStr(Math.floor(self._videoElement.duration));
  309. self.setToast(str);
  310. self.simMouseMoveDock();
  311. self.longTouch=true;
  312. }
  313. }, 500);
  314. }
  315. this.touchDownPt=e.touches[0];
  316.  
  317. var ableft=this._videoElement.offsetLeft;
  318. var temp=this._videoElement.offsetParent;
  319. while(temp!=null){
  320. ableft+=temp.offsetLeft;
  321. temp=temp.offsetParent;
  322. }
  323. this._videoElementAbLeft=ableft;
  324. // console.log("ableft:"+ableft);
  325. }else{
  326. this.cancelTouch();
  327. }
  328. };
  329.  
  330. TouchGesture.VideoGesture.prototype.onTouchMove=function(e){
  331. // console.log(e);
  332. var videoElement=this._videoElement;
  333. if(this.touchDownPt==null)
  334. return;
  335. // if(e.touches.length!=this.startTouchFingers){
  336. // this.cancelTouch();
  337. // return;
  338. // }
  339. if(this.startTouchFingers==1){
  340. // 单个手指触摸
  341. var touchPt=e.touches[0];
  342. delX=touchPt.clientX-this.touchDownPt.clientX;
  343. delY=touchPt.clientY-this.touchDownPt.clientY;
  344. if(this.sweepDir==0){
  345. var radius=Math.sqrt(delX*delX+delY*delY);
  346. var w=videoElement.offsetWidth,h=videoElement.offsetHeight;
  347. var judge=Math.sqrt(w*w+h*h)/30;
  348. if(radius>judge){
  349. if(Math.abs(delX)>Math.abs(delY)){
  350. if(delX>0)
  351. this.sweepDir=4;
  352. else
  353. this.sweepDir=3;
  354. }else{
  355. if(delY>0)
  356. this.sweepDir=2;
  357. else
  358. this.sweepDir=1;
  359.  
  360. }
  361. // console.log("get sweep dir:"+this.sweepDir);
  362. this.startTouchVideoTime=Math.floor(videoElement.currentTime);
  363. this.startTouchVideoVolume=videoElement.volume;
  364. this.startTouchBrightness=this.videoBrightness;
  365. this.touchStartPt=touchPt;
  366. }
  367. }else if(this.sweepDir==3||this.sweepDir==4){
  368. delX=touchPt.clientX-this.touchStartPt.clientX;
  369. var delXRatio=delX/videoElement.offsetWidth;
  370. if(Math.abs(delXRatio)<0.5){
  371. this.touchResult=Math.floor(delXRatio*180);
  372. }else{
  373. if(delXRatio>0)
  374. this.touchResult=Math.floor((Math.pow(100,delXRatio-0.5)-1)*180+90);
  375. else
  376. this.touchResult=Math.floor(-(Math.pow(100,-delXRatio-0.5)-1)*180-90);
  377. //this.touchResult=Math.floor(Math.pow(2*delX/videoElement.offsetWidth,3)*120);
  378. }
  379. if(this.touchResult+this.startTouchVideoTime<0)
  380. this.touchResult=-this.startTouchVideoTime;
  381. if(this.touchResult+this.startTouchVideoTime>videoElement.duration){
  382. this.touchResult=Math.floor(videoElement.duration-this.startTouchVideoTime)-1;
  383. }
  384. if( (this.sweepDir==3&&this.touchResult>0)||(this.sweepDir==4&&this.touchResult<0)){
  385. this.touchResult=0;
  386. }
  387. var offsetValStr;
  388. if(Math.abs(this.touchResult)<300)
  389. offsetValStr=this.touchResult+"s";
  390. else
  391. offsetValStr=Math.floor(this.touchResult/6)/10+"min";
  392.  
  393. if(this.touchResult>0)
  394. this.setToast(seconds2TimeStr(this.startTouchVideoTime)+" +"+offsetValStr);
  395. else
  396. this.setToast(seconds2TimeStr(this.startTouchVideoTime)+" "+offsetValStr);
  397. // console.log(videoElement);
  398. }else if(this.sweepDir==1||this.sweepDir==2){
  399. if(this.touchStartPt.clientX-this._videoElementAbLeft<this._videoElement.clientWidth/2){
  400. delY=touchPt.clientY-this.touchStartPt.clientY;
  401. var plus=-delY/videoElement.offsetHeight*4;
  402. this.touchResult=this.startTouchBrightness+plus;
  403. if(this.touchResult<0) this.touchResult=0;
  404. else if(this.touchResult>1) this.touchResult=1;
  405. this.videoBrightness=this.touchResult;
  406. var realBrightness=Math.sqrt(this.touchResult)*0.85+0.15;
  407. videoElement.style.filter="brightness("+realBrightness+")";
  408. this.setToast("亮度:"+Math.floor(this.touchResult*100)+"%");
  409. }else{
  410. delY=touchPt.clientY-this.touchStartPt.clientY;
  411. var plus=-delY/videoElement.offsetHeight*4;
  412. this.touchResult=this.startTouchVideoVolume+plus;
  413. if(this.touchResult<0) this.touchResult=0;
  414. else if(this.touchResult>1) this.touchResult=1;
  415. videoElement.volume =this.touchResult;
  416. // if(videoElement.volume!=0)
  417. // videoElement.muted=false;
  418. this.setToast("音量:"+Math.floor(this.touchResult*100)+"%");
  419. }
  420. }
  421.  
  422. //console.log("delx:"+delX);
  423. }else if(this.startTouchFingers==2){
  424. // 2个手指触摸
  425. var touchPt=e.touches[0];
  426. delX=touchPt.clientX-this.touchDownPt.clientX;
  427. delY=touchPt.clientY-this.touchDownPt.clientY;
  428. if(this.sweepDir==0){
  429. var radius=Math.sqrt(delX*delX+delY*delY);
  430. var w=videoElement.offsetWidth,h=videoElement.offsetHeight;
  431. var judge=Math.sqrt(w*w+h*h)/30;
  432. if(radius>judge){
  433. if(Math.abs(delX)>Math.abs(delY)){
  434. this._videoElement.playbackRate=this.originalPlayrate;
  435. if(delX>0)
  436. this.sweepDir=4;
  437. else
  438. this.sweepDir=3;
  439. }else{
  440. if(delY>0)
  441. this.sweepDir=2;
  442. else
  443. this.sweepDir=1;
  444.  
  445. }
  446. // console.log("get sweep dir:"+this.sweepDir);
  447. this.touchStartPt=touchPt;
  448. }
  449. }else if(this.sweepDir==3||this.sweepDir==4){
  450. delX=touchPt.clientX-this.touchStartPt.clientX;
  451. this.touchResult=this.originalPlayrate+Math.floor((delX/videoElement.offsetWidth)*10)*0.25;
  452. if(this.touchResult>4) this.touchResult=4;
  453. if(this.touchResult<0.25) this.touchResult=0.25;
  454. this.setToast("倍速X "+this.touchResult);
  455. }
  456. }
  457. };
  458.  
  459.  
  460. TouchGesture.VideoGesture.prototype.onTouchEnd=function(e){
  461. var videoElement=this._videoElement;
  462. this.touchDownPt=null;
  463. if(this.touchResult!=0){
  464. if(this.startTouchFingers==1){
  465. if(this.sweepDir==3||this.sweepDir==4){
  466. var res=this.startTouchVideoTime+this.touchResult;
  467. // console.log(videoElement.currentTime);
  468. // console.log("touch end:"+res);
  469. videoElement.currentTime=res;
  470. // this.hideToast();
  471. // videoElement.play();
  472. }else if(this.sweepDir==1||this.sweepDir==2){
  473. // this.hideToast();
  474. // videoElement.play();
  475. }
  476. }else if(this.startTouchFingers==2){
  477. if(this.sweepDir==3||this.sweepDir==4){
  478. this._videoElement.playbackRate=this.touchResult;
  479. this.originalPlayrate=this.touchResult;
  480. }
  481. }
  482. }else{
  483.  
  484. }
  485. this.sweepDir=0;
  486. this.touchResult=0;
  487. this._videoElement.playbackRate=this.originalPlayrate;
  488. this.hideToast();
  489. if(this.longTouch==true)
  490. this.simMouseMoveCenter();
  491. this.longTouch=false;
  492. // this.cancelTouch();
  493.  
  494. // this.permitcroll();
  495. };
  496.  
  497. // 启动监听
  498. TouchGesture.VideoGesture.prototype.applyDom=function(videoElement){
  499. this._containElement.appendChild(this._elementFrame);
  500. this._containElement.appendChild(this._unlockBtn);
  501.  
  502. var temp=this._videoElement;
  503. while(temp!=this._eventListenElement){
  504. temp.classList.add("TouchGestureForbidScroll");
  505. temp=temp.parentElement;
  506. }
  507. this._eventListenElement.classList.add("TouchGestureForbidScroll");
  508.  
  509. this._eventListenElement.addEventListener("touchstart",this._touchStartHandler,false);
  510. this._eventListenElement.addEventListener("touchend",this._touchEndHandler,false);
  511. this._eventListenElement.addEventListener("touchmove",this._touchMoveHandler,false);
  512.  
  513.  
  514. };
  515.  
  516. // Resize时恢复元素原样,取消事件监听
  517. TouchGesture.VideoGesture.prototype.restoreDom=function(){
  518. this._containElement.appendChild(this._elementFrame);
  519.  
  520. var temp=this._videoElement;
  521. while(temp!=this._eventListenElement){
  522. if(temp.classList!=null)
  523. temp.classList.remove("TouchGestureForbidScroll");
  524. temp=temp.parentElement;
  525. if(temp == null)
  526. break;
  527. }
  528. if(this._eventListenElement==null)
  529. return;
  530. this._eventListenElement.classList.remove("TouchGestureForbidScroll");
  531.  
  532. this._eventListenElement.removeEventListener("touchstart",this._touchStartHandler);
  533. this._eventListenElement.removeEventListener("touchend",this._touchEndHandler);
  534. this._eventListenElement.removeEventListener("touchmove",this._touchMoveHandler);
  535. };
  536.  
  537. // 窗口resize时检测是否全屏并且适配
  538. TouchGesture.VideoGesture.prototype.fullScreenDetect=function(){
  539. // alert("resize");
  540. this.setUnlockBtnLayout();
  541. var fullScreenState=tg_IsFullscreen();
  542. if(fullScreenState!=this._fullScreenNow){
  543. this._fullScreenNow=fullScreenState;
  544. this.restoreDom();
  545. this.findBestRoot();
  546. this.applyDom();
  547. if(fullScreenState == true){
  548. // console.log("fullscreen");
  549. this._elementFrame.style.position = "fixed";
  550. this.simMouseMoveCenter();
  551. //模拟鼠标移到中间,使得自动隐藏视频底部
  552. }
  553. else{
  554. this._elementFrame.style.position = "absolute";
  555. }
  556. }
  557. if(fullScreenState==true && this.videoInFullscreenElement()){
  558. if(this._videoElement.videoWidth/this._videoElement.videoHeight>1.3){
  559. // console.log("lock");
  560. if(TouchGesture.orientationLocked == false && TouchGesture.ismobile == true){
  561. var self = this;
  562. TouchGesture.orientationLocked = true;
  563. setTimeout(() => {
  564. {
  565. screen.orientation.lock("landscape");
  566. setTimeout(() => {
  567. this._unlockBtn.style.display = "block";
  568. this._unlockBtn.classList.remove("fadeout");
  569. }, 300);
  570. setTimeout(() => {
  571. self._unlockBtn.classList.add("fadeout");
  572. }, 2300);
  573. }
  574. }, 300);
  575. }
  576. // screen.lockOrientationUniversal("landscape-primary");
  577. }
  578. }else if(fullScreenState == false){
  579. TouchGesture.orientationLocked = false;
  580. screen.orientation.unlock();
  581. }
  582. };
  583.  
  584. // TouchGesture.VideoGesture.prototype.onOrientationChange=function(){
  585. // var self = this;
  586.  
  587. // };
  588.  
  589.  
  590. TouchGesture.VideoGesture.prototype.videoInFullscreenElement=function(){
  591. var temp = this._videoElement;
  592. while(temp!=null && temp!=document.body){
  593. if(temp == document.fullscreenElement)
  594. return true;
  595. temp = temp.parentElement;
  596. }
  597. };
  598.  
  599. //自动调节DIV元素位置
  600. TouchGesture.VideoGesture.prototype.setElementLayout=function(){
  601. var videoTarget=this._containElement;
  602. var vw=videoTarget.offsetWidth,vh=videoTarget.offsetHeight;
  603. var w=vw/5;
  604. //var h=vh/8;
  605. var h=w/3;
  606. var x=(vw-w)/2+videoTarget.offsetLeft;
  607. var y=(vh-h)/2+videoTarget.offsetTop;
  608. // console.log("w:"+w," h:"+h+" x:"+x+" y:"+y);
  609. this._elementFrame.style.width=w+"px";
  610. this._elementFrame.style.height=h+"px";
  611. this._elementFrame.style.left=x+"px";
  612. this._elementFrame.style.top=y+"px";
  613. // this._element.style.display="block";
  614. var fontsize=h/3;
  615. this._toastText.style.fontSize=fontsize+"px";
  616. this._toastText.style.marginTop=(h-fontsize)/2+"px";
  617. this._elementFrame.style.display="none";
  618. this._elementFrame.style.borderRadius = w/10 +"px";
  619.  
  620.  
  621. };
  622.  
  623. TouchGesture.VideoGesture.prototype.setUnlockBtnLayout=function(){
  624. var videoTarget=this._containElement;
  625. var vw=window.screen.width,vh=window.screen.height;
  626. var w = vh/8;
  627. var h = vh/8;
  628. var x=(vw-w)/2;
  629. var y=(vh-h)/2+vh/6;
  630.  
  631. this._unlockBtn.style.fontSize=w/2+"px";
  632. this._unlockBtn.style.width=w+"px";
  633. this._unlockBtn.style.height=h+"px";
  634. this._unlockBtn.style.lineHeight = h+"px";
  635. this._unlockBtn.style.left=x+"px";
  636. this._unlockBtn.style.top=y+"px";
  637. this._unlockBtn.style.borderRadius = w/2+"px";
  638. // this._unlockBtn.style.display = "block";
  639. // this._unlockBtn.classList.remove("fadeout");
  640. }
  641.  
  642. //显示Toast
  643. TouchGesture.VideoGesture.prototype.setToast=function(str){
  644. // this._element.style.opacity=0.75;
  645. this._elementFrame.style.display="block";
  646. this._elementFrame.classList.remove("fadeout");
  647. this._toastText.innerHTML=str;
  648. }
  649.  
  650. //在Touchend之前取消手势
  651. TouchGesture.VideoGesture.prototype.cancelTouch=function(){
  652. this.sweepDir=0;
  653. this._videoElement.playbackRate=this.originalPlayrate;
  654. this.touchDownPt=null;
  655. this.hideToast();
  656. }
  657.  
  658. // 隐藏toast
  659. TouchGesture.VideoGesture.prototype.hideToast=function(){
  660. var element=this._elementFrame;
  661. setTimeout(function(){
  662. element.classList.add("fadeout");
  663. },500);
  664. // setTimeout(function(){
  665. // element.style.opacity=0;
  666. // element.classList.remove("fadeout");
  667. // },1500);
  668. }
  669.  
  670. // 检测<video>并插入元素
  671. TouchGesture.VideoGesture.insertDom=function(dom){
  672. var videoTagsNative = dom.getElementsByTagName('video');
  673. // console.log(dom);
  674. Array.prototype.forEach.call(videoTagsNative, function(videoTag) {
  675. if (!videoTag.getAttribute('TouchGesture_Video')) {
  676. videoTag.setAttribute('TouchGesture_Video', true);
  677. new TouchGesture.VideoGesture(videoTag);
  678. // console.log("insert node");
  679. }
  680. });
  681.  
  682. var hostDomain=window.location.host;
  683. if(CustomizedVideoTAG[hostDomain] == null)
  684. return;
  685.  
  686. CustomizedVideoTAG[hostDomain].forEach(function(videoTagName){
  687. var videoTags = dom.getElementsByTagName(videoTagName);
  688. // console.log(dom);
  689. Array.prototype.forEach.call(videoTags, function(videoTag) {
  690. if (!videoTag.getAttribute('TouchGesture_Video')) {
  691. videoTag.setAttribute('TouchGesture_Video', true);
  692. new TouchGesture.VideoGesture(videoTag);
  693. // console.log("insert node");
  694. }
  695. });
  696. });
  697. };
  698.  
  699. TouchGesture.VideoGesture.insertAll=function(){
  700. // var self=this;
  701. TouchGesture.VideoGesture.insertDom(document);
  702.  
  703. };
  704.  
  705. // TouchGesture.VideoGesture.prototype.forbidScroll=function(){
  706. // // var bodies=document.getElementsByTagName("body");
  707. // // Array.prototype.forEach.call(bodies, function(body) {
  708. // // body.style.position="fixed";
  709. // // });
  710. // TouchGesture.forbidScroll=true;
  711. // }
  712.  
  713. // TouchGesture.VideoGesture.prototype.permitcroll=function(){
  714. // // var bodies=document.getElementsByTagName("body");
  715. // // Array.prototype.forEach.call(bodies, function(body) {
  716. // // body.style.position="relative";
  717. // // });
  718. // TouchGesture.forbidScroll=false;
  719. // }
  720.  
  721. function seconds2TimeStr(secs){
  722. var hour=parseInt(secs/3600);
  723. var min=parseInt(secs/60)-60*hour;
  724. var sec=secs%60;
  725. var ret="";
  726. if(hour>0){
  727. ret+=hour+":";
  728. }
  729. ret+=(min < 10? '0' + min : min) + ':' + (sec < 10? '0' + sec : sec);
  730. return ret;
  731. }
  732.  
  733. function initForbidScrollList(){
  734. var hostDomain=window.location.host;
  735. var defaultSetting=TouchGestureWhiteList[hostDomain];
  736. if(defaultSetting!=null){
  737. if(defaultSetting.forbidScrollList!=null)
  738. forbidScrollList=defaultSetting.forbidScrollList;
  739. // console.log(forbidScrollList);
  740. }
  741. }
  742.  
  743. function whetherInBlackList(){
  744. var hostDomain=window.location.host;
  745. if(TouchGestureBlackList.indexOf(hostDomain)>=0){
  746. return true;
  747. }
  748. return false;
  749. }
  750. function tg_IsFullscreen(){
  751. return document.fullscreenElement!=null ||
  752. document.msFullscreenElement!=null ||
  753. document.mozFullScreenElement!=null ||
  754. document.webkitFullscreenElement!=null ||
  755. document.fullscreen == true || false;
  756. }
  757.  
  758. function tg_IsMobile(){
  759. if(window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
  760. return true; // 移动端
  761. }else if(window.screen.height/window.screen.width>1.3){
  762. return true; // 屏幕纵向
  763. }else{
  764. return false; // PC端
  765. }
  766. }
  767.  
  768. (function() {
  769. 'use strict';
  770. GM_addStyle('div.TouchGesture_Toast{ width: 200px; height: 100px; opacity: 0.75; position: absolute; z-index: 2147483648; top: 100px; left: 200px; background-color: black; pointer-events:none;} ');
  771. GM_addStyle('span.TouchGesture_ToastText{ position: absolute; left: 0; right: 0; text-align: center; color: white; pointer-events:none;}');
  772. GM_addStyle('div.TouchGesture_Toast.fadeout{ -webkit-transition: all 1.5s; -moz-transition: all 1.5s; -ms-transition: all 1.5s; -o-transition: all 1.5s; transition: all 1.5s; opacity: 0;}');
  773. GM_addStyle('div.TouchGesture_UnlockBtn{opacity: 0.75; position: fixed; z-index: 2147483648; width: 50px;height: 50px;text-align: center;font-size: 25;margin: 20;background-color: black;color: white;}');
  774. GM_addStyle('div.TouchGesture_UnlockBtn.fadeout{ -webkit-transition: all 1.5s; -moz-transition: all 1.5s; -ms-transition: all 1.5s; -o-transition: all 1.5s; transition: all 1.5s; opacity: 0;}');
  775. if(whetherInBlackList()){
  776. return;
  777. }
  778. initForbidScrollList();
  779.  
  780. TouchGesture.ismobile = tg_IsMobile();
  781. window.addEventListener('resize',function(e){
  782. TouchGesture.ismobile = tg_IsMobile();
  783. });
  784.  
  785. document.addEventListener('touchstart',function(e){
  786. // console.log(e);
  787. // if(e.srcElement.tagName!="VIDEO"
  788. // &&forbidScrollList.indexOf(e.srcElement.classList[0])<0
  789. // &&!e.srcElement.classList.contains("TouchGestureForbidScroll")){
  790. // TouchGesture.forbidScroll=false;
  791. // }else{
  792. // document.addEventListener('touchmove',preventDefault,{passive:false});
  793. // }
  794. if(forbidScrollList.indexOf(e.srcElement.classList[0])>=0){
  795. document.addEventListener('touchmove',preventDefault,{passive:false});
  796. }else{
  797. var noVideo=true;
  798. for(var i=0;i<e.path.length;i++){
  799. var element=e.path[i];
  800. // console.log(element);
  801. if(element.tagName=="VIDEO"||(element.classList&&element.classList.contains["TouchGestureForbidScroll"])){
  802. // TouchGesture.forbidScroll=true;
  803. noVideo=false;
  804. break;
  805. }
  806. }
  807.  
  808. if(!noVideo){
  809. document.addEventListener('touchmove',preventDefault,{passive:false});
  810. }else if(tg_IsFullscreen()){
  811. if(e.touches[0].clientX>document.body.clientWidth/8&&e.touches[0].clientX<document.body.clientWidth){
  812. document.addEventListener('touchmove',preventDefault,{passive:false});
  813. }else{
  814. // TouchGesture.forbidScroll=false;
  815. }
  816. }
  817. else{
  818. // TouchGesture.forbidScroll=false;
  819. }
  820. }
  821. });
  822. function preventDefault(e){
  823. e.preventDefault();
  824. return false;
  825. };
  826. document.addEventListener('touchend',function(e){
  827. document.removeEventListener('touchmove',preventDefault);
  828. });
  829. // document.addEventListener('touchmove',function(e){
  830. // console.log(e.srcElement.classList[0]);
  831. // });
  832. TouchGesture.VideoGesture.insertAll();
  833. setInterval(TouchGesture.VideoGesture.insertAll, 1500);
  834. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址