视频站h5

视频站启用html5播放器

As of 2017-06-16. See the latest version.

  1. // ==UserScript==
  2. // @name 视频站h5
  3. // @description 视频站启用html5播放器
  4. // @version 0.3.1
  5. // @homepage http://bbs.kafan.cn/thread-2093014-1-1.html
  6. // @include *://*.qq.com/*
  7. // @include *://v.youku.com/v_show/id_*
  8. // @include *://video.tudou.com/v/*
  9. // @include *://*.le.com/*.html*
  10. // @include *://*.lesports.com/*.html*
  11. // @include *://tv.sohu.com/*.shtml*
  12. // @include *://m*.tv.sohu.com/*.shtml*
  13. // @include https://www.panda.tv/*
  14. // @exclude https://www.panda.tv/
  15. // @grant unsafeWindow
  16. // @run-at document-start
  17. // @namespace https://greasyfork.org/users/7036-xinggsf
  18. // ==/UserScript==
  19. 'use strict';
  20.  
  21. Object.defineProperty(navigator, 'plugins', {
  22. get() {
  23. return { length: 0 };
  24. }
  25. });
  26.  
  27. let siteFn, v, totalTime,
  28. path = location.pathname;
  29. const stepLen = 5, //快进快退5秒
  30. skipLen = 21, //shift + 快进快退
  31. u = location.hostname,
  32. mDomain = u.split('.').reverse()[1],//主域名
  33.  
  34. $ = id => document.getElementById(id),
  35. q = css => document.querySelector(css),
  36. $C = (name, attr) => {
  37. const el = document.createElement(name);
  38. if (attr) {
  39. for (var i in attr) //用var修正TM的for-in循环BUG
  40. el.setAttribute(i, attr[i]);
  41. }
  42. return el;
  43. },
  44. fakeUA = ua => Object.defineProperty(navigator, 'userAgent', {
  45. value: ua,
  46. writable: false,
  47. configurable: false,
  48. enumerable: true
  49. }),
  50. getAllDuration = css => {
  51. const a = q(css).innerHTML.split(':');
  52. console.log(q(css), a);
  53. let n = a.pop() *1, multiplier = 1;
  54. for (let k of a.reverse()) {
  55. multiplier *= 60;
  56. n += k * multiplier;
  57. }
  58. return n || 2e4;
  59. },
  60. hotKey = function (e) {
  61. //console.log('hotKey', v.seeking, v.seekable);
  62. // 可播放
  63. //if (!v.seekable) return;
  64. //if (v.readyState !== 4) return;
  65. let seekPoint = ~~v.currentTime;
  66. switch (e.keyCode) {
  67. case 32: //space
  68. if (v.paused) v.play();
  69. else v.pause();
  70. e.preventDefault();
  71. //e.stopPropagation();
  72. break;
  73. case 37: //left
  74. seekPoint -= e.shiftKey ? skipLen : stepLen;
  75. if (seekPoint < 0) seekPoint = 0;
  76. v.currentTime = seekPoint;
  77. break;
  78. case 39: //right
  79. seekPoint += e.shiftKey ? skipLen : stepLen;
  80. v.currentTime = seekPoint;
  81. }
  82. },
  83. onCanplay = function (e) {
  84. //v.removeEventListener('oncanplay', onCanplay);
  85. //v.oncanplay = null;//注释应对列表播放而不刷新页面
  86. console.log('脚本[启用html5播放器],事件oncanplay');
  87. const r = path !== location.pathname;//点播了另一个视频
  88. if (totalTime && !r) return;//分段视频返回
  89. if (!totalTime) document.addEventListener('keydown', hotKey, !1);
  90. totalTime = 0;
  91. siteFn && siteFn();
  92. totalTime = totalTime || Math.round(v.duration);
  93. //跳过片头
  94. if (totalTime > 666 && !['youku', 'le', 'lesports'].includes(mDomain))
  95. v.currentTime = 66;
  96. if (r) path = location.pathname;
  97. },
  98. init = () => {
  99. let mo = new MutationObserver(records => {
  100. v = q('video');
  101. if (v) {
  102. //console.log('mo', v.oncanplay);
  103. v.oncanplay = onCanplay;
  104. mo.disconnect();
  105. mo = undefined;
  106. }
  107. });
  108. document.addEventListener('DOMContentLoaded', e => mo.observe(document.body, {
  109. childList : true,
  110. subtree : true
  111. }), !1);
  112. };
  113.  
  114. switch (mDomain) {
  115. case 'qq':
  116. fakeUA('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:48.0) Gecko/20100101 Firefox/48.0');
  117. break;
  118. case 'youku':
  119. sessionStorage.P_l_h5 = 1;
  120. init();
  121. break;
  122. case 'le':
  123. case 'lesports':
  124. fakeUA('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 Version/7.0.3 Safari/7046A194A');
  125. init();
  126. break;
  127. case 'sohu':
  128. //if (window.chrome)
  129. fakeUA('Mozilla/5.0 (Linux; U; Android 4.0.4; GT-I9300 Build/IMM76D) AppleWebKit/534.30 Version/4.0 Mobile Safari/534.30');
  130. // q('meta[name=mobile-agent]').remove();
  131. siteFn = () => {
  132. totalTime = getAllDuration('span.x-duration-txt');
  133. };
  134. /* unsafeWindow.setTimeout( () => {
  135. if (isUseH5Player) {
  136. isUseH5Player = () => 1;
  137. console.log(_videoInfo.videoLength);
  138. }
  139. }, 99); */
  140. init();
  141. break;
  142. case 'tudou':
  143. //fakeUA('Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) Chrome/55.0.10 Mobile');
  144. siteFn = () => {
  145. //获取播放时长
  146. //totalTime = getAllDuration('span.td-h5__player__console__time-total');
  147. totalTime = ~~q('meta[name=duration]').getAttribute('content');
  148. const cur = ~~v.duration +1;
  149. //console.log(cur, totalTime);
  150. if (cur < totalTime) {
  151. //分段播放时,保持播放器原状
  152. q('#td-h5+div').remove();
  153. }
  154. else {
  155. document.body.innerHTML = `<video width="100%" height="100%" autoplay controls src="${v.src}"/>`;
  156. setTimeout(() => {
  157. v = q('video');
  158. if (totalTime > 666) v.currentTime = 66;
  159. //v.oncanplay = onCanplay;
  160. }, 9);
  161. }
  162. };
  163. init();
  164. break;
  165. case 'panda':
  166. localStorage.setItem('panda.tv/user/player', {useH5player: true});
  167. }