iqiyi player switch

iqiyi player switch between flash and html5

当前为 2017-04-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name iqiyi player switch
  3. // @namespace https://gf.qytechs.cn/users/111819-gooyie
  4. // @version 0.1.0
  5. // @description iqiyi player switch between flash and html5
  6. // @author gooyie
  7. // @license MIT License
  8. //
  9. // @include *://www.iqiyi.com/v_*
  10. // @include *://www.iqiyi.com/w_*
  11. // @include *://www.iqiyi.com/dongman/*/*
  12. // @include *://www.iqiyi.com/yinyue/*/*
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_unregisterMenuCommand
  15. // @grant GM_log
  16. // @run-at document-start
  17.  
  18. // ==/UserScript==
  19.  
  20. (function() {
  21. 'use strict';
  22.  
  23. // ua
  24. const UA_CHROME = 'chrome';
  25. const UA_SAFARY = 'safari';
  26. // platform
  27. const PLAFORM_MAC = 'mac';
  28.  
  29. const PLAYER_TYPE = {
  30. Html5VOD: "h5_VOD",
  31. FlashVOD: "flash_VOD"
  32. };
  33.  
  34. class DocCookies {
  35. static get(key) {
  36. let value;
  37. if (new RegExp('^[^\\x00-\\x20\\x7f\\(\\)<>@,;:\\\\\\"\\[\\]\\?=\\{\\}\\/\\u0080-\\uffff]+$').test(key)) {
  38. let re = new RegExp("(^| )" + key + "=([^;]*)(;|$)");
  39. let rs = re.exec(document.cookie);
  40. value = rs ? rs[2] : '';
  41. }
  42. return value ? decodeURIComponent(value) : '';
  43. }
  44.  
  45. static set(k, v, o={}) {
  46. let n = o.expires;
  47. if ("number" == typeof o.expires) {
  48. n = new Date();
  49. n.setTime(n.getTime() + o.expires);
  50. }
  51. let key = k;
  52. let value = encodeURIComponent(v);
  53. let path = o.path ? '; path=' + o.path : '';
  54. let expires = n ? '; expires=' + n.toGMTString() : '';
  55. let domain = o.domain ? '; domain=' + o.domain : '';
  56. document.cookie = `${key}=${value}${path}${expires}${domain}`;
  57. }
  58.  
  59. static remove(k, o={}) {
  60. o.expires = new Date(0);
  61. this.set(k, '', o);
  62. }
  63. }
  64.  
  65. class Switcher {
  66. static switch() {
  67. let currType = DocCookies.get('player_forcedType');
  68. let toType = currType === PLAYER_TYPE.Html5VOD ? PLAYER_TYPE.FlashVOD : PLAYER_TYPE.Html5VOD;
  69.  
  70. GM_log('switching to %s ...', toType);
  71. if (!confirm(`刷新页面切换到${toType}播放器?`)) return;
  72.  
  73. if (toType === PLAYER_TYPE.Html5VOD && !this._canPlayback()) {
  74. alert('╮(╯▽╰)╭ 你的浏览器播放不了html5视频~~~~');
  75. return;
  76. }
  77. // cookie 有效时间为一年
  78. let date = new Date();
  79. date.setFullYear(date.getFullYear() + 1);
  80.  
  81. DocCookies.set('player_forcedType', toType, {expires: date});
  82. document.location.reload();
  83. }
  84.  
  85. static _canPlayback() {
  86. let v = document.createElement('video');
  87. return !!(
  88. v.canPlayType('audio/mp4; codecs="mp4a.40.2"') &&
  89. v.canPlayType('video/mp4; codecs="avc1.640029"') &&
  90. v.canPlayType('video/mp4; codecs="avc1.640029, mp4a.40.2"')
  91. );
  92. }
  93.  
  94. }
  95. // TODO: polyfill使不同的浏览器都能使用vms
  96. class Mocker {
  97. static mock() {
  98. let currType = DocCookies.get('player_forcedType');
  99. if (currType !== PLAYER_TYPE.Html5VOD) return;
  100.  
  101. if (this._canUseVms()) {
  102. // 使用 vms
  103. this._fakeMacPlatform();
  104. this._fakeChrome();
  105. } else if (this._canUseM3u8()) {
  106. // 使用 tmts m3u8
  107. this._fakeMacPlatform();
  108. this._fakeSafary();
  109. }
  110. // 默认使用 tmts mp4 ...
  111. }
  112.  
  113. static _fakeMacPlatform() {
  114. Object.defineProperty(navigator, 'platform', {get: () => PLAFORM_MAC});
  115. }
  116.  
  117. static _fakeSafary() {
  118. Object.defineProperty(navigator, 'userAgent', {get: () => UA_SAFARY});
  119. }
  120.  
  121. static _fakeChrome() {
  122. Object.defineProperty(navigator, 'userAgent', {get: () => UA_CHROME});
  123. }
  124.  
  125. static _canUseVms() {
  126. return !!(
  127. window.MediaSource && window.URL && window.WebSocket && window.ReadableStream &&
  128. (window.RTCSessionDescription || window.webkitRTCSessionDescription) &&
  129. (window.RTCPeerConnection || window.webkitRTCPeerConnection) &&
  130. (window.RTCIceCandidate || window.webkitRTCIceCandidate)
  131. );
  132. }
  133.  
  134. static _canUseM3u8() {
  135. let v = document.createElement('video');
  136. return !!(
  137. v.canPlayType('application/x-mpegurl') &&
  138. v.canPlayType('application/vnd.apple.mpegurl')
  139. );
  140. }
  141. }
  142.  
  143.  
  144. GM_registerMenuCommand('Switch Player', () => Switcher.switch(), null);
  145.  
  146. Mocker.mock();
  147.  
  148. })();

QingJ © 2025

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