iqiyi-player-switch

iqiyi player switch between flash and html5

目前為 2017-06-01 提交的版本,檢視 最新版本

  1. 'use strict';
  2.  
  3. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  4.  
  5. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  6.  
  7. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  8.  
  9. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  10.  
  11. // ==UserScript==
  12. // @name iqiyi-player-switch
  13. // @namespace https://github.com/gooyie/userscript-iqiyi-player-switch
  14. // @homepageURL https://github.com/gooyie/userscript-iqiyi-player-switch
  15. // @supportURL https://github.com/gooyie/userscript-iqiyi-player-switch/issues
  16. // @version 1.8.1
  17. // @description iqiyi player switch between flash and html5
  18. // @author gooyie
  19. // @license MIT License
  20. //
  21. // @include *://*.iqiyi.com/*
  22. // @include *://v.baidu.com/*
  23. // @grant GM_registerMenuCommand
  24. // @grant GM_xmlhttpRequest
  25. // @grant GM_addStyle
  26. // @grant GM_getValue
  27. // @grant GM_setValue
  28. // @grant GM_info
  29. // @grant unsafeWindow
  30. // @connect qiyi.com
  31. // @require https://greasyfork.org/scripts/29319-web-streams-polyfill/code/web-streams-polyfill.js?version=191261
  32. // @require https://greasyfork.org/scripts/29306-fetch-readablestream/code/fetch-readablestream.js?version=191832
  33. // @require https://cdnjs.cloudflare.com/ajax/libs/webrtc-adapter/3.3.4/adapter.min.js
  34. // @require https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.7.0/js/md5.min.js
  35. // @run-at document-start
  36. // ==/UserScript==
  37.  
  38. (function () {
  39. 'use strict';
  40.  
  41. var PLAYER_TYPE = {
  42. Html5VOD: 'h5_VOD',
  43. FlashVOD: 'flash_VOD'
  44. };
  45. /* eslint-disable no-console */
  46.  
  47. var Logger = function () {
  48. function Logger() {
  49. _classCallCheck(this, Logger);
  50. }
  51.  
  52. _createClass(Logger, null, [{
  53. key: 'log',
  54. value: function log() {
  55. var _console;
  56.  
  57. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  58. args[_key] = arguments[_key];
  59. }
  60.  
  61. (_console = console).log.apply(_console, ['%c' + this.tag + '%c' + args.shift(), 'color: green; font-weight: bolder', 'color: blue'].concat(args));
  62. }
  63. }, {
  64. key: 'info',
  65. value: function info() {
  66. var _console2;
  67.  
  68. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  69. args[_key2] = arguments[_key2];
  70. }
  71.  
  72. (_console2 = console).info.apply(_console2, [this.tag + args.shift()].concat(args));
  73. }
  74. }, {
  75. key: 'debug',
  76. value: function debug() {
  77. var _console3;
  78.  
  79. for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  80. args[_key3] = arguments[_key3];
  81. }
  82.  
  83. (_console3 = console).debug.apply(_console3, [this.tag + args.shift()].concat(args));
  84. }
  85. }, {
  86. key: 'warn',
  87. value: function warn() {
  88. var _console4;
  89.  
  90. for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
  91. args[_key4] = arguments[_key4];
  92. }
  93.  
  94. (_console4 = console).warn.apply(_console4, [this.tag + args.shift()].concat(args));
  95. }
  96. }, {
  97. key: 'error',
  98. value: function error() {
  99. var _console5;
  100.  
  101. for (var _len5 = arguments.length, args = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
  102. args[_key5] = arguments[_key5];
  103. }
  104.  
  105. (_console5 = console).error.apply(_console5, [this.tag + args.shift()].concat(args));
  106. }
  107. }, {
  108. key: 'tag',
  109. get: function get() {
  110. return '[' + GM_info.script.name + ']';
  111. }
  112. }]);
  113.  
  114. return Logger;
  115. }();
  116. /* eslint-enable no-console */
  117.  
  118.  
  119. var Cookies = function () {
  120. function Cookies() {
  121. _classCallCheck(this, Cookies);
  122. }
  123.  
  124. _createClass(Cookies, null, [{
  125. key: 'get',
  126. value: function get(key) {
  127. var value = void 0;
  128. if (new RegExp('^[^\\x00-\\x20\\x7f\\(\\)<>@,;:\\\\\\"\\[\\]\\?=\\{\\}\\/\\u0080-\\uffff]+$').test(key)) {
  129. // eslint-disable-line no-control-regex
  130. var re = new RegExp('(^| )' + key + '=([^;]*)(;|$)');
  131. var rs = re.exec(document.cookie);
  132. value = rs ? rs[2] : '';
  133. }
  134. return value ? decodeURIComponent(value) : '';
  135. }
  136. }, {
  137. key: 'set',
  138. value: function set(k, v) {
  139. var o = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  140.  
  141. var n = o.expires;
  142. if ('number' == typeof o.expires) {
  143. n = new Date();
  144. n.setTime(n.getTime() + o.expires);
  145. }
  146. var key = k;
  147. var value = encodeURIComponent(v);
  148. var path = o.path ? '; path=' + o.path : '';
  149. var expires = n ? '; expires=' + n.toGMTString() : '';
  150. var domain = o.domain ? '; domain=' + o.domain : '';
  151. document.cookie = key + '=' + value + path + expires + domain;
  152. }
  153. }, {
  154. key: 'remove',
  155. value: function remove(k) {
  156. var o = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  157.  
  158. o.expires = new Date(0);
  159. this.set(k, '', o);
  160. }
  161. }]);
  162.  
  163. return Cookies;
  164. }();
  165.  
  166. var Detector = function () {
  167. function Detector() {
  168. _classCallCheck(this, Detector);
  169. }
  170.  
  171. _createClass(Detector, null, [{
  172. key: 'isSupportHtml5',
  173. value: function isSupportHtml5() {
  174. var v = document.createElement('video');
  175. return !!(v.canPlayType('audio/mp4; codecs="mp4a.40.2"') && v.canPlayType('video/mp4; codecs="avc1.640029"') && v.canPlayType('video/mp4; codecs="avc1.640029, mp4a.40.2"'));
  176. }
  177. }, {
  178. key: 'isSupportVms',
  179. value: function isSupportVms() {
  180. return !!(window.MediaSource && window.URL && window.WebSocket && window.ReadableStream && (window.RTCSessionDescription || window.webkitRTCSessionDescription) && (window.RTCPeerConnection || window.webkitRTCPeerConnection) && (window.RTCIceCandidate || window.webkitRTCIceCandidate));
  181. }
  182. }, {
  183. key: 'isSupportM3u8',
  184. value: function isSupportM3u8() {
  185. var v = document.createElement('video');
  186. return !!(v.canPlayType('application/x-mpegurl') && v.canPlayType('application/vnd.apple.mpegurl'));
  187. }
  188. }, {
  189. key: 'isChrome',
  190. value: function isChrome() {
  191. return (/chrome/i.test(navigator.userAgent)
  192. );
  193. }
  194. }, {
  195. key: 'isFirefox',
  196. value: function isFirefox() {
  197. return (/firefox/i.test(navigator.userAgent)
  198. );
  199. }
  200. }, {
  201. key: 'isEdge',
  202. value: function isEdge() {
  203. return (/edge/i.test(navigator.userAgent)
  204. );
  205. }
  206. }, {
  207. key: 'isInnerFrame',
  208. value: function isInnerFrame() {
  209. return window.top !== window.self;
  210. }
  211. }]);
  212.  
  213. return Detector;
  214. }();
  215.  
  216. var Hooker = function () {
  217. function Hooker() {
  218. _classCallCheck(this, Hooker);
  219. }
  220.  
  221. _createClass(Hooker, null, [{
  222. key: 'hookCall',
  223. value: function hookCall() {
  224. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  225.  
  226. var call = Function.prototype.call;
  227. Function.prototype.call = function () {
  228. for (var _len6 = arguments.length, args = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
  229. args[_key6] = arguments[_key6];
  230. }
  231.  
  232. var ret = call.apply(this, args);
  233. try {
  234. if (args) cb.apply(undefined, args);
  235. } catch (err) {
  236. Logger.error(err.stack);
  237. }
  238. return ret;
  239. };
  240.  
  241. Function.prototype.call.toString = Function.prototype.call.toLocaleString = function () {
  242. return 'function call() { [native code] }';
  243. };
  244. }
  245. }, {
  246. key: '_isFactoryCall',
  247. value: function _isFactoryCall(args) {
  248. // module.exports, module, module.exports, require
  249. return args.length === 4 && args[1] instanceof Object && args[1].hasOwnProperty('exports');
  250. }
  251. }, {
  252. key: 'hookFactoryCall',
  253. value: function hookFactoryCall() {
  254. var _this = this;
  255.  
  256. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  257.  
  258. this.hookCall(function () {
  259. for (var _len7 = arguments.length, args = Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
  260. args[_key7] = arguments[_key7];
  261. }
  262.  
  263. if (_this._isFactoryCall(args)) cb.apply(undefined, args);
  264. });
  265. }
  266. }, {
  267. key: '_isJqueryFactoryCall',
  268. value: function _isJqueryFactoryCall(exports) {
  269. return exports.hasOwnProperty('fn') && exports.fn.hasOwnProperty('jquery');
  270. }
  271. }, {
  272. key: 'hookJquery',
  273. value: function hookJquery() {
  274. var _this2 = this;
  275.  
  276. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  277.  
  278. this.hookFactoryCall(function () {
  279. for (var _len8 = arguments.length, args = Array(_len8), _key8 = 0; _key8 < _len8; _key8++) {
  280. args[_key8] = arguments[_key8];
  281. }
  282.  
  283. if (_this2._isJqueryFactoryCall(args[1].exports)) cb(args[1].exports);
  284. });
  285. }
  286. }, {
  287. key: 'hookJqueryAjax',
  288. value: function hookJqueryAjax() {
  289. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  290.  
  291. this.hookJquery(function (exports) {
  292. var ajax = exports.ajax.bind(exports);
  293.  
  294. exports.ajax = function (url) {
  295. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  296.  
  297. if ((typeof url === 'undefined' ? 'undefined' : _typeof(url)) === 'object') {
  298. var _ref = [url.url, url];
  299. url = _ref[0];
  300. options = _ref[1];
  301. }
  302.  
  303. var isHijacked = cb(url, options);
  304. if (isHijacked) return;
  305.  
  306. return ajax(url, options);
  307. };
  308. });
  309. }
  310. }, {
  311. key: '_isHttpFactoryCall',
  312. value: function _isHttpFactoryCall() {
  313. var exports = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  314.  
  315. return exports.hasOwnProperty('jsonp') && exports.hasOwnProperty('ajax');
  316. }
  317. }, {
  318. key: 'hookHttp',
  319. value: function hookHttp() {
  320. var _this3 = this;
  321.  
  322. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  323.  
  324. this.hookFactoryCall(function () {
  325. for (var _len9 = arguments.length, args = Array(_len9), _key9 = 0; _key9 < _len9; _key9++) {
  326. args[_key9] = arguments[_key9];
  327. }
  328.  
  329. if (_this3._isHttpFactoryCall(args[1].exports)) cb(args[1].exports);
  330. });
  331. }
  332. }, {
  333. key: 'hookHttpJsonp',
  334. value: function hookHttpJsonp() {
  335. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  336.  
  337. this.hookHttp(function (exports) {
  338. var jsonp = exports.jsonp.bind(exports);
  339.  
  340. exports.jsonp = function (options) {
  341. var isHijacked = cb(options);
  342. if (isHijacked) return;
  343. return jsonp(options);
  344. };
  345. });
  346. }
  347. }, {
  348. key: '_isLogoFactoryCall',
  349. value: function _isLogoFactoryCall() {
  350. var exports = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  351.  
  352. return 'function' === typeof exports && exports.prototype.hasOwnProperty('showLogo');
  353. }
  354. }, {
  355. key: 'hookLogo',
  356. value: function hookLogo() {
  357. var _this4 = this;
  358.  
  359. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  360.  
  361. this.hookFactoryCall(function () {
  362. for (var _len10 = arguments.length, args = Array(_len10), _key10 = 0; _key10 < _len10; _key10++) {
  363. args[_key10] = arguments[_key10];
  364. }
  365.  
  366. if (_this4._isLogoFactoryCall(args[1].exports)) cb(args[1].exports);
  367. });
  368. }
  369. }, {
  370. key: '_isFullScreenFactoryCall',
  371. value: function _isFullScreenFactoryCall() {
  372. var exports = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  373.  
  374. return exports.__proto__ && exports.__proto__.hasOwnProperty('isFullScreen');
  375. }
  376. }, {
  377. key: 'hookFullScreen',
  378. value: function hookFullScreen() {
  379. var _this5 = this;
  380.  
  381. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  382.  
  383. this.hookFactoryCall(function () {
  384. for (var _len11 = arguments.length, args = Array(_len11), _key11 = 0; _key11 < _len11; _key11++) {
  385. args[_key11] = arguments[_key11];
  386. }
  387.  
  388. if (_this5._isFullScreenFactoryCall(args[1].exports)) cb(args[1].exports);
  389. });
  390. }
  391. }, {
  392. key: '_isWebFullScreenFactoryCall',
  393. value: function _isWebFullScreenFactoryCall() {
  394. var exports = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  395.  
  396. return exports.__proto__ && exports.__proto__.hasOwnProperty('isWebFullScreen');
  397. }
  398. }, {
  399. key: 'hookWebFullScreen',
  400. value: function hookWebFullScreen() {
  401. var _this6 = this;
  402.  
  403. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  404.  
  405. this.hookFactoryCall(function () {
  406. for (var _len12 = arguments.length, args = Array(_len12), _key12 = 0; _key12 < _len12; _key12++) {
  407. args[_key12] = arguments[_key12];
  408. }
  409.  
  410. if (_this6._isWebFullScreenFactoryCall(args[1].exports)) cb(args[1].exports);
  411. });
  412. }
  413. }, {
  414. key: '_isPluginControlsFactoryCall',
  415. value: function _isPluginControlsFactoryCall() {
  416. var exports = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  417.  
  418. return 'function' === typeof exports && exports.prototype.hasOwnProperty('initFullScreen');
  419. }
  420. }, {
  421. key: 'hookPluginControls',
  422. value: function hookPluginControls() {
  423. var _this7 = this;
  424.  
  425. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  426.  
  427. this.hookFactoryCall(function () {
  428. for (var _len13 = arguments.length, args = Array(_len13), _key13 = 0; _key13 < _len13; _key13++) {
  429. args[_key13] = arguments[_key13];
  430. }
  431.  
  432. if (_this7._isPluginControlsFactoryCall(args[1].exports)) cb(args[1].exports);
  433. });
  434. }
  435. }, {
  436. key: '_isCoreFactoryCall',
  437. value: function _isCoreFactoryCall() {
  438. var exports = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  439.  
  440. return 'function' === typeof exports && exports.prototype.hasOwnProperty('getdefaultvds') && exports.prototype.hasOwnProperty('getMovieInfo');
  441. }
  442. }, {
  443. key: 'hookCore',
  444. value: function hookCore() {
  445. var _this8 = this;
  446.  
  447. var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  448.  
  449. this.hookFactoryCall(function () {
  450. for (var _len14 = arguments.length, args = Array(_len14), _key14 = 0; _key14 < _len14; _key14++) {
  451. args[_key14] = arguments[_key14];
  452. }
  453.  
  454. if (_this8._isCoreFactoryCall(args[1].exports)) cb(args[1].exports);
  455. });
  456. }
  457. }]);
  458.  
  459. return Hooker;
  460. }();
  461.  
  462. var Faker = function () {
  463. function Faker() {
  464. _classCallCheck(this, Faker);
  465. }
  466.  
  467. _createClass(Faker, null, [{
  468. key: 'fakeMacPlatform',
  469. value: function fakeMacPlatform() {
  470. var PLAFORM_MAC = 'mac';
  471. Object.defineProperty(unsafeWindow.navigator, 'platform', { get: function get() {
  472. return PLAFORM_MAC;
  473. } });
  474. }
  475. }, {
  476. key: 'fakeSafari',
  477. value: function fakeSafari() {
  478. var UA_SAFARY = 'safari';
  479. Object.defineProperty(unsafeWindow.navigator, 'userAgent', { get: function get() {
  480. return UA_SAFARY;
  481. } });
  482. }
  483. }, {
  484. key: 'fakeChrome',
  485. value: function fakeChrome() {
  486. var UA_CHROME = 'chrome';
  487. Object.defineProperty(unsafeWindow.navigator, 'userAgent', { get: function get() {
  488. return UA_CHROME;
  489. } });
  490. }
  491. }, {
  492. key: '_calcSign',
  493. value: function _calcSign(authcookie) {
  494. var RESPONSE_KEY = '-0J1d9d^ESd)9jSsja';
  495. return md5(authcookie.substring(5, 39).split('').reverse().join('') + '<1<' + RESPONSE_KEY);
  496. }
  497. }, {
  498. key: 'fakeVipRes',
  499. value: function fakeVipRes(authcookie) {
  500. var json = {
  501. code: 'A00000',
  502. data: {
  503. sign: this._calcSign(authcookie)
  504. }
  505. };
  506. return json;
  507. }
  508. }, {
  509. key: 'fakeAdRes',
  510. value: function fakeAdRes() {
  511. var json = {};
  512. return json;
  513. }
  514. }, {
  515. key: 'fakePassportCookie',
  516. value: function fakePassportCookie() {
  517. Cookies.set('P00001', 'faked_passport', { domain: '.iqiyi.com' });
  518. Logger.log('faked passport cookie');
  519. }
  520. }]);
  521.  
  522. return Faker;
  523. }();
  524.  
  525. var Mocker = function () {
  526. function Mocker() {
  527. _classCallCheck(this, Mocker);
  528. }
  529.  
  530. _createClass(Mocker, null, [{
  531. key: 'mock',
  532. value: function mock() {
  533. this.mockForBestDefintion();
  534. this.mockAd();
  535. this.mockVip();
  536. this.mockLogo();
  537. }
  538. }, {
  539. key: 'mockToUseVms',
  540. value: function mockToUseVms() {
  541. Faker.fakeChrome();
  542. }
  543. }, {
  544. key: 'mockToUseM3u8',
  545. value: function mockToUseM3u8() {
  546. Faker.fakeMacPlatform();
  547. Faker.fakeSafari();
  548. }
  549. }, {
  550. key: '_isVideoReq',
  551. value: function _isVideoReq(url) {
  552. return (/^https?:\/\/(?:\d+.?){4}\/videos\/v.*$/.test(url)
  553. );
  554. }
  555. }, {
  556. key: 'mockForBestDefintion',
  557. value: function mockForBestDefintion() {
  558. var _this9 = this;
  559.  
  560. // apply shims
  561. if (Detector.isFirefox()) {
  562. var fetch = unsafeWindow.fetch.bind(unsafeWindow);
  563.  
  564. unsafeWindow.fetch = function (url, opts) {
  565. if (_this9._isVideoReq(url)) {
  566. Logger.log('fetching stream ' + url);
  567. return fetchStream(url, opts); // xhr with moz-chunked-arraybuffer
  568. } else {
  569. return fetch(url, opts);
  570. }
  571. };
  572. } else if (Detector.isEdge()) {
  573. // export to the global window object
  574. unsafeWindow.RTCIceCandidate = window.RTCIceCandidate;
  575. unsafeWindow.RTCPeerConnection = window.RTCPeerConnection;
  576. unsafeWindow.RTCSessionDescription = window.RTCSessionDescription;
  577. }
  578. // auto fall-back
  579. if (Detector.isSupportVms()) {
  580. if (!Detector.isChrome()) this.mockToUseVms(); // vms, 1080p or higher
  581. } else if (Detector.isSupportM3u8()) {
  582. this.mockToUseM3u8(); // tmts m3u8
  583. } else {
  584. // by default, tmts mp4 ...
  585. }
  586. }
  587. }, {
  588. key: '_isAdReq',
  589. value: function _isAdReq(url) {
  590. var AD_URL = 'http://t7z.cupid.iqiyi.com/show2';
  591. return url.indexOf(AD_URL) === 0;
  592. }
  593. }, {
  594. key: 'mockAd',
  595. value: function mockAd() {
  596. var _this10 = this;
  597.  
  598. Hooker.hookJqueryAjax(function (url, options) {
  599. if (_this10._isAdReq(url)) {
  600. var res = Faker.fakeAdRes();
  601. (options.complete || options.success)({ responseJSON: res }, 'success');
  602. Logger.log('mocked ad request ' + url);
  603. return true;
  604. }
  605. });
  606. }
  607. }, {
  608. key: '_isCheckVipReq',
  609. value: function _isCheckVipReq(url) {
  610. var CHECK_VIP_URL = 'https://cmonitor.iqiyi.com/apis/user/check_vip.action';
  611. return url === CHECK_VIP_URL;
  612. }
  613. }, {
  614. key: '_isLogin',
  615. value: function _isLogin() {
  616. return !!Cookies.get('P00001');
  617. }
  618. }, {
  619. key: 'mockVip',
  620. value: function mockVip() {
  621. var _this11 = this;
  622.  
  623. if (!this._isLogin()) Faker.fakePassportCookie();
  624.  
  625. Hooker.hookHttpJsonp(function (options) {
  626. var url = options.url;
  627.  
  628. if (_this11._isCheckVipReq(url)) {
  629. var res = Faker.fakeVipRes(options.params.authcookie);
  630. options.success(res);
  631. Logger.log('mocked check vip request ' + url);
  632. return true;
  633. }
  634. });
  635. }
  636. }, {
  637. key: 'mockLogo',
  638. value: function mockLogo() {
  639. Hooker.hookLogo(function (exports) {
  640. return exports.prototype.showLogo = function () {};
  641. });
  642. }
  643. }]);
  644.  
  645. return Mocker;
  646. }();
  647.  
  648. var Switcher = function () {
  649. function Switcher() {
  650. _classCallCheck(this, Switcher);
  651. }
  652.  
  653. _createClass(Switcher, null, [{
  654. key: 'switchTo',
  655. value: function switchTo(toType) {
  656. Logger.log('switching to ' + toType + ' ...');
  657.  
  658. GM_setValue('player_forcedType', toType);
  659. document.location.reload();
  660. }
  661. }]);
  662.  
  663. return Switcher;
  664. }();
  665.  
  666. var Finder = function () {
  667. function Finder() {
  668. _classCallCheck(this, Finder);
  669. }
  670.  
  671. _createClass(Finder, null, [{
  672. key: 'findVid',
  673. value: function findVid(text) {
  674. var result = /vid=([\da-z]+)/.exec(text);
  675. return result ? result[1] : null;
  676. }
  677. }, {
  678. key: 'findTvid',
  679. value: function findTvid(text) {
  680. var result = /tvId=(\d+)/.exec(text);
  681. return result ? result[1] : null;
  682. }
  683. }, {
  684. key: 'findEmbedNodes',
  685. value: function findEmbedNodes() {
  686. var nodes = document.querySelectorAll('object, embed');
  687. return nodes.length > 0 ? nodes : null;
  688. }
  689. }]);
  690.  
  691. return Finder;
  692. }();
  693.  
  694. function getVideoUrl(tvid, vid) {
  695. return new Promise(function (resolve, reject) {
  696. GM_xmlhttpRequest({
  697. url: 'http://cache.video.qiyi.com/jp/vi/' + tvid + '/' + vid + '/?callback=callback',
  698. method: 'GET',
  699. timeout: 8e3,
  700. onload: function onload(details) {
  701. try {
  702. var json = JSON.parse(/callback\s*\(\s*(\{.*\})\s*\)/.exec(details.responseText)[1]);
  703. resolve(json.vu);
  704. } catch (err) {
  705. reject(err);
  706. }
  707. },
  708. onerror: reject,
  709. onabort: reject,
  710. ontimeout: reject
  711. });
  712. });
  713. }
  714.  
  715. function embedSrc(targetNode, _ref2) {
  716. var tvid = _ref2.tvid,
  717. vid = _ref2.vid;
  718.  
  719. targetNode.innerHTML = '<div class="' + GM_info.script.name + ' info">\u6B63\u5728\u83B7\u53D6\u89C6\u9891\u6E90...</div>';
  720.  
  721. getVideoUrl(tvid, vid).then(function (url) {
  722. targetNode.innerHTML = '<iframe id="innerFrame" src="' + url + '" frameborder="0" allowfullscreen="true" width="100%" height="100%"></iframe>';
  723. }).catch(function (err) {
  724. targetNode.innerHTML = '<div class="' + GM_info.script.name + ' error"><p>\u83B7\u53D6\u89C6\u9891\u6E90\u51FA\u9519\uFF01</p><p>' + err.message + '</p></div>';
  725. });
  726. }
  727.  
  728. function replaceFlash() {
  729. var nodes = Finder.findEmbedNodes();
  730. if (!nodes) return;
  731.  
  732. var _iteratorNormalCompletion = true;
  733. var _didIteratorError = false;
  734. var _iteratorError = undefined;
  735.  
  736. try {
  737. for (var _iterator = nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  738. var node = _step.value;
  739.  
  740. var text = node.outerHTML;
  741.  
  742. var vid = Finder.findVid(text);
  743. var tvid = Finder.findTvid(text);
  744.  
  745. if (tvid && vid) {
  746. embedSrc(node.parentNode, { tvid: tvid, vid: vid });
  747. }
  748. }
  749. } catch (err) {
  750. _didIteratorError = true;
  751. _iteratorError = err;
  752. } finally {
  753. try {
  754. if (!_iteratorNormalCompletion && _iterator.return) {
  755. _iterator.return();
  756. }
  757. } finally {
  758. if (_didIteratorError) {
  759. throw _iteratorError;
  760. }
  761. }
  762. }
  763. }
  764.  
  765. function adapteIframe() {
  766. var style = '\n body[class|="qypage"] {\n overflow: hidden !important;\n background: #000 !important;\n visibility: hidden;\n }\n\n .mod-func {\n display: none !important;\n }\n\n .' + GM_info.script.name + '.info {\n width: 20em;\n height: 5em;\n position: absolute;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n margin: auto;\n text-align: center;\n line-height: 5em;\n font-size: 1em;\n color: #ccc;\n }\n\n .' + GM_info.script.name + '.error {\n height: 3em;\n position: absolute;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n margin: auto;\n text-align: center;\n font-size: 1em;\n color: #c00;\n }\n ';
  767.  
  768. GM_addStyle(style);
  769.  
  770. Hooker.hookWebFullScreen(function (exports) {
  771. var init = exports.__proto__.init;
  772. exports.__proto__.init = function (wrapper, btn) {
  773. init.apply(this, [wrapper, btn]);
  774. this.enter();
  775.  
  776. btn[0].style.display = 'none';
  777. document.body.style.visibility = 'visible';
  778. };
  779.  
  780. exports.__proto__.exit = function () {};
  781. });
  782.  
  783. Hooker.hookCore(function (exports) {
  784. exports.prototype.hasNextVideo = function () {
  785. return null;
  786. };
  787. });
  788. }
  789.  
  790. function forceHtml5() {
  791. Logger.log('setting player_forcedType cookie as ' + PLAYER_TYPE.Html5VOD);
  792. Cookies.set('player_forcedType', PLAYER_TYPE.Html5VOD, { domain: '.iqiyi.com' });
  793. }
  794.  
  795. function forceFlash() {
  796. Logger.log('setting player_forcedType cookie as ' + PLAYER_TYPE.FlashVOD);
  797. Cookies.set('player_forcedType', PLAYER_TYPE.FlashVOD, { domain: '.iqiyi.com' });
  798. }
  799.  
  800. function clean() {
  801. Cookies.remove('player_forcedType', { domain: '.iqiyi.com' });
  802. if (Cookies.get('P00001') === 'faked_passport') Cookies.remove('P00001', { domain: '.iqiyi.com' });
  803. Logger.log('removed cookies.');
  804. }
  805.  
  806. function registerMenu() {
  807. var MENU_NAME = {
  808. HTML5: 'HTML5播放器',
  809. FLASH: 'Flash播放器'
  810. };
  811.  
  812. var currType = GM_getValue('player_forcedType', PLAYER_TYPE.Html5VOD); // 默认为Html5播放器,免去切换。
  813.  
  814. var _ref3 = currType === PLAYER_TYPE.Html5VOD ? [PLAYER_TYPE.FlashVOD, MENU_NAME.FLASH] : [PLAYER_TYPE.Html5VOD, MENU_NAME.HTML5],
  815. _ref4 = _slicedToArray(_ref3, 2),
  816. toType = _ref4[0],
  817. name = _ref4[1];
  818.  
  819. GM_registerMenuCommand(name, function () {
  820. return Switcher.switchTo(toType);
  821. }, null);
  822. Logger.log('registered menu.');
  823. }
  824.  
  825. //=============================================================================
  826.  
  827. registerMenu();
  828.  
  829. var currType = GM_getValue('player_forcedType', PLAYER_TYPE.Html5VOD);
  830. if (currType === PLAYER_TYPE.Html5VOD) {
  831. if (!Detector.isSupportHtml5()) {
  832. alert('╮(╯▽╰)╭ 你的浏览器播放不了html5视频~~~~');
  833. return;
  834. }
  835.  
  836. forceHtml5();
  837. Mocker.mock();
  838.  
  839. if (Detector.isInnerFrame()) adapteIframe();
  840. document.addEventListener('DOMContentLoaded', function () {
  841. return replaceFlash();
  842. });
  843. } else {
  844. forceFlash();
  845. }
  846.  
  847. window.addEventListener('unload', function () {
  848. return clean();
  849. });
  850. })();