HTML5 Video Player Enhance

To enhance the functionality of HTML5 Video Player (h5player) supporting all websites using shortcut keys similar to PotPlayer.

当前为 2021-06-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name HTML5 Video Player Enhance
  3. // @version 2.9.3
  4. // @description To enhance the functionality of HTML5 Video Player (h5player) supporting all websites using shortcut keys similar to PotPlayer.
  5. // @author CY Fung
  6. // @match http://*/*
  7. // @match https://*/*
  8. // @run-at document-start
  9. // @require https://cdnjs.cloudflare.com/ajax/libs/js-sha256/0.9.0/sha256.min.js
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/mathjs/9.3.2/math.js
  11. // @namespace https://gf.qytechs.cn/users/371179
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // ==/UserScript==
  15.  
  16.  
  17. /**
  18. * Remarks
  19. * This script support modern browser only with ES6+.
  20. * fullscreen and pointerLock buggy in shadowRoot
  21. * Space Pause not success
  22. * shift F key issue
  23. **/
  24. (function $$() {
  25. 'use strict';
  26.  
  27. if (!document || !document.documentElement) return window.requestAnimationFrame($$);
  28.  
  29. let _debug_h5p_logging_ = false;
  30.  
  31. try {
  32. _debug_h5p_logging_ = +window.localStorage.getItem('_h5_player_sLogging_') > 0
  33. } catch (e) {}
  34.  
  35.  
  36.  
  37. const SHIFT = 1;
  38. const CTRL = 2;
  39. const ALT = 4;
  40. const TERMINATE = 0x842;
  41. const _sVersion_ = 1817;
  42. const str_postMsgData = '__postMsgData__'
  43. const DOM_ACTIVE_FOUND = 1;
  44. const DOM_ACTIVE_SRC_LOADED = 2;
  45. const DOM_ACTIVE_ONCE_PLAYED = 4;
  46. const DOM_ACTIVE_MOUSE_CLICK = 8;
  47. const DOM_ACTIVE_MOUSE_IN = 16;
  48. const DOM_ACTIVE_DELAYED_PAUSED = 32;
  49. const DOM_ACTIVE_INVALID_PARENT = 2048;
  50.  
  51. var console = {};
  52.  
  53. console.log = function() {
  54. window.console.log(...['[h5p]', ...arguments])
  55. }
  56. console.error = function() {
  57. window.console.error(...['[h5p]', ...arguments])
  58. }
  59.  
  60.  
  61. let _endlessloop = null;
  62. const isIframe = (window.top !== window.self && window.top && window.self);
  63. const shadowRoots = [];
  64.  
  65. const getRoot = (elm) => elm.getRootNode instanceof Function ? elm.getRootNode() : (elm.ownerDocument || null);
  66.  
  67. const isShadowRoot = (elm) => (elm && ('host' in elm)) ? elm.nodeType == 11 && !!elm.host && elm.host.nodeType == 1 : null; //instanceof ShadowRoot
  68.  
  69. const playerConfs = {}
  70.  
  71. const hanlderResizeVideo = (entries) => {
  72. const detected_changes = {};
  73. for (let entry of entries) {
  74. const player = entry.target.nodeName == "VIDEO" ? entry.target : entry.target.querySelector("VIDEO[_h5ppid]");
  75. if (!player) continue;
  76. const vpid = player.getAttribute('_h5ppid');
  77. if (!vpid) continue;
  78. if (vpid in detected_changes) continue;
  79. detected_changes[vpid] = true;
  80. const wPlayer = $hs.getPlayerBlockElement(player, true)
  81. if (!wPlayer) continue;
  82. const layoutBox = wPlayer.parentNode
  83. if (!layoutBox) continue;
  84. const tipsDom = layoutBox.querySelector('[_potTips_]');
  85. if (!tipsDom) continue;
  86.  
  87. $hs.fixNonBoxingVideoTipsPosition(tipsDom, player);
  88. window.requestAnimationFrame(() => $hs.fixNonBoxingVideoTipsPosition(tipsDom, player))
  89.  
  90. }
  91. };
  92.  
  93. const $mb = {
  94.  
  95.  
  96. nightly_isSupportQueueMicrotask: function() {
  97.  
  98. if ('_isSupportQueueMicrotask' in $mb) return $mb._isSupportQueueMicrotask;
  99.  
  100. $mb._isSupportQueueMicrotask = false;
  101. $mb.queueMicrotask = window.queueMicrotask;
  102. if (typeof $mb.queueMicrotask == 'function') {
  103. $mb._isSupportQueueMicrotask = true;
  104. }
  105.  
  106. return $mb._isSupportQueueMicrotask;
  107.  
  108. },
  109.  
  110. stable_isSupportAdvancedEventListener: function() {
  111.  
  112. if ('_isSupportAdvancedEventListener' in $mb) return $mb._isSupportAdvancedEventListener
  113. let prop = 0;
  114. document.createAttribute('z').addEventListener('', null, {
  115. get passive() {
  116. prop++;
  117. },
  118. get once() {
  119. prop++;
  120. }
  121. });
  122. return ($mb._isSupportAdvancedEventListener = (prop == 2));
  123. }
  124.  
  125. }
  126.  
  127.  
  128. const $ws = {
  129. requestAnimationFrame,
  130. cancelAnimationFrame,
  131. MutationObserver
  132. }
  133. //throw Error if your browser is too outdated. (eg ES6 script, no such window object)
  134.  
  135. Element.prototype.__matches__ = (Element.prototype.matches || Element.prototype.matchesSelector ||
  136. Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector ||
  137. Element.prototype.oMatchesSelector || Element.prototype.webkitMatchesSelector ||
  138. Element.prototype.matches()); // throw Error if not supported
  139.  
  140.  
  141. Element.prototype.__requestPointerLock__ = (Element.prototype.requestPointerLock ||
  142. Element.prototype.mozRequestPointerLock || Element.prototype.webkitRequestPointerLock || function() {});
  143.  
  144. // Ask the browser to release the pointer
  145. Document.prototype.__exitPointerLock__ = (Document.prototype.exitPointerLock ||
  146. Document.prototype.mozExitPointerLock || Document.prototype.webkitExitPointerLock || function() {});
  147.  
  148. // built-in hash - https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
  149. async function digestMessage(message) {
  150. return window.sha256(message)
  151. }
  152.  
  153. const dround = (x) => ~~(x + .5);
  154.  
  155. const jsonStringify_replacer = function(key, val) {
  156. if (val && (val instanceof Element || val instanceof Document)) return val.toString();
  157. return val; // return as is
  158. };
  159.  
  160. const jsonParse = function() {
  161. try {
  162. return JSON.parse.apply(this, arguments)
  163. } catch (e) {}
  164. return null;
  165. }
  166. const jsonStringify = function(obj) {
  167. try {
  168. return JSON.stringify.call(this, obj, jsonStringify_replacer)
  169. } catch (e) {}
  170. return null;
  171. }
  172.  
  173. function _postMsg() {
  174. //async is needed. or error handling for postMessage
  175. const [win, tag, ...data] = arguments;
  176. if (typeof tag == 'string') {
  177. let postMsgObj = {
  178. tag,
  179. passing: true,
  180. winOrder: _postMsg.a
  181. }
  182. try {
  183. let k = 'msg-' + (+new Date)
  184. win.document[str_postMsgData] = win.document[str_postMsgData] || {}
  185. win.document[str_postMsgData][k] = data; //direct
  186. postMsgObj.str = k;
  187. postMsgObj.stype = 1;
  188. } catch (e) {}
  189. if (!postMsgObj.stype) {
  190. postMsgObj.str = jsonStringify({
  191. d: data
  192. })
  193. if (postMsgObj.str && postMsgObj.str.length) postMsgObj.stype = 2;
  194. }
  195. if (!postMsgObj.stype) {
  196. postMsgObj.str = "" + data;
  197. postMsgObj.stype = 0;
  198. }
  199. win.postMessage(postMsgObj, '*');
  200. }
  201.  
  202. }
  203.  
  204. function postMsg() {
  205. let win = window;
  206. let a = 0;
  207. while (win = win.parent) {
  208. _postMsg.a = ++a;
  209. _postMsg(win, ...arguments)
  210. if (win == top) break;
  211. }
  212. }
  213.  
  214.  
  215. function crossBrowserTransition(type) {
  216. if (crossBrowserTransition['_result_' + type]) return crossBrowserTransition['_result_' + type]
  217. let el = document.createElement("fakeelement");
  218.  
  219. const capital = (x) => x[0].toUpperCase() + x.substr(1);
  220. const capitalType = capital(type);
  221.  
  222. const transitions = {
  223. [type]: `${type}end`,
  224. [`O${capitalType}`]: `o${capitalType}End`,
  225. [`Moz${capitalType}`]: `${type}end`,
  226. [`Webkit${capitalType}`]: `webkit${capitalType}End`,
  227. [`MS${capitalType}`]: `MS${capitalType}End`
  228. }
  229.  
  230. for (let styleProp in transitions) {
  231. if (el.style[styleProp] !== undefined) {
  232. return (crossBrowserTransition['_result_' + type] = transitions[styleProp]);
  233. }
  234. }
  235. }
  236.  
  237. function isInOperation(elm) {
  238. let elmInFocus = elm || document.activeElement;
  239. if (!elmInFocus) return false;
  240. let res1 = elmInFocus.__matches__(
  241. 'a[href],link[href],button,input:not([type="hidden"]),select,textarea,iframe,frame,menuitem,[draggable],[contenteditable]'
  242. );
  243. return res1;
  244. }
  245.  
  246. const fn_toString = (f, n = 50) => {
  247. let s = (f + "");
  248. if (s.length > 2 * n + 5) {
  249. s = s.substr(0, n) + ' ... ' + s.substr(-n);
  250. }
  251. return s
  252. };
  253.  
  254. function consoleLog() {
  255. if (!_debug_h5p_logging_) return;
  256. if (isIframe) postMsg('consoleLog', ...arguments);
  257. else console.log.apply(console, arguments);
  258. }
  259.  
  260. function consoleLogF() {
  261. if (isIframe) postMsg('consoleLog', ...arguments);
  262. else console.log.apply(console, arguments);
  263. }
  264.  
  265. class AFLooperArray extends Array {
  266. constructor() {
  267. super();
  268. this.activeLoopsCount = 0;
  269. this.cid = 0;
  270. this.loopingFrame = this.loopingFrame.bind(this);
  271. }
  272.  
  273. loopingFrame() {
  274. if (!this.cid) return; //cancelled
  275. for (const opt of this) {
  276. if (opt.isFunctionLooping) opt.qn(opt.fn);
  277. }
  278. this.cid = $ws.requestAnimationFrame(this.loopingFrame);
  279. }
  280.  
  281. get isArrayLooping() {
  282. return this.cid > 0;
  283. }
  284.  
  285. loopStart() {
  286. this.cid = $ws.requestAnimationFrame(this.loopingFrame);
  287. }
  288. loopStop() {
  289. if (this.cid) $ws.cancelAnimationFrame(this.cid);
  290. this.cid = 0;
  291. }
  292. appendLoop(fn) {
  293. if (typeof fn != 'function' || !this) return;
  294. const opt = new AFLooperFunc(fn, this);
  295. super.push(opt);
  296. return opt;
  297. }
  298. }
  299.  
  300. class AFLooperFunc {
  301. constructor(fn, bind) {
  302. this._looping = false;
  303. this.fn = fn.bind(this);
  304. if ($mb.nightly_isSupportQueueMicrotask()) this.qn = $mb.queueMicrotask;
  305. else this.qn = this.fn; //qn(fn) = qn() = fn()
  306. this.bind = bind;
  307. }
  308. get isFunctionLooping() {
  309. return this._looping;
  310. }
  311. loopingStart() {
  312. if (this._looping === false) {
  313. this._looping = true;
  314. if (++this.bind.activeLoopsCount == 1) this.bind.loopStart();
  315. }
  316. }
  317. loopingStop() {
  318. if (this._looping === true) {
  319. this._looping = false;
  320. if (--this.bind.activeLoopsCount == 0) this.bind.loopStop();
  321. }
  322. }
  323. }
  324.  
  325. function decimalEqual(a, b) {
  326. return Math.round(a * 100000000) == Math.round(b * 100000000)
  327. }
  328.  
  329. function nonZeroNum(a) {
  330. return a > 0 || a < 0;
  331. }
  332.  
  333. class PlayerConf {
  334.  
  335. get scaleFactor() {
  336. return this.mFactor * this.vFactor;
  337. }
  338.  
  339. cssTransform() {
  340.  
  341. const playerConf = this;
  342. const player = playerConf.domElement;
  343. if (!player) return;
  344. const videoScale = playerConf.scaleFactor;
  345.  
  346. let {
  347. x,
  348. y
  349. } = playerConf.translate;
  350.  
  351. let [_x, _y] = ((playerConf.rotate % 180) == 90) ? [y, x] : [x, y];
  352.  
  353.  
  354. if ((playerConf.rotate % 360) == 270) _x = -_x;
  355. if ((playerConf.rotate % 360) == 90) _y = -_y;
  356.  
  357. var s = [
  358. playerConf.rotate > 0 ? 'rotate(' + playerConf.rotate + 'deg)' : '',
  359. !decimalEqual(videoScale, 1.0) ? 'scale(' + videoScale + ')' : '',
  360. (nonZeroNum(_x) || nonZeroNum(_y)) ? `translate(${_x}px, ${_y}px)` : '',
  361. ];
  362.  
  363. player.style.transform = s.join(' ').trim()
  364.  
  365. }
  366.  
  367. constructor() {
  368.  
  369. this.translate = {
  370. x: 0,
  371. y: 0
  372. };
  373. this.rotate = 0;
  374. this.mFactor = 1.0;
  375. this.vFactor = 1.0;
  376. this.fps = 30;
  377. this.filter_key = {};
  378. this.filter_view_units = {
  379. 'hue-rotate': 'deg',
  380. 'blur': 'px'
  381. };
  382. this.filterReset();
  383.  
  384. }
  385.  
  386. setFilter(prop, f) {
  387.  
  388. let oldValue = this.filter_key[prop];
  389. if (typeof oldValue != 'number') return;
  390. let newValue = f(oldValue)
  391. if (oldValue != newValue) {
  392.  
  393. newValue = +newValue.toFixed(6); //javascript bug
  394.  
  395. }
  396.  
  397. this.filter_key[prop] = newValue
  398. this.filterSetup();
  399.  
  400. return newValue;
  401.  
  402.  
  403.  
  404. }
  405.  
  406. filterSetup(options) {
  407.  
  408. let ums = GM_getValue("unsharpen_mask")
  409. if (!ums) ums = ""
  410.  
  411. let view = []
  412. let playerElm = $hs.player();
  413. if (!playerElm) return;
  414. for (let view_key in this.filter_key) {
  415. let filter_value = +((+this.filter_key[view_key] || 0).toFixed(3))
  416. let addTo = true;
  417. switch (view_key) {
  418. case 'brightness':
  419. /* fall through */
  420. case 'contrast':
  421. /* fall through */
  422. case 'saturate':
  423. if (decimalEqual(filter_value, 1.0)) addTo = false;
  424. break;
  425. case 'hue-rotate':
  426. /* fall through */
  427. case 'blur':
  428. if (decimalEqual(filter_value, 0.0)) addTo = false;
  429. break;
  430. }
  431. let view_unit = this.filter_view_units[view_key] || ''
  432. if (addTo) view.push(`${view_key}(${filter_value}${view_unit})`)
  433. this.filter_key[view_key] = Number(+this.filter_key[view_key] || 0)
  434. }
  435. if (ums) view.push(`url("#_h5p_${ums}")`);
  436. if (options && options.grey) view.push('url("#grey1")');
  437. playerElm.style.filter = view.join(' ').trim(); //performance in firefox is bad
  438. }
  439.  
  440. filterReset() {
  441. this.filter_key['brightness'] = 1.0
  442. this.filter_key['contrast'] = 1.0
  443. this.filter_key['saturate'] = 1.0
  444. this.filter_key['hue-rotate'] = 0.0
  445. this.filter_key['blur'] = 0.0
  446. this.filterSetup()
  447. }
  448.  
  449. }
  450.  
  451. const Store = {
  452. prefix: '_h5_player',
  453. save: function(k, v) {
  454. if (!Store.available()) return false;
  455. if (typeof v != 'string') return false;
  456. Store.LS.setItem(Store.prefix + k, v)
  457. let sk = fn_toString(k + "", 30);
  458. let sv = fn_toString(v + "", 30);
  459. consoleLog(`localStorage Saved "${sk}" = "${sv}"`)
  460. return true;
  461.  
  462. },
  463. read: function(k) {
  464. if (!Store.available()) return false;
  465. let v = Store.LS.getItem(Store.prefix + k)
  466. let sk = fn_toString(k + "", 30);
  467. let sv = fn_toString(v + "", 30);
  468. consoleLog(`localStorage Read "${sk}" = "${sv}"`);
  469. return v;
  470.  
  471. },
  472. remove: function(k) {
  473.  
  474. if (!Store.available()) return false;
  475. Store.LS.removeItem(Store.prefix + k)
  476. let sk = fn_toString(k + "", 30);
  477. consoleLog(`localStorage Removed "${sk}"`)
  478. return true;
  479. },
  480. clearInvalid: function(sVersion) {
  481. if (!Store.available()) return false;
  482.  
  483. //let sVersion=1814;
  484. if (+Store.read('_sVersion_') < sVersion) {
  485. Store._keys()
  486. .filter(s => s.indexOf(Store.prefix) === 0)
  487. .forEach(key => window.localStorage.removeItem(key))
  488. Store.save('_sVersion_', sVersion + '')
  489. return 2;
  490. }
  491. return 1;
  492.  
  493. },
  494. available: function() {
  495. if (Store.LS) return true;
  496. if (!window) return false;
  497. const localStorage = window.localStorage;
  498. if (!localStorage) return false;
  499. if (typeof localStorage != 'object') return false;
  500. if (!('getItem' in localStorage)) return false;
  501. if (!('setItem' in localStorage)) return false;
  502. Store.LS = localStorage;
  503. return true;
  504.  
  505. },
  506. _keys: function() {
  507. return Object.keys(localStorage);
  508. },
  509. _setItem: function(key, value) {
  510. return localStorage.setItem(key, value)
  511. },
  512. _getItem: function(key) {
  513. return localStorage.getItem(key)
  514. },
  515. _removeItem: function(key) {
  516. return localStorage.removeItem(key)
  517. }
  518.  
  519. }
  520.  
  521. const domTool = {
  522. nopx: (x) => +x.replace('px', ''),
  523. cssWH: function(m, r) {
  524. if (!r) r = getComputedStyle(m, null);
  525. let c = (x) => +x.replace('px', '');
  526. return {
  527. w: m.offsetWidth || c(r.width),
  528. h: m.offsetHeight || c(r.height)
  529. }
  530. },
  531. _isActionBox_1: function(vEl, pEl) {
  532.  
  533. const vElCSS = domTool.cssWH(vEl);
  534. let vElCSSw = vElCSS.w;
  535. let vElCSSh = vElCSS.h;
  536.  
  537. let vElx = vEl;
  538. const res = [];
  539. //let mLevel = 0;
  540. if (vEl && pEl && vEl != pEl && pEl.contains(vEl)) {
  541. while (vElx && vElx != pEl) {
  542. vElx = vElx.parentNode;
  543. let vElx_css = null;
  544. if (isShadowRoot(vElx)) {} else {
  545. vElx_css = getComputedStyle(vElx, null);
  546. let vElx_wp = domTool.nopx(vElx_css.paddingLeft) + domTool.nopx(vElx_css.paddingRight)
  547. vElCSSw += vElx_wp
  548. let vElx_hp = domTool.nopx(vElx_css.paddingTop) + domTool.nopx(vElx_css.paddingBottom)
  549. vElCSSh += vElx_hp
  550. }
  551. res.push({
  552. //level: ++mLevel,
  553. padW: vElCSSw,
  554. padH: vElCSSh,
  555. elm: vElx,
  556. css: vElx_css
  557. })
  558.  
  559. }
  560. }
  561.  
  562. // in the array, each item is the parent of video player
  563. //res.vEl_cssWH = vElCSS
  564.  
  565. return res;
  566.  
  567. },
  568. _isActionBox: function(vEl, walkRes, pEl_idx) {
  569.  
  570. function absDiff(w1, w2, h1, h2) {
  571. const w = (w1 - w2),
  572. h = h1 - h2;
  573. return [(w > 0 ? w : -w), (h > 0 ? h : -h)]
  574. }
  575.  
  576. function midPoint(rect) {
  577. return {
  578. x: (rect.left + rect.right) / 2,
  579. y: (rect.top + rect.bottom) / 2
  580. }
  581. }
  582.  
  583. const parentCount = walkRes.length;
  584. if (pEl_idx >= 0 && pEl_idx < parentCount) {} else {
  585. return;
  586. }
  587. const pElr = walkRes[pEl_idx]
  588. if (!pElr.css) {
  589. //shadowRoot
  590. return true;
  591. }
  592.  
  593. const pEl = pElr.elm;
  594.  
  595. //prevent activeElement==body
  596. const pElCSS = domTool.cssWH(pEl, pElr.css);
  597.  
  598. //check prediction of parent dimension
  599. const d1v = absDiff(pElCSS.w, pElr.padW, pElCSS.h, pElr.padH)
  600.  
  601. const d1x = d1v[0] < 10
  602. const d1y = d1v[1] < 10;
  603.  
  604. if (d1x && d1y) return true; //both edge along the container - fit size
  605. if (!d1x && !d1y) return false; //no edge along the container - body contain the video element, fixed width&height
  606.  
  607. //case: youtube video fullscreen
  608.  
  609. //check centre point
  610.  
  611. const pEl_rect = pEl.getBoundingClientRect()
  612. const vEl_rect = vEl.getBoundingClientRect()
  613.  
  614. const pEl_center = midPoint(pEl_rect)
  615. const vEl_center = midPoint(vEl_rect)
  616.  
  617. const d2v = absDiff(pEl_center.x, vEl_center.x, pEl_center.y, vEl_center.y);
  618.  
  619. const d2x = d2v[0] < 10;
  620. const d2y = d2v[1] < 10;
  621.  
  622. return (d2x && d2y);
  623.  
  624. },
  625. getRect: function(element) {
  626. let rect = element.getBoundingClientRect();
  627. let scroll = domTool.getScroll();
  628. return {
  629. pageX: rect.left + scroll.left,
  630. pageY: rect.top + scroll.top,
  631. screenX: rect.left,
  632. screenY: rect.top
  633. };
  634. },
  635. getScroll: function() {
  636. return {
  637. left: document.documentElement.scrollLeft || document.body.scrollLeft,
  638. top: document.documentElement.scrollTop || document.body.scrollTop
  639. };
  640. },
  641. getClient: function() {
  642. return {
  643. width: document.compatMode == 'CSS1Compat' ? document.documentElement.clientWidth : document.body.clientWidth,
  644. height: document.compatMode == 'CSS1Compat' ? document.documentElement.clientHeight : document.body.clientHeight
  645. };
  646. },
  647. addStyle: //GM_addStyle,
  648. function(css, head) {
  649. if (!head) {
  650. let _doc = document.documentElement;
  651. head = _doc.querySelector('head') || _doc.querySelector('html') || _doc;
  652. }
  653. let doc = head.ownerDocument;
  654. let style = doc.createElement('style');
  655. style.type = 'text/css';
  656. style.textContent = css;
  657. head.appendChild(style);
  658. //console.log(document.head,style,'add style')
  659. return style;
  660. },
  661. eachParentNode: function(dom, fn) {
  662. let parent = dom.parentNode
  663. while (parent) {
  664. let isEnd = fn(parent, dom)
  665. parent = parent.parentNode
  666. if (isEnd) {
  667. break
  668. }
  669. }
  670. },
  671.  
  672. hideDom: function hideDom(selector) {
  673. let dom = document.querySelector(selector)
  674. if (dom) {
  675. $ws.requestAnimationFrame(function() {
  676. dom.style.opacity = 0;
  677. dom.style.transform = 'translate(-9999px)';
  678. dom = null;
  679. })
  680. }
  681. }
  682. };
  683.  
  684. const handle = {
  685.  
  686.  
  687. afPlaybackRecording: async function() {
  688. const opts = this;
  689.  
  690. let qTime = +new Date;
  691. if (qTime >= opts.pTime) {
  692. opts.pTime = qTime + opts.timeDelta; //prediction of next Interval
  693. opts.savePlaybackProgress()
  694. }
  695.  
  696. },
  697. savePlaybackProgress: function() {
  698.  
  699. //this refer to endless's opts
  700. let player = this.player;
  701.  
  702. let _uid = this.player_uid; //_h5p_uid_encrypted
  703. if (!_uid) return;
  704.  
  705. let shallSave = true;
  706. let currentTimeToSave = ~~player.currentTime;
  707.  
  708. if (this._lastSave == currentTimeToSave) shallSave = false;
  709.  
  710. if (shallSave) {
  711.  
  712. this._lastSave = currentTimeToSave
  713.  
  714. //console.log('aasas',this.player_uid, shallSave, '_play_progress_'+_uid, currentTimeToSave)
  715.  
  716. Store.save('_play_progress_' + _uid, jsonStringify({
  717. 't': currentTimeToSave
  718. }))
  719.  
  720. }
  721.  
  722. },
  723. playingWithRecording: function() {
  724. let player = this.player;
  725. if (!player.paused && !this.isFunctionLooping) {
  726. let player = this.player;
  727. let _uid = player.getAttribute('_h5p_uid_encrypted') || ''
  728. if (_uid) {
  729. this.player_uid = _uid;
  730. this.pTime = 0;
  731. this.loopingStart();
  732. }
  733. }
  734. }
  735.  
  736. };
  737.  
  738. class Momentary extends Map {
  739. act(uniqueId, fn_start, fn_end, delay) {
  740. if (!uniqueId) return;
  741. uniqueId = uniqueId + "";
  742. const last_cid = this.get(uniqueId);
  743. if (last_cid > 0) clearTimeout(last_cid);
  744. fn_start();
  745. const new_cid = setTimeout(fn_end, delay)
  746. this.set(uniqueId, new_cid)
  747. }
  748. }
  749.  
  750. const momentary = new Momentary();
  751.  
  752. const $hs = {
  753.  
  754. /* 提示文本的字號 */
  755. fontSize: 16,
  756. enable: true,
  757. playerInstance: null,
  758. playbackRate: 1,
  759. /* 快進快退步長 */
  760. skipStep: 5,
  761.  
  762. /* 獲取當前播放器的實例 */
  763. player: function() {
  764. let res = $hs.playerInstance || null;
  765. if (res && res.parentNode == null) {
  766. $hs.playerInstance = null;
  767. res = null;
  768. }
  769.  
  770. if (res == null) {
  771. for (let k in playerConfs) {
  772. let playerConf = playerConfs[k];
  773. if (playerConf && playerConf.domElement && playerConf.domElement.parentNode) return playerConf.domElement;
  774. }
  775. }
  776. return res;
  777. },
  778.  
  779. pictureInPicture: function(videoElm) {
  780. if (document.pictureInPictureElement) {
  781. document.exitPictureInPicture();
  782. } else if ('requestPictureInPicture' in videoElm) {
  783. videoElm.requestPictureInPicture()
  784. } else {
  785. $hs.tips('PIP is not supported.');
  786. }
  787. },
  788.  
  789. getPlayerConf: function(video) {
  790.  
  791. if (!video) return null;
  792. let vpid = video.getAttribute('_h5ppid') || null;
  793. if (!vpid) return null;
  794. return playerConfs[vpid] || null;
  795.  
  796. },
  797.  
  798. handlerVideoPlaying: function(evt) {
  799. const videoElm = evt.target || this || null;
  800. const playerConf = $hs.getPlayerConf(videoElm)
  801.  
  802. $hs._actionBoxObtain(videoElm);
  803. if (playerConf) {
  804. if (playerConf.timeout_pause > 0) playerConf.timeout_pause = clearTimeout(playerConf.timeout_pause);
  805. playerConf.lastPauseAt = 0
  806. playerConf.domActive |= DOM_ACTIVE_ONCE_PLAYED;
  807. playerConf.domActive &= ~DOM_ACTIVE_DELAYED_PAUSED;
  808. }
  809.  
  810. $hs.swtichPlayerInstance();
  811.  
  812.  
  813.  
  814. $hs.onVideoTriggering();
  815.  
  816. if (!$hs.enable) return $hs.tips(false);
  817.  
  818. if (videoElm._isThisPausedBefore_) consoleLog('resumed')
  819. let _pausedbefore_ = videoElm._isThisPausedBefore_
  820.  
  821. if (videoElm.playpause_cid) {
  822. clearTimeout(videoElm.playpause_cid);
  823. videoElm.playpause_cid = 0;
  824. }
  825. let _last_paused = videoElm._last_paused
  826. videoElm._last_paused = videoElm.paused
  827. if (_last_paused === !videoElm.paused) {
  828. videoElm.playpause_cid = setTimeout(() => {
  829. if (videoElm.paused === !_last_paused && !videoElm.paused && _pausedbefore_) {
  830. $hs.tips('Playback resumed', undefined, 2500)
  831. }
  832. }, 90)
  833. }
  834.  
  835. /* 播放的時候進行相關同步操作 */
  836.  
  837. if (!videoElm._record_continuous) {
  838.  
  839. /* 同步之前設定的播放速度 */
  840. $hs.setPlaybackRate()
  841.  
  842. if (!_endlessloop) _endlessloop = new AFLooperArray();
  843.  
  844. videoElm._record_continuous = _endlessloop.appendLoop(handle.afPlaybackRecording);
  845. videoElm._record_continuous._lastSave = -999;
  846.  
  847. videoElm._record_continuous.timeDelta = 2000;
  848. videoElm._record_continuous.player = videoElm
  849. videoElm._record_continuous.savePlaybackProgress = handle.savePlaybackProgress;
  850. videoElm._record_continuous.playingWithRecording = handle.playingWithRecording;
  851. }
  852.  
  853. videoElm._record_continuous.playingWithRecording(videoElm); //try to start recording
  854.  
  855. videoElm._isThisPausedBefore_ = false;
  856.  
  857. },
  858. handlerVideoPause: function(evt) {
  859.  
  860. const videoElm = evt.target || this || null;
  861.  
  862. const playerConf = $hs.getPlayerConf(videoElm)
  863. if (playerConf) {
  864. playerConf.lastPauseAt = +new Date;
  865. playerConf.timeout_pause = setTimeout(() => {
  866. if (playerConf.lastPauseAt > 0) playerConf.domActive |= DOM_ACTIVE_DELAYED_PAUSED;
  867. }, 600)
  868. }
  869.  
  870. if (!$hs.enable) return $hs.tips(false);
  871. consoleLog('pause')
  872. videoElm._isThisPausedBefore_ = true;
  873.  
  874. let _last_paused = videoElm._last_paused
  875. videoElm._last_paused = videoElm.paused
  876. if (videoElm.playpause_cid) {
  877. clearTimeout(videoElm.playpause_cid);
  878. videoElm.playpause_cid = 0;
  879. }
  880. if (_last_paused === !videoElm.paused) {
  881. videoElm.playpause_cid = setTimeout(() => {
  882. if (videoElm.paused === !_last_paused && videoElm.paused) {
  883. $hs._tips(videoElm, 'Playback paused', undefined, 2500)
  884. }
  885. }, 90)
  886. }
  887.  
  888.  
  889. if (videoElm._record_continuous && videoElm._record_continuous.isFunctionLooping) {
  890. videoElm._record_continuous.savePlaybackProgress(); //savePlaybackProgress once before stopping //handle.savePlaybackProgress;
  891. videoElm._record_continuous.loopingStop();
  892. }
  893.  
  894.  
  895. },
  896. handlerVideoVolumeChange: function(evt) {
  897.  
  898. const videoElm = evt.target || this || null;
  899.  
  900. if (videoElm.volume >= 0) {} else {
  901. return;
  902. }
  903. let cVol = videoElm.volume;
  904. let cMuted = videoElm.muted;
  905.  
  906. if (cVol === videoElm._volume_p && cMuted === videoElm._muted_p) {
  907. // nothing changed
  908. } else if (cVol === videoElm._volume_p && cMuted !== videoElm._muted_p) {
  909. // muted changed
  910. } else { // cVol != pVol
  911.  
  912. // only volume changed
  913.  
  914. let shallShowTips = videoElm._volume >= 0; //prevent initialization
  915.  
  916. if (!cVol) {
  917. videoElm.muted = true;
  918. } else if (cMuted) {
  919. videoElm.muted = false;
  920. videoElm._volume = cVol;
  921. } else if (!cMuted) {
  922. videoElm._volume = cVol;
  923. }
  924. consoleLog('volume changed')
  925.  
  926. if (shallShowTips)
  927. $hs._tips(videoElm, 'Volume: ' + dround(videoElm.volume * 100) + '%', undefined, 3000)
  928.  
  929. }
  930.  
  931. videoElm._volume_p = cVol
  932. videoElm._muted_p = cMuted
  933.  
  934. },
  935. handlerVideoLoadedMetaData: function(evt) {
  936. const videoElm = evt.target || this || null;
  937. consoleLog('video size', this.videoWidth + ' x ' + this.videoHeight);
  938.  
  939. let vpid = videoElm.getAttribute('_h5ppid') || null;
  940. if (!vpid || !videoElm.currentSrc) return;
  941.  
  942. if ($hs.varSrcList[vpid] != videoElm.currentSrc) {
  943. $hs.varSrcList[vpid] = videoElm.currentSrc;
  944. $hs.videoSrcFound(videoElm);
  945. $hs._actionBoxObtain(videoElm);
  946. }
  947. if (!videoElm._onceVideoLoaded) {
  948. videoElm._onceVideoLoaded = true;
  949. playerConfs[vpid].domActive |= DOM_ACTIVE_SRC_LOADED;
  950. }
  951.  
  952. },
  953. handlerElementMouseEnter: function(evt) {
  954. if ($hs.intVideoInitCount > 0) {} else {
  955. return;
  956. }
  957. const actionBoxRelation = $hs.getActionBoxRelationFromDOM(evt.target);
  958. if (!actionBoxRelation) return;
  959. const actionBox = actionBoxRelation.actionBox
  960. if (!actionBox) return;
  961. const vpid = actionBox.getAttribute('_h5p_actionbox_');
  962. const videoElm = actionBoxRelation.player;
  963. if (!videoElm) return;
  964.  
  965. $hs._actionBoxObtain(videoElm);
  966.  
  967. const playerConf = $hs.getPlayerConf(videoElm)
  968. if (playerConf) {
  969. momentary.act("actionBoxMouseEnter",
  970. () => {
  971. playerConf.domActive |= DOM_ACTIVE_MOUSE_IN;
  972. },
  973. () => {
  974. playerConf.domActive &= ~DOM_ACTIVE_MOUSE_IN;
  975. },
  976. 300)
  977. }
  978.  
  979. },
  980. handlerElementMouseDown: function(evt) {
  981.  
  982. if ($hs.intVideoInitCount > 0) {} else {
  983. return;
  984. }
  985.  
  986. const actionBoxRelation = $hs.getActionBoxRelationFromDOM(evt.target);
  987. if (!actionBoxRelation) return;
  988. const actionBox = actionBoxRelation.actionBox
  989. if (!actionBox) return;
  990. const vpid = actionBox.getAttribute('_h5p_actionbox_');
  991. const videoElm = actionBoxRelation.player;
  992. if (!videoElm) return;
  993.  
  994. $hs._actionBoxObtain(videoElm);
  995.  
  996. const playerConf = $hs.getPlayerConf(videoElm)
  997. if (playerConf) {
  998. momentary.act("actionBoxClicking",
  999. () => {
  1000. playerConf.domActive |= DOM_ACTIVE_MOUSE_CLICK;
  1001. },
  1002. () => {
  1003. playerConf.domActive &= ~DOM_ACTIVE_MOUSE_CLICK;
  1004. },
  1005. 300)
  1006. }
  1007. $hs.swtichPlayerInstance();
  1008.  
  1009. },
  1010. handlerElementWheelTuneVolume: function(evt) { //shift + wheel
  1011.  
  1012. if ($hs.intVideoInitCount > 0) {} else {
  1013. return;
  1014. }
  1015.  
  1016. const actionBoxRelation = $hs.getActionBoxRelationFromDOM(evt.target);
  1017. if (!actionBoxRelation) return;
  1018. const actionBox = actionBoxRelation.actionBox
  1019. if (!actionBox) return;
  1020. const vpid = actionBox.getAttribute('_h5p_actionbox_');
  1021. const videoElm = actionBoxRelation.player;
  1022. if (!videoElm) return;
  1023.  
  1024. $hs._actionBoxObtain(videoElm);
  1025.  
  1026. if (!evt.shiftKey) return;
  1027. if (evt.deltaY) {
  1028. let player = $hs.player();
  1029. if (!player || player != videoElm) return;
  1030.  
  1031. if (evt.deltaY > 0) {
  1032.  
  1033. if ((player.muted && player.volume === 0) && player._volume > 0) {
  1034.  
  1035. player.muted = false;
  1036. player.volume = player._volume;
  1037. } else if (player.muted && (player.volume > 0 || !player._volume)) {
  1038. player.muted = false;
  1039. }
  1040. $hs.tuneVolume(-0.05)
  1041.  
  1042. evt.stopPropagation()
  1043. evt.preventDefault()
  1044. return false
  1045.  
  1046. } else if (evt.deltaY < 0) {
  1047.  
  1048. if ((player.muted && player.volume === 0) && player._volume > 0) {
  1049. player.muted = false;
  1050. player.volume = player._volume;
  1051. } else if (player.muted && (player.volume > 0 || !player._volume)) {
  1052. player.muted = false;
  1053. }
  1054. $hs.tuneVolume(+0.05)
  1055.  
  1056. evt.stopPropagation()
  1057. evt.preventDefault()
  1058. return false
  1059.  
  1060. }
  1061. }
  1062. },
  1063. debug01: function(evt, videoActive) {
  1064.  
  1065. if (!$hs.eventHooks) {
  1066. document.__h5p_eventhooks = ($hs.eventHooks = {
  1067. _debug_: []
  1068. });
  1069. }
  1070. $hs.eventHooks._debug_.push([videoActive, evt.type]);
  1071. // console.log('h5p eventhooks = document.__h5p_eventhooks')
  1072. },
  1073.  
  1074. swtichPlayerInstance: function() {
  1075.  
  1076. let newPlayerInstance = null;
  1077. const ONLY_PLAYING_NONE = 0x4A00;
  1078. const ONLY_PLAYING_MORE_THAN_ONE = 0x5A00;
  1079. let onlyPlayingInstance = ONLY_PLAYING_NONE;
  1080. for (let k in playerConfs) {
  1081. let playerConf = playerConfs[k] || {};
  1082. let {
  1083. domElement,
  1084. domActive
  1085. } = playerConf;
  1086. if (domElement) {
  1087. if (domActive & DOM_ACTIVE_INVALID_PARENT) continue;
  1088. if (!domElement.parentNode) {
  1089. playerConf.domActive |= DOM_ACTIVE_INVALID_PARENT;
  1090. continue;
  1091. }
  1092. if (domActive & DOM_ACTIVE_MOUSE_CLICK) {
  1093. newPlayerInstance = domElement
  1094. break;
  1095. }
  1096. if (domActive & DOM_ACTIVE_ONCE_PLAYED && (domActive & DOM_ACTIVE_DELAYED_PAUSED) == 0) {
  1097. if (onlyPlayingInstance == ONLY_PLAYING_NONE) onlyPlayingInstance = domElement;
  1098. else onlyPlayingInstance = ONLY_PLAYING_MORE_THAN_ONE;
  1099. }
  1100. }
  1101. }
  1102. if (newPlayerInstance == null && onlyPlayingInstance.nodeType == 1) {
  1103. newPlayerInstance = onlyPlayingInstance;
  1104. }
  1105.  
  1106. $hs.playerInstance = newPlayerInstance
  1107.  
  1108.  
  1109. },
  1110. handlerElementDblClick: function(evt) {
  1111.  
  1112. if ($hs.intVideoInitCount > 0) {} else {
  1113. return;
  1114. }
  1115.  
  1116. if (document.readyState != "complete") return;
  1117.  
  1118. const actionBoxRelation = $hs.getActionBoxRelationFromDOM(evt.target);
  1119. if (!actionBoxRelation) return;
  1120. const actionBox = actionBoxRelation.actionBox
  1121. if (!actionBox) return;
  1122. const vpid = actionBox.getAttribute('_h5p_actionbox_');
  1123. const videoElm = actionBoxRelation.player;
  1124. if (!videoElm) return;
  1125.  
  1126. $hs._actionBoxObtain(videoElm);
  1127.  
  1128. const playerConf = $hs.getPlayerConf(videoElm)
  1129. if (playerConf) {
  1130. momentary.act("actionBoxClicking",
  1131. () => {
  1132. playerConf.domActive |= DOM_ACTIVE_MOUSE_CLICK;
  1133. },
  1134. () => {
  1135. playerConf.domActive &= ~DOM_ACTIVE_MOUSE_CLICK;
  1136. },
  1137. 600)
  1138. }
  1139. $hs.swtichPlayerInstance()
  1140.  
  1141. $hs.onVideoTriggering()
  1142.  
  1143. $hs.callFullScreenBtn();
  1144.  
  1145. evt.stopPropagation()
  1146. evt.preventDefault()
  1147. return false
  1148.  
  1149. },
  1150. handlerDocFocusOut: function(e) {
  1151. let doc = this;
  1152. $hs.focusFxLock = true;
  1153. $ws.requestAnimationFrame(function() {
  1154. $hs.focusFxLock = false;
  1155. if (!$hs.enable) $hs.tips(false);
  1156. else
  1157. if (!doc.hasFocus() && $hs.player() && !$hs.isLostFocus) {
  1158. $hs.isLostFocus = true;
  1159. consoleLog('doc.focusout')
  1160. //$hs.tips('focus is lost', -1);
  1161. }
  1162. });
  1163. },
  1164. handlerDocFocusIn: function(e) {
  1165. let doc = this;
  1166. if ($hs.focusFxLock) return;
  1167. $ws.requestAnimationFrame(function() {
  1168.  
  1169. if ($hs.focusFxLock) return;
  1170. if (!$hs.enable) $hs.tips(false);
  1171. else
  1172. if (doc.hasFocus() && $hs.player() && $hs.isLostFocus) {
  1173. $hs.isLostFocus = false;
  1174. consoleLog('doc.focusin')
  1175. $hs.tips(false);
  1176.  
  1177. }
  1178. });
  1179. },
  1180.  
  1181. handlerWinMessage: async function(e) {
  1182. let tag, ed;
  1183. if (typeof e.data == 'object' && typeof e.data.tag == 'string') {
  1184. tag = e.data.tag;
  1185. ed = e.data
  1186. } else {
  1187. return;
  1188. }
  1189. let msg = null,
  1190. success = 0;
  1191. switch (tag) {
  1192. case 'consoleLog':
  1193. let msg_str = ed.str;
  1194. let msg_stype = ed.stype;
  1195. if (msg_stype === 1) {
  1196. msg = (document[str_postMsgData] || {})[msg_str] || [];
  1197. success = 1;
  1198. } else if (msg_stype === 2) {
  1199. msg = jsonParse(msg_str);
  1200. if (msg && msg.d) {
  1201. success = 2;
  1202. msg = msg.d;
  1203. }
  1204. } else {
  1205. msg = msg_str
  1206. }
  1207. let p = (ed.passing && ed.winOrder) ? [' | from win-' + ed.winOrder] : [];
  1208. if (success) {
  1209. console.log(...msg, ...p)
  1210. //document[ed.data]=null; // also delete the information
  1211. } else {
  1212. console.log('msg--', msg, ...p, ed);
  1213. }
  1214. break;
  1215.  
  1216. }
  1217. },
  1218.  
  1219. isInActiveMode: function(activeElm, player) {
  1220.  
  1221. console.log('check active mode', activeElm, player)
  1222. if (activeElm == player) {
  1223. return true;
  1224. }
  1225.  
  1226. for (let vpid in $hs.actionBoxRelations) {
  1227. const actionBox = $hs.actionBoxRelations[vpid].actionBox
  1228. if (actionBox && actionBox.parentNode) {
  1229. if (activeElm == actionBox || actionBox.contains(activeElm)) {
  1230. return true;
  1231. }
  1232. }
  1233. }
  1234.  
  1235. let _checkingPass = false;
  1236.  
  1237. if (!player) return;
  1238. let layoutBox = $hs.getPlayerBlockElement(player).parentNode;
  1239. if (layoutBox && layoutBox.parentNode && layoutBox.contains(activeElm)) {
  1240. let rpid = player.getAttribute('_h5ppid') || "NULL";
  1241. let actionBox = layoutBox.parentNode.querySelector(`[_h5p_actionbox_="${rpid}"]`); //the box can be layoutBox
  1242. if (actionBox && actionBox.contains(activeElm)) _checkingPass = true;
  1243. }
  1244.  
  1245. return _checkingPass
  1246. },
  1247.  
  1248.  
  1249. toolCheckFullScreen: function(doc) {
  1250. if (typeof doc.fullScreen == 'boolean') return doc.fullScreen;
  1251. if (typeof doc.webkitIsFullScreen == 'boolean') return doc.webkitIsFullScreen;
  1252. if (typeof doc.mozFullScreen == 'boolean') return doc.mozFullScreen;
  1253. return null;
  1254. },
  1255.  
  1256. toolFormatCT: function(u) {
  1257.  
  1258. let w = Math.round(u, 0)
  1259. let a = w % 60
  1260. w = (w - a) / 60
  1261. let b = w % 60
  1262. w = (w - b) / 60
  1263. let str = ("0" + b).substr(-2) + ":" + ("0" + a).substr(-2);
  1264. if (w) str = w + ":" + str
  1265.  
  1266. return str
  1267.  
  1268. },
  1269.  
  1270. makeFocus: function(player, evt) {
  1271. setTimeout(function() {
  1272. let rpid = player.getAttribute('_h5ppid');
  1273. let actionBox = getRoot(player).querySelector(`[_h5p_actionbox_="${rpid}"]`);
  1274.  
  1275. //console.log('p',rpid, player,actionBox,document.activeElement)
  1276. if (actionBox && actionBox != document.activeElement && !actionBox.contains(document.activeElement)) {
  1277. consoleLog('make focus on', actionBox)
  1278. actionBox.focus();
  1279.  
  1280. }
  1281.  
  1282. }, 300)
  1283. },
  1284.  
  1285. getActionBlockElement: function(player, layoutBox) {
  1286.  
  1287. //player, $hs.getPlayerBlockElement(player).parentNode;
  1288. //player, player.parentNode .... player.parentNode.parentNode.parentNode
  1289.  
  1290. const allFullScreenBtns = $hs.queryFullscreenBtnsIndependant(layoutBox)
  1291. let actionBox = null;
  1292.  
  1293. // console.log('fa0a', allFullScreenBtns.length, layoutBox)
  1294. if (allFullScreenBtns.length > 0) {
  1295. // console.log('faa', allFullScreenBtns.length)
  1296.  
  1297. for (const possibleFullScreenBtn of allFullScreenBtns) possibleFullScreenBtn.setAttribute('__h5p_fsb__', '');
  1298. let pElm = player.parentNode;
  1299. let fullscreenBtns = null;
  1300. while (pElm && pElm.parentNode) {
  1301. fullscreenBtns = pElm.querySelectorAll('[__h5p_fsb__]');
  1302. if (fullscreenBtns.length > 0) {
  1303. // console.log('fsb', pElm, fullscreenBtns)
  1304. break;
  1305. }
  1306. pElm = pElm.parentNode;
  1307. }
  1308. for (const possibleFullScreenBtn of allFullScreenBtns) possibleFullScreenBtn.removeAttribute('__h5p_fsb__');
  1309. if (fullscreenBtns && fullscreenBtns.length > 0) {
  1310. actionBox = pElm;
  1311. fullscreenBtns = $hs.exclusiveElements(fullscreenBtns);
  1312. return {
  1313. actionBox,
  1314. fullscreenBtns
  1315. };
  1316. }
  1317. }
  1318.  
  1319. let walkRes = domTool._isActionBox_1(player, layoutBox);
  1320. //walkRes.elm = player... player.parentNode.parentNode (i.e. wPlayer)
  1321. let parentCount = walkRes.length;
  1322.  
  1323. if (parentCount - 1 >= 0 && domTool._isActionBox(player, walkRes, parentCount - 1)) {
  1324. actionBox = walkRes[parentCount - 1].elm;
  1325. } else if (parentCount - 2 >= 0 && domTool._isActionBox(player, walkRes, parentCount - 2)) {
  1326. actionBox = walkRes[parentCount - 2].elm;
  1327. } else {
  1328. actionBox = player;
  1329. }
  1330.  
  1331. return {
  1332. actionBox,
  1333. fullscreenBtns: []
  1334. };
  1335.  
  1336.  
  1337.  
  1338.  
  1339. },
  1340.  
  1341. actionBoxRelations: {},
  1342.  
  1343. actionBoxRelationClearNodes: function(param) {
  1344.  
  1345. if (!param) return;
  1346. let refNode = null;
  1347. let domNodes = null;
  1348.  
  1349. if (param.nodeType > 0) {
  1350. refNode = param;
  1351. const rootNode = getRoot(refNode);
  1352. domNodes = rootNode ? rootNode.querySelectorAll(`[_h5p_mo_="${vpid}"]`) : [];
  1353. if (refNode) refNode.removeAttribute('_h5p_mo_');
  1354. } else {
  1355. const actionBoxRelation = param;
  1356. domNodes = actionBoxRelation.domNodes;
  1357. }
  1358.  
  1359. if(domNodes){
  1360. for (const domNode of domNodes) domNode.removeAttribute('_h5p_mo_')
  1361. domNodes.length = 0;
  1362. }
  1363.  
  1364.  
  1365. },
  1366.  
  1367. actionBoxMutationCallback: function(mutations, observer) {
  1368. for (const mutation of mutations) {
  1369. let pElm = mutation.target;
  1370. let vpid = null;
  1371. if (pElm && pElm.nodeType > 0 && (vpid = pElm.getAttribute('_h5p_mo_'))) {
  1372. const actionBoxRelation = $hs.actionBoxRelations[vpid]
  1373. if (actionBoxRelation) {
  1374. actionBoxRelation.mutationCount++;
  1375. } else {
  1376. $hs.actionBoxRelationClearNodes(pElm);
  1377. }
  1378. }
  1379. }
  1380. },
  1381.  
  1382.  
  1383. getActionBoxRelationFromDOM: function(elm) {
  1384.  
  1385. //assume action boxes are mutually exclusive
  1386.  
  1387. for (let vpid in $hs.actionBoxRelations) {
  1388. const actionBoxRelation = $hs.actionBoxRelations[vpid];
  1389. const actionBox = actionBoxRelation.actionBox
  1390. //console.log('ab', actionBox)
  1391. if (actionBox && actionBox.parentNode) {
  1392. if (elm == actionBox || actionBox.contains(elm)) {
  1393. return actionBoxRelation;
  1394. }
  1395. }
  1396. }
  1397.  
  1398.  
  1399. return null;
  1400.  
  1401. },
  1402.  
  1403.  
  1404.  
  1405. _actionBoxObtain: function(player) {
  1406.  
  1407. if (!player) return null;
  1408. let vpid = player.getAttribute('_h5ppid');
  1409. if (!vpid) return null;
  1410. if (!player.parentNode) return null;
  1411.  
  1412. let actionBoxRelation = $hs.actionBoxRelations[vpid],
  1413. layoutBox = null,
  1414. actionBox = null,
  1415. boxSearchResult = null,
  1416. fullscreenBtns = null,
  1417. wPlayer = null;
  1418.  
  1419. function a() {
  1420. wPlayer = $hs.getPlayerBlockElement(player);
  1421. layoutBox = wPlayer.parentNode;
  1422. boxSearchResult = $hs.getActionBlockElement(player, layoutBox);
  1423. actionBox = boxSearchResult.actionBox
  1424. fullscreenBtns = boxSearchResult.fullscreenBtns
  1425. if (actionBox && actionBox.querySelector('[_h5p_actionbox_]')) actionBox = null;
  1426. }
  1427.  
  1428. function b(domNodes) {
  1429. $hs.actionBoxRelations[vpid] = {
  1430. player: player,
  1431. wPlayer: wPlayer,
  1432. layoutBox: layoutBox,
  1433. actionBox: actionBox,
  1434. mutationCount: 0,
  1435. domNodes: domNodes,
  1436. fullscreenBtns: fullscreenBtns
  1437. }
  1438. for (const domNode of domNodes) {
  1439. domNode.setAttribute('_h5p_mo_', vpid);
  1440. $hs.actionBoxMutationObserver.observe(domNode, {
  1441. childList: true
  1442. });
  1443. }
  1444. }
  1445.  
  1446. if (actionBoxRelation) {
  1447. if (actionBoxRelation.actionBox && actionBoxRelation.actionBox.parentNode && actionBoxRelation.layoutBox && actionBoxRelation.layoutBox.parentNode) {
  1448. if (actionBoxRelation.mutationCount === 0) return actionBoxRelation.actionBox
  1449. a();
  1450. if (actionBox == actionBoxRelation.actionBox && layoutBox == actionBoxRelation.layoutBox && wPlayer == actionBoxRelation.wPlayer) {
  1451. actionBoxRelation.mutationCount = 0;
  1452. actionBoxRelation.fullscreenBtns = fullscreenBtns;
  1453. return actionBox
  1454. }
  1455. }
  1456. $hs.actionBoxRelationClearNodes(actionBoxRelation);
  1457. for (var k in actionBoxRelation) delete actionBoxRelation[k]
  1458. actionBoxRelation = null;
  1459. delete $hs.actionBoxRelations[vpid]
  1460. }
  1461.  
  1462. a();
  1463. if (actionBox) {
  1464. actionBox.setAttribute('_h5p_actionbox_', vpid);
  1465. if (!$hs.actionBoxMutationObserver) $hs.actionBoxMutationObserver = new MutationObserver($hs.actionBoxMutationCallback);
  1466. const domNodes = [];
  1467. let pElm = player;
  1468. let containing = 0;
  1469. while (pElm) {
  1470. domNodes.push(pElm);
  1471. if (pElm === actionBox) containing |= 1;
  1472. if (pElm === layoutBox) containing |= 2;
  1473. if (containing === 3) {
  1474. b(domNodes);
  1475. return actionBox
  1476. }
  1477. pElm = pElm.parentNode;
  1478. }
  1479. }
  1480.  
  1481. return null;
  1482.  
  1483.  
  1484. // if (!actionBox.hasAttribute('tabindex')) actionBox.setAttribute('tabindex', '-1');
  1485.  
  1486.  
  1487.  
  1488.  
  1489. },
  1490.  
  1491. videoSrcFound: function(player) {
  1492.  
  1493. // src loaded
  1494.  
  1495. if (!player) return;
  1496. let vpid = player.getAttribute('_h5ppid') || null;
  1497. if (!vpid || !player.currentSrc) return;
  1498.  
  1499. player._isThisPausedBefore_ = false;
  1500.  
  1501. player.removeAttribute('_h5p_uid_encrypted');
  1502.  
  1503. if (player._record_continuous) player._record_continuous._lastSave = -999; //first time must save
  1504.  
  1505. let uid_A = location.pathname.replace(/[^\d+]/g, '') + '.' + location.search.replace(/[^\d+]/g, '');
  1506. let _uid = location.hostname.replace('www.', '').toLowerCase() + '!' + location.pathname.toLowerCase() + 'A' + uid_A + 'W' + player.videoWidth + 'H' + player.videoHeight + 'L' + (player.duration << 0);
  1507.  
  1508. digestMessage(_uid).then(function(_uid_encrypted) {
  1509.  
  1510. let d = +new Date;
  1511.  
  1512. let recordedTime = null;
  1513.  
  1514. ;
  1515. (function() {
  1516. //read the last record only;
  1517.  
  1518. let k1 = '_h5_player_play_progress_';
  1519. let k1n = '_play_progress_';
  1520. let k2 = _uid_encrypted;
  1521. let k3 = k1 + k2;
  1522. let k3n = k1n + k2;
  1523. let m2 = Store._keys().filter(key => key.substr(0, k3.length) == k3); //all progress records for this video
  1524. let m2v = m2.map(keyName => +(keyName.split('+')[1] || '0'))
  1525. let m2vMax = Math.max(0, ...m2v)
  1526. if (!m2vMax) recordedTime = null;
  1527. else {
  1528. let _json_recordedTime = null;
  1529. _json_recordedTime = Store.read(k3n + '+' + m2vMax);
  1530. if (!_json_recordedTime) _json_recordedTime = {};
  1531. else _json_recordedTime = jsonParse(_json_recordedTime);
  1532. if (typeof _json_recordedTime == 'object') recordedTime = _json_recordedTime;
  1533. else recordedTime = null;
  1534. recordedTime = typeof recordedTime == 'object' ? recordedTime.t : recordedTime;
  1535. if (typeof recordedTime == 'number' && (+recordedTime >= 0 || +recordedTime <= 0)) {
  1536.  
  1537. } else if (typeof recordedTime == 'string' && recordedTime.length > 0 && (+recordedTime >= 0 || +recordedTime <= 0)) {
  1538. recordedTime = +recordedTime
  1539. } else {
  1540. recordedTime = null
  1541. }
  1542. }
  1543. if (recordedTime !== null) {
  1544. player._h5player_lastrecord_ = recordedTime;
  1545. } else {
  1546. player._h5player_lastrecord_ = null;
  1547. }
  1548. if (player._h5player_lastrecord_ > 5) {
  1549. consoleLog('last record playing', player._h5player_lastrecord_);
  1550. setTimeout(function() {
  1551. $hs._tips(player, `Press Shift-R to restore Last Playback: ${$hs.toolFormatCT(player._h5player_lastrecord_)}`, 5000, 4000)
  1552. }, 1000)
  1553. }
  1554.  
  1555. })();
  1556. // delay the recording by 5.4s => prevent ads or mis operation
  1557. setTimeout(function() {
  1558.  
  1559. let k1 = '_h5_player_play_progress_';
  1560. let k1n = '_play_progress_';
  1561. let k2 = _uid_encrypted;
  1562. let k3 = k1 + k2;
  1563. let k3n = k1n + k2;
  1564.  
  1565. //re-read all the localStorage keys
  1566. let m1 = Store._keys().filter(key => key.substr(0, k1.length) == k1); //all progress records in this site
  1567. let p = m1.length + 1;
  1568.  
  1569. for (const key of m1) { //all progress records for this video
  1570. if (key.substr(0, k3.length) == k3) {
  1571. Store._removeItem(key); //remove previous record for the current video
  1572. p--;
  1573. }
  1574. }
  1575.  
  1576. if (recordedTime !== null) {
  1577. Store.save(k3n + '+' + d, jsonStringify({
  1578. 't': recordedTime
  1579. })) //prevent loss of last record
  1580. }
  1581.  
  1582. const _record_max_ = 48;
  1583. const _record_keep_ = 26;
  1584.  
  1585. if (p > _record_max_) {
  1586. //exisiting 48 records for one site;
  1587. //keep only 26 records
  1588.  
  1589. const comparator = (a, b) => (a.t < b.t ? -1 : a.t > b.t ? 1 : 0);
  1590.  
  1591. m1
  1592. .map(keyName => ({
  1593. keyName,
  1594. t: +(keyName.split('+')[1] || '0')
  1595. }))
  1596. .sort(comparator)
  1597. .slice(0, -_record_keep_)
  1598. .forEach((item) => localStorage.removeItem(item.keyName));
  1599.  
  1600. consoleLog(`stored progress: reduced to ${_record_keep_}`)
  1601. }
  1602.  
  1603. player.setAttribute('_h5p_uid_encrypted', _uid_encrypted + '+' + d);
  1604.  
  1605. //try to start recording
  1606. if (player._record_continuous) player._record_continuous.playingWithRecording();
  1607.  
  1608. }, 5400);
  1609.  
  1610. })
  1611.  
  1612. },
  1613. bindDocEvents: function(rootNode) {
  1614. if (!rootNode._onceBindedDocEvents) {
  1615.  
  1616. rootNode._onceBindedDocEvents = true;
  1617. rootNode.addEventListener('keydown', $hs.handlerRootKeyDownEvent, true)
  1618. document._debug_rootNode_ = rootNode;
  1619.  
  1620. rootNode.addEventListener('mouseenter', $hs.handlerElementMouseEnter, true)
  1621. rootNode.addEventListener('mousedown', $hs.handlerElementMouseDown, true)
  1622. rootNode.addEventListener('dblclick', $hs.handlerElementDblClick, true)
  1623. rootNode.addEventListener('wheel', $hs.handlerElementWheelTuneVolume, {
  1624. passive: false
  1625. });
  1626. // wheel - bubble events to keep it simple (i.e. it must be passive:false & capture:false)
  1627.  
  1628. }
  1629. },
  1630. fireGlobalInit: function() {
  1631. if ($hs.intVideoInitCount != 1) return;
  1632. if (!$hs.varSrcList) $hs.varSrcList = {};
  1633. $hs.isLostFocus = null;
  1634. try {
  1635. //iframe may not be able to control top window
  1636. //error; just ignore with async
  1637. let topDoc = window.top && window.top.document ? window.top.document : null;
  1638. if (topDoc) {
  1639. topDoc.addEventListener('focusout', $hs.handlerDocFocusOut, true)
  1640. topDoc.addEventListener('focusin', $hs.handlerDocFocusIn, true)
  1641. }
  1642.  
  1643. } catch (e) {}
  1644. Store.clearInvalid(_sVersion_)
  1645. },
  1646. onVideoTriggering: function() {
  1647.  
  1648.  
  1649. // initialize a single video player - h5Player.playerInstance
  1650.  
  1651. /**
  1652. * 初始化播放器實例
  1653. */
  1654. let player = $hs.playerInstance
  1655. if (!player) return
  1656.  
  1657. let vpid = player.getAttribute('_h5ppid');
  1658.  
  1659. if (!vpid) return;
  1660.  
  1661. let firstTime = !!$hs.initTips()
  1662. if (firstTime) {
  1663. // first time to trigger this player
  1664. if (!player.hasAttribute('playsinline')) player.setAttribute('playsinline', 'playsinline');
  1665. if (!player.hasAttribute('x-webkit-airplay')) player.setAttribute('x-webkit-airplay', 'deny');
  1666. if (!player.hasAttribute('preload')) player.setAttribute('preload', 'auto');
  1667. //player.style['image-rendering'] = 'crisp-edges';
  1668. $hs.playbackRate = $hs.getPlaybackRate()
  1669. }
  1670.  
  1671. },
  1672. getPlaybackRate: function() {
  1673. let playbackRate = Store.read('_playback_rate_') || $hs.playbackRate
  1674. return Number(Number(playbackRate).toFixed(1))
  1675. },
  1676. getPlayerBlockElement: function(player, useCache) {
  1677.  
  1678. let layoutBox = null,
  1679. wPlayer = null
  1680.  
  1681. if (!player || !player.offsetHeight || !player.offsetWidth || !player.parentNode) {
  1682. return null;
  1683. }
  1684.  
  1685.  
  1686. if (useCache === true) {
  1687. let vpid = player.getAttribute('_h5ppid');
  1688. let actionBoxRelation = $hs.actionBoxRelations[vpid]
  1689. if (actionBoxRelation && actionBoxRelation.mutationCount === 0) {
  1690. return actionBoxRelation.wPlayer
  1691. }
  1692. }
  1693.  
  1694.  
  1695. //without checkActiveBox, just a DOM for you to append tipsDom
  1696.  
  1697. function oWH(elm) {
  1698. return [elm.offsetWidth, elm.offsetHeight].join(',');
  1699. }
  1700.  
  1701. function search_nodes() {
  1702.  
  1703. wPlayer = player; // NOT NULL
  1704. layoutBox = wPlayer.parentNode; // NOT NULL
  1705.  
  1706. while (layoutBox.parentNode && layoutBox.nodeType == 1 && layoutBox.offsetHeight == 0) {
  1707. wPlayer = layoutBox; // NOT NULL
  1708. layoutBox = layoutBox.parentNode; // NOT NULL
  1709. }
  1710. //container must be with offsetHeight
  1711.  
  1712. while (layoutBox.parentNode && layoutBox.nodeType == 1 && layoutBox.offsetHeight < player.offsetHeight) {
  1713. wPlayer = layoutBox; // NOT NULL
  1714. layoutBox = layoutBox.parentNode; // NOT NULL
  1715. }
  1716. //container must have height >= player height
  1717.  
  1718. const layoutOWH = oWH(layoutBox)
  1719. //const playerOWH=oWH(player)
  1720.  
  1721. //skip all inner wraps
  1722. while (layoutBox.parentNode && layoutBox.nodeType == 1 && oWH(layoutBox.parentNode) == layoutOWH) {
  1723. wPlayer = layoutBox; // NOT NULL
  1724. layoutBox = layoutBox.parentNode; // NOT NULL
  1725. }
  1726.  
  1727. // oWH of layoutBox.parentNode != oWH of layoutBox and layoutBox.offsetHeight >= player.offsetHeight
  1728.  
  1729. }
  1730.  
  1731. search_nodes();
  1732.  
  1733. if (layoutBox.nodeType == 11) {
  1734.  
  1735.  
  1736. //shadowRoot without html and body
  1737. let shadowChild, shadowElm_container, shadowElm_head, shadowElm_html;
  1738. let rootNode = getRoot(player);
  1739. if (rootNode.querySelectorAll('html,body').length < 2) {
  1740.  
  1741. shadowElm_container = player.ownerDocument.createElement('BODY')
  1742. rootNode.insertBefore(shadowElm_container, rootNode.firstChild)
  1743.  
  1744. while (shadowChild = shadowElm_container.nextSibling) shadowElm_container.appendChild(shadowChild);
  1745.  
  1746. shadowElm_head = rootNode.insertBefore(player.ownerDocument.createElement('HEAD'), shadowElm_container)
  1747.  
  1748. shadowElm_html = rootNode.insertBefore(player.ownerDocument.createElement('HTML'), shadowElm_head)
  1749.  
  1750. shadowElm_container.setAttribute('style', 'padding:0;margin:0;border:0; box-sizing: border-box;')
  1751. shadowElm_html.setAttribute('style', 'padding:0;margin:0;border:0; box-sizing: border-box;')
  1752.  
  1753. shadowElm_html.appendChild(shadowElm_head)
  1754. shadowElm_html.appendChild(shadowElm_container)
  1755.  
  1756. }
  1757.  
  1758. search_nodes();
  1759.  
  1760. }
  1761.  
  1762. //condition:
  1763. //!layoutBox.parentNode || layoutBox.nodeType != 1 || layoutBox.offsetHeight > player.offsetHeight
  1764.  
  1765. // layoutBox is a node contains <video> and offsetHeight>=video.offsetHeight
  1766. // wPlayer is a HTML Element (nodeType==1)
  1767. // you can insert the DOM element into the layoutBox
  1768.  
  1769. if (layoutBox && wPlayer && layoutBox.nodeType === 1 && wPlayer.parentNode == layoutBox && layoutBox.parentNode) return wPlayer;
  1770. throw 'unknown error';
  1771.  
  1772. },
  1773. getCommonContainer: function(elm1, elm2) {
  1774.  
  1775. let box1 = elm1;
  1776. let box2 = elm2;
  1777.  
  1778. while (box1 && box2) {
  1779. if (box1.contains(box2) || box2.contains(box1)) {
  1780. break;
  1781. }
  1782. box1 = box1.parentNode;
  1783. box2 = box2.parentNode;
  1784. }
  1785.  
  1786. let layoutBox = null;
  1787.  
  1788. box1 = (box1 && box1.contains(elm2)) ? box1 : null;
  1789. box2 = (box2 && box2.contains(elm1)) ? box2 : null;
  1790.  
  1791. if (box1 && box2) layoutBox = box1.contains(box2) ? box2 : box1;
  1792. else layoutBox = box1 || box2 || null;
  1793.  
  1794. return layoutBox
  1795.  
  1796. },
  1797. change_layoutBox: function(tipsDom) {
  1798. let player = $hs.player()
  1799. if (!player) return;
  1800. let wPlayer = $hs.getPlayerBlockElement(player, true);
  1801. let layoutBox = wPlayer.parentNode;
  1802.  
  1803. if ((layoutBox && layoutBox.nodeType == 1) && (!tipsDom.parentNode || tipsDom.parentNode !== layoutBox)) {
  1804.  
  1805. consoleLog('changed_layoutBox')
  1806. layoutBox.insertBefore(tipsDom, wPlayer);
  1807.  
  1808. }
  1809. },
  1810.  
  1811. queryFullscreenBtnsIndependant: function(parentNode) {
  1812.  
  1813. let btns = [];
  1814.  
  1815. function elmCallback(elm) {
  1816.  
  1817. let hasClickListeners = null,
  1818. childElementCount = null,
  1819. isVisible = null,
  1820. btnElm = elm;
  1821. var pElm = elm;
  1822. while (pElm && pElm.nodeType === 1 && pElm != parentNode && pElm.querySelector('video') === null) {
  1823.  
  1824. if ((typeof pElm.onclick == 'function') || (pElm._listeners && pElm._listeners.click && pElm._listeners.click.funcCount > 0)) {
  1825. hasClickListeners = true
  1826. btnElm = pElm;
  1827. break;
  1828. }
  1829.  
  1830. pElm = pElm.parentNode;
  1831. }
  1832. if (btns.indexOf(btnElm) >= 0) return; //btn>a.fullscreen-1>b.fullscreen-2>c.fullscreen-3
  1833.  
  1834. if ('_listeners' in elm) {
  1835.  
  1836. //hasClickListeners = elm._listeners && elm._listeners.click && elm._listeners.click.funcCount > 0
  1837.  
  1838. }
  1839.  
  1840. if ('childElementCount' in elm) {
  1841.  
  1842. childElementCount = elm.childElementCount;
  1843.  
  1844. }
  1845. if ('offsetParent' in elm) {
  1846. isVisible = !!elm.offsetParent; //works with parent/self display none; not work with visiblity hidden / opacity0
  1847.  
  1848. }
  1849.  
  1850. if (hasClickListeners) {
  1851. let btn = {
  1852. elm,
  1853. btnElm,
  1854. isVisible,
  1855. hasClickListeners,
  1856. childElementCount,
  1857. isContained: null
  1858. };
  1859.  
  1860. console.log('btnElm', btnElm)
  1861.  
  1862. btns.push(btnElm)
  1863.  
  1864. }
  1865. }
  1866.  
  1867.  
  1868. for (const elm of parentNode.querySelectorAll('[class*="full"][class*="screen"]')) {
  1869. let className = (elm.getAttribute('class') || "");
  1870. if (/\b(fullscreen|full-screen)\b/i.test(className.replace(/([A-Z][a-z]+)/g, '-$1-').replace(/[\_\-]+/g, '-'))) {
  1871. elmCallback(elm)
  1872. }
  1873. }
  1874.  
  1875.  
  1876. for (const elm of parentNode.querySelectorAll('[id*="full"][id*="screen"]')) {
  1877. let idName = (elm.getAttribute('id') || "");
  1878. if (/\b(fullscreen|full-screen)\b/i.test(idName.replace(/([A-Z][a-z]+)/g, '-$1-').replace(/[\_\-]+/g, '-'))) {
  1879. elmCallback(elm)
  1880. }
  1881. }
  1882.  
  1883. for (const elm of parentNode.querySelectorAll('[name*="full"][name*="screen"]')) {
  1884. let nName = (elm.getAttribute('name') || "");
  1885. if (/\b(fullscreen|full-screen)\b/i.test(nName.replace(/([A-Z][a-z]+)/g, '-$1-').replace(/[\_\-]+/g, '-'))) {
  1886. elmCallback(elm)
  1887. }
  1888. }
  1889.  
  1890.  
  1891. return btns;
  1892.  
  1893. },
  1894. exclusiveElements: function(elms) {
  1895.  
  1896. //not containing others
  1897. let res = [];
  1898.  
  1899. for (const roleElm of elms) {
  1900.  
  1901. let isContained = false;
  1902. for (const testElm of elms) {
  1903. if (testElm != roleElm && roleElm.contains(testElm)) {
  1904. isContained = true;
  1905. break;
  1906. }
  1907. }
  1908. if (!isContained) res.push(roleElm)
  1909. }
  1910. return res;
  1911.  
  1912. },
  1913. callFullScreenBtn: function() {
  1914.  
  1915. let player = $hs.player()
  1916. if (!player || !player.ownerDocument || !('exitFullscreen' in player.ownerDocument)) return;
  1917.  
  1918. let btnElement = null;
  1919.  
  1920. let vpid = player.getAttribute('_h5ppid') || null;
  1921.  
  1922. if (!vpid) return;
  1923.  
  1924.  
  1925. const chFull = $hs.toolCheckFullScreen(player.ownerDocument);
  1926.  
  1927.  
  1928.  
  1929. let actionBoxRelation = $hs.actionBoxRelations[vpid];
  1930.  
  1931. if (chFull === true) {
  1932. player.ownerDocument.exitFullscreen();
  1933. return;
  1934. } else if (chFull === false) {
  1935. if (actionBoxRelation && actionBoxRelation.actionBox) {
  1936. let actionBox = actionBoxRelation.actionBox;
  1937. let btnElements = actionBoxRelation.fullscreenBtns;
  1938. if (btnElements && btnElements.length > 0) {
  1939. //consoleLog('fullscreen btn', btnElements)
  1940.  
  1941. const lens_elemsClassName = btnElements.map(elm => elm.className.length)
  1942. const j_elemsClassName = Math.min(...lens_elemsClassName)
  1943. let btnElement_idx = lens_elemsClassName.lastIndexOf(j_elemsClassName)
  1944. // pick the last btn if there is more than one
  1945. btnElement = btnElements[btnElement_idx];
  1946.  
  1947. //consoleLog('original fullscreen')
  1948. window.requestAnimationFrame(() => btnElement.click());
  1949. return;
  1950. }
  1951. }
  1952.  
  1953. }
  1954.  
  1955. let gPlayer = null;
  1956. if (actionBoxRelation && actionBoxRelation.actionBox) {
  1957. gPlayer = actionBoxRelation.actionBox;
  1958. } else if (actionBoxRelation && actionBoxRelation.layoutBox) {
  1959. gPlayer = actionBoxRelation.layoutBox;
  1960. } else {
  1961. gPlayer = player;
  1962. }
  1963.  
  1964. consoleLog('DOM fullscreen', gPlayer)
  1965. try {
  1966. gPlayer.requestFullscreen() //known bugs : TypeError: fullscreen error
  1967. } catch (e) {
  1968. consoleLogF('fullscreen error', e)
  1969. }
  1970.  
  1971.  
  1972. },
  1973. /* 設置播放速度 */
  1974. setPlaybackRate: function(num, flagTips) {
  1975. let player = $hs.player()
  1976. let curPlaybackRate
  1977. if (num) {
  1978. num = +num
  1979. if (num > 0) { // also checking the type of variable
  1980. curPlaybackRate = num < 0.1 ? 0.1 : +(num.toFixed(1))
  1981. } else {
  1982. console.error('h5player: 播放速度轉換出錯')
  1983. return false
  1984. }
  1985. } else {
  1986. curPlaybackRate = $hs.getPlaybackRate()
  1987. }
  1988. /* 記錄播放速度的信息 */
  1989.  
  1990. let changed = curPlaybackRate !== player.playbackRate;
  1991.  
  1992. if (curPlaybackRate !== player.playbackRate) {
  1993.  
  1994. Store.save('_playback_rate_', curPlaybackRate + '')
  1995. $hs.playbackRate = curPlaybackRate
  1996. player.playbackRate = curPlaybackRate
  1997. /* 本身處於1被播放速度的時候不再提示 */
  1998. //if (!num && curPlaybackRate === 1) return;
  1999.  
  2000. }
  2001.  
  2002. flagTips = (flagTips < 0) ? false : (flagTips > 0) ? true : changed;
  2003. if (flagTips) $hs.tips('Playback speed: ' + player.playbackRate + 'x')
  2004. },
  2005. tuneCurrentTime: function(amount, flagTips) {
  2006. let _amount = +(+amount).toFixed(1);
  2007. let player = $hs.player();
  2008. if (_amount >= 0 || _amount < 0) {} else {
  2009. return;
  2010. }
  2011.  
  2012. let newCurrentTime = player.currentTime + _amount;
  2013. if (newCurrentTime < 0) newCurrentTime = 0;
  2014. if (newCurrentTime > player.duration) newCurrentTime = player.duration;
  2015.  
  2016. let changed = newCurrentTime != player.currentTime && newCurrentTime >= 0 && newCurrentTime <= player.duration;
  2017.  
  2018. if (changed) {
  2019. //player.currentTime = newCurrentTime;
  2020. player.pause();
  2021. let t_ch = (Math.random() / 5 + .75);
  2022. player.currentTime = newCurrentTime * t_ch + player.currentTime * (1.0 - t_ch);
  2023. setTimeout(() => {
  2024. player.play();
  2025. player.currentTime = newCurrentTime;
  2026. }, 33);
  2027. flagTips = (flagTips < 0) ? false : (flagTips > 0) ? true : changed;
  2028. $hs.tips(false);
  2029. if (flagTips) {
  2030. if (_amount > 0) $hs.tips(_amount + ' Sec. Forward', undefined, 3000);
  2031. else $hs.tips(-_amount + ' Sec. Backward', undefined, 3000)
  2032. }
  2033. }
  2034.  
  2035. },
  2036. tuneVolume: function(amount) {
  2037. let _amount = +(+amount).toFixed(2);
  2038.  
  2039. let player = $hs.player()
  2040.  
  2041. let newVol = player.volume + _amount;
  2042. if (newVol < 0) newVol = 0;
  2043. if (newVol > 1) newVol = 1;
  2044. let chVol = player.volume !== newVol && newVol >= 0 && newVol <= 1;
  2045.  
  2046. if (chVol) {
  2047.  
  2048. if (_amount > 0 && player.volume < 1) {
  2049. player.volume = newVol // positive
  2050. } else if (_amount < 0 && player.volume > 0) {
  2051. player.volume = newVol // negative
  2052. }
  2053. $hs.tips(false);
  2054. $hs.tips('Volume: ' + dround(player.volume * 100) + '%', undefined)
  2055. }
  2056. },
  2057. switchPlayStatus: function() {
  2058. let player = $hs.player()
  2059. if (player.paused) {
  2060. player.play()
  2061. if (player._isThisPausedBefore_) {
  2062. $hs.tips(false);
  2063. $hs.tips('Playback resumed', undefined, 2500)
  2064. }
  2065. } else {
  2066. player.pause()
  2067. $hs.tips(false);
  2068. $hs.tips('Playback paused', undefined, 2500)
  2069. }
  2070. },
  2071. tipsClassName: 'html_player_enhance_tips',
  2072. _tips: function(player, str, duration, order) {
  2073.  
  2074.  
  2075. if (!player.getAttribute('_h5player_tips')) $hs.initTips();
  2076.  
  2077. let tipsSelector = '#' + (player.getAttribute('_h5player_tips') || $hs.tipsClassName) //if this attribute still doesnt exist, set it to the base cls name
  2078. let tipsDom = getRoot(player).querySelector(tipsSelector)
  2079. if (!tipsDom) {
  2080. consoleLog('init h5player tips dom error...')
  2081. return false
  2082. }
  2083. $hs.change_layoutBox(tipsDom);
  2084.  
  2085.  
  2086.  
  2087. if (str === false) {
  2088. tipsDom.textContent = '';
  2089. tipsDom.setAttribute('_potTips_', '0')
  2090. } else {
  2091. order = order || 1000
  2092. tipsDom.tipsOrder = tipsDom.tipsOrder || 0;
  2093.  
  2094. let shallDisplay = true
  2095. if (order < tipsDom.tipsOrder && getComputedStyle(tipsDom).opacity > 0) shallDisplay = false
  2096.  
  2097. if (shallDisplay) {
  2098. tipsDom._playerElement = player;
  2099. tipsDom._playerVPID = player.getAttribute('_h5ppid');
  2100. tipsDom._playerBlockElm = $hs.getPlayerBlockElement(player, true)
  2101.  
  2102. if (duration === undefined) duration = 2000
  2103. tipsDom.textContent = str
  2104. tipsDom.setAttribute('_potTips_', '2')
  2105.  
  2106. if (duration > 0) {
  2107. $ws.requestAnimationFrame(() => tipsDom.setAttribute('_potTips_', '1'))
  2108. } else {
  2109. order = -1;
  2110. }
  2111.  
  2112. tipsDom.tipsOrder = order
  2113.  
  2114. }
  2115.  
  2116. }
  2117.  
  2118.  
  2119. if (window.ResizeObserver) {
  2120. //observe not fire twice for the same element.
  2121. if (!$hs.observer_resizeVideos) $hs.observer_resizeVideos = new ResizeObserver(hanlderResizeVideo)
  2122. $hs.observer_resizeVideos.observe(tipsDom._playerBlockElm.parentNode)
  2123. $hs.observer_resizeVideos.observe(tipsDom._playerBlockElm)
  2124. $hs.observer_resizeVideos.observe(player)
  2125. }
  2126. //ensure function called
  2127. window.requestAnimationFrame(() => $hs.fixNonBoxingVideoTipsPosition(tipsDom, player))
  2128.  
  2129. },
  2130. tips: function(str, duration, order) {
  2131. let player = $hs.player()
  2132. if (!player) {
  2133. consoleLog('h5Player Tips:', str)
  2134. return true
  2135. }
  2136.  
  2137. return $hs._tips(player, str, duration, order)
  2138.  
  2139. },
  2140. listOfTipsDom: [],
  2141. getPotTips: function(arr) {
  2142. const res = [];
  2143. for (const tipsDom of $hs.listOfTipsDom) {
  2144. if (tipsDom && tipsDom.nodeType > 0 && tipsDom.parentNode && tipsDom.ownerDocument) {
  2145. if (tipsDom._playerBlockElm && tipsDom._playerBlockElm.parentNode) {
  2146. const potTipsAttr = tipsDom.getAttribute('_potTips_')
  2147. if (arr.indexOf(potTipsAttr) >= 0) res.push(tipsDom)
  2148. }
  2149. }
  2150. }
  2151. return res;
  2152. },
  2153. initTips: function() {
  2154. /* 設置提示DOM的樣式 */
  2155. let player = $hs.player()
  2156. let shadowRoot = getRoot(player);
  2157. let doc = player.ownerDocument;
  2158. //console.log((document.documentElement.qq=player),shadowRoot,'xax')
  2159. let parentNode = player.parentNode
  2160. let tcn = player.getAttribute('_h5player_tips') || ($hs.tipsClassName + '_' + (+new Date));
  2161. player.setAttribute('_h5player_tips', tcn)
  2162. if (shadowRoot.querySelector('#' + tcn)) return false;
  2163.  
  2164. if (!shadowRoot._onceAddedCSS) {
  2165. shadowRoot._onceAddedCSS = true;
  2166.  
  2167. let cssStyle = `
  2168. [_potTips_="1"]{
  2169. animation: 2s linear 0s normal forwards 1 delayHide;
  2170. }
  2171. [_potTips_="0"]{
  2172. opacity:0; transform:translate(-9999px);
  2173. }
  2174. [_potTips_="2"]{
  2175. opacity:.95; transform: translate(0,0);
  2176. }
  2177.  
  2178. @keyframes delayHide{
  2179. 0%, 99% { opacity:0.95; transform: translate(0,0); }
  2180. 100% { opacity:0; transform:translate(-9999px); }
  2181. }
  2182. ` + `
  2183. [_potTips_]{
  2184. font-weight: bold !important;
  2185. position: absolute !important;
  2186. z-index: 999 !important;
  2187. font-size: ${$hs.fontSize || 16}px !important;
  2188. padding: 0px !important;
  2189. border:none !important;
  2190. background: rgba(0,0,0,0) !important;
  2191. color:#738CE6 !important;
  2192. text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
  2193. top: 50%;
  2194. left: 50%;
  2195. max-width:500px;max-height:50px;
  2196. border-radius:3px;
  2197. font-family: 'microsoft yahei', Verdana, Geneva, sans-serif;
  2198. pointer-events: none;
  2199. }
  2200. body div[_potTips_]{
  2201. -webkit-user-select: none !important;
  2202. -moz-user-select: none !important;
  2203. -ms-user-select: none !important;
  2204. user-select: none !important;
  2205. -webkit-touch-callout: none !important;
  2206. -webkit-user-select: none !important;
  2207. -khtml-user-drag: none !important;
  2208. -khtml-user-select: none !important;
  2209. -moz-user-select: none !important;
  2210. -moz-user-select: -moz-none !important;
  2211. -ms-user-select: none !important;
  2212. user-select: none !important;
  2213. }
  2214. `.replace(/\r\n/g, '');
  2215.  
  2216. let cssContainer = (shadowRoot.querySelector('head') || shadowRoot.querySelector('html') || document.documentElement);
  2217.  
  2218. domTool.addStyle(cssStyle, cssContainer);
  2219.  
  2220. }
  2221.  
  2222. let tipsDom = doc.createElement('div')
  2223.  
  2224. tipsDom.addEventListener(crossBrowserTransition('animation'), function(e) {
  2225. if (this.getAttribute('_potTips_') == '1') {
  2226. this.setAttribute('_potTips_', '0')
  2227. }
  2228. })
  2229.  
  2230. tipsDom.id = tcn;
  2231. tipsDom.setAttribute('_potTips_', '0');
  2232. $hs.listOfTipsDom.push(tipsDom);
  2233. $hs.change_layoutBox(tipsDom);
  2234.  
  2235. return true;
  2236. },
  2237.  
  2238. responsiveSizing: function(container, elm) {
  2239.  
  2240. let gcssP = getComputedStyle(container);
  2241.  
  2242. let gcssE = getComputedStyle(elm);
  2243.  
  2244. //console.log(gcssE.left,gcssP.width)
  2245. let elmBound = {
  2246. left: parseFloat(gcssE.left) / parseFloat(gcssP.width),
  2247. width: parseFloat(gcssE.width) / parseFloat(gcssP.width),
  2248. top: parseFloat(gcssE.top) / parseFloat(gcssP.height),
  2249. height: parseFloat(gcssE.height) / parseFloat(gcssP.height)
  2250. };
  2251.  
  2252. let elm00 = [elmBound.left, elmBound.top];
  2253. let elm01 = [elmBound.left + elmBound.width, elmBound.top];
  2254. let elm10 = [elmBound.left, elmBound.top + elmBound.height];
  2255. let elm11 = [elmBound.left + elmBound.width, elmBound.top + elmBound.height];
  2256.  
  2257. return {
  2258. elm00,
  2259. elm01,
  2260. elm10,
  2261. elm11,
  2262. plw: elmBound.width,
  2263. plh: elmBound.height
  2264. };
  2265.  
  2266. },
  2267.  
  2268. fixNonBoxingVideoTipsPosition: function(tipsDom, player) {
  2269.  
  2270. if (!tipsDom || !player) return;
  2271.  
  2272. let ct = $hs.getCommonContainer(tipsDom, player)
  2273.  
  2274. if (!ct) return;
  2275.  
  2276. //relative
  2277.  
  2278. let elm00 = $hs.responsiveSizing(ct, player).elm00;
  2279.  
  2280. if (isNaN(elm00[0]) || isNaN(elm00[1])) {
  2281.  
  2282. [tipsDom.style.left, tipsDom.style.top] = [player.style.left, player.style.top];
  2283. //eg auto
  2284. } else {
  2285.  
  2286. let rlm00 = elm00.map(t => (t * 100).toFixed(2) + '%');
  2287. [tipsDom.style.left, tipsDom.style.top] = rlm00;
  2288.  
  2289. }
  2290.  
  2291. // absolute
  2292.  
  2293. let _offset = {
  2294. left: 10,
  2295. top: 15
  2296. };
  2297.  
  2298. let customOffset = {
  2299. left: _offset.left,
  2300. top: _offset.top
  2301. };
  2302. let p = tipsDom.getBoundingClientRect();
  2303. let q = player.getBoundingClientRect();
  2304. let currentPos = [p.left, p.top];
  2305.  
  2306. let targetPos = [q.left + player.offsetWidth * 0 + customOffset.left, q.top + player.offsetHeight * 0 + customOffset.top];
  2307.  
  2308. let mL = +tipsDom.style.marginLeft.replace('px', '') || 0;
  2309. if (isNaN(mL)) mL = 0;
  2310. let mT = +tipsDom.style.marginTop.replace('px', '') || 0;
  2311. if (isNaN(mT)) mT = 0;
  2312.  
  2313. let z1 = -(currentPos[0] - targetPos[0]);
  2314. let z2 = -(currentPos[1] - targetPos[1]);
  2315.  
  2316. if (z1 || z2) {
  2317.  
  2318. let y1 = z1 + mL;
  2319. let y2 = z2 + mT;
  2320.  
  2321. tipsDom.style.marginLeft = y1 + 'px';
  2322. tipsDom.style.marginTop = y2 + 'px';
  2323.  
  2324. }
  2325. },
  2326.  
  2327. playerTrigger: function(player, event) {
  2328. if (!player || !event) return
  2329. const pCode = event.code;
  2330. let keyAsm = (event.shiftKey ? SHIFT : 0) | ((event.ctrlKey || event.metaKey) ? CTRL : 0) | (event.altKey ? ALT : 0);
  2331.  
  2332.  
  2333. let vpid = player.getAttribute('_h5ppid') || null;
  2334. if (!vpid) return;
  2335. let playerConf = playerConfs[vpid]
  2336. if (!playerConf) return;
  2337.  
  2338. //shift + key
  2339. if (keyAsm == SHIFT) {
  2340. // 網頁FULLSCREEN
  2341. if (pCode === 'Enter') {
  2342. $hs.callFullScreenBtn()
  2343. return TERMINATE
  2344. } else if (pCode == 'KeyF') {
  2345. //change unsharpen filter
  2346.  
  2347. let resList = ["unsharpen3_05", "unsharpen3_10", "unsharpen5_05", "unsharpen5_10", "unsharpen9_05", "unsharpen9_10"]
  2348. let res = (prompt("Enter the unsharpen mask\n(" + resList.map(x => '"' + x + '"').join(', ') + ")", "unsharpen9_05") || "").toLowerCase();
  2349. if (resList.indexOf(res) < 0) res = ""
  2350. GM_setValue("unsharpen_mask", res)
  2351. for (const el of document.querySelectorAll('video[_h5p_uid_encrypted]')) {
  2352. if (el.style.filter == "" || el.style.filter) {
  2353. let filterStr1 = el.style.filter.replace(/\s*url\(\"#_h5p_unsharpen[\d\_]+\"\)/, '');
  2354. let filterStr2 = (res.length > 0 ? ' url("#_h5p_' + res + '")' : '')
  2355. el.style.filter = filterStr1 + filterStr2;
  2356. }
  2357. }
  2358. return TERMINATE
  2359.  
  2360. }
  2361. // 進入或退出畫中畫模式
  2362. else if (pCode == 'KeyP') {
  2363. $hs.pictureInPicture(player)
  2364.  
  2365. return TERMINATE
  2366. } else if (pCode == 'KeyR') {
  2367. if (player._h5player_lastrecord_ !== null && (player._h5player_lastrecord_ >= 0 || player._h5player_lastrecord_ <= 0)) {
  2368. $hs.setPlayProgress(player, player._h5player_lastrecord_)
  2369.  
  2370. return TERMINATE
  2371. }
  2372.  
  2373. } else if (pCode == 'KeyO') {
  2374. let _debug_h5p_logging_ch = false;
  2375. try {
  2376. Store._setItem('_h5_player_sLogging_', 1 - Store._getItem('_h5_player_sLogging_'))
  2377. _debug_h5p_logging_ = +Store._getItem('_h5_player_sLogging_') > 0;
  2378. _debug_h5p_logging_ch = true;
  2379. } catch (e) {
  2380.  
  2381. }
  2382. consoleLogF('_debug_h5p_logging_', !!_debug_h5p_logging_, 'changed', _debug_h5p_logging_ch)
  2383.  
  2384. if (_debug_h5p_logging_ch) {
  2385.  
  2386. return TERMINATE
  2387. }
  2388. } else if (pCode == 'KeyT') {
  2389. if (/^blob/i.test(player.currentSrc)) {
  2390. alert(`The current video is ${player.currentSrc}\nSorry, it cannot be opened in PotPlayer.`);
  2391. } else {
  2392. let confirm_res = confirm(`The current video is ${player.currentSrc}\nDo you want to open it in PotPlayer?`);
  2393. if (confirm_res) window.open('potplayer://' + player.currentSrc, '_blank');
  2394. }
  2395. return TERMINATE
  2396. }
  2397.  
  2398.  
  2399.  
  2400. let videoScale = playerConf.vFactor;
  2401.  
  2402. function tipsForVideoScaling() {
  2403.  
  2404. playerConf.vFactor = +videoScale.toFixed(1);
  2405.  
  2406. playerConf.cssTransform();
  2407. let tipsMsg = `視頻縮放率:${ +(videoScale * 100).toFixed(2) }%`
  2408. if (playerConf.translate.x) {
  2409. tipsMsg += `,水平位移:${playerConf.translate.x}px`
  2410. }
  2411. if (playerConf.translate.y) {
  2412. tipsMsg += `,垂直位移:${playerConf.translate.y}px`
  2413. }
  2414. $hs.tips(false);
  2415. $hs.tips(tipsMsg)
  2416.  
  2417.  
  2418. }
  2419.  
  2420. // 視頻畫面縮放相關事件
  2421.  
  2422. switch (pCode) {
  2423. // shift+X:視頻縮小 -0.1
  2424. case 'KeyX':
  2425. videoScale -= 0.1
  2426. if (videoScale < 0.1) videoScale = 0.1;
  2427. tipsForVideoScaling();
  2428. return TERMINATE
  2429. break
  2430. // shift+C:視頻放大 +0.1
  2431. case 'KeyC':
  2432. videoScale += 0.1
  2433. if (videoScale > 16) videoScale = 16;
  2434. tipsForVideoScaling();
  2435. return TERMINATE
  2436. break
  2437. // shift+Z:視頻恢復正常大小
  2438. case 'KeyZ':
  2439. videoScale = 1.0
  2440. playerConf.translate.x = 0;
  2441. playerConf.translate.y = 0;
  2442. tipsForVideoScaling();
  2443. return TERMINATE
  2444. break
  2445. case 'ArrowRight':
  2446. playerConf.translate.x += 10
  2447. tipsForVideoScaling();
  2448. return TERMINATE
  2449. break
  2450. case 'ArrowLeft':
  2451. playerConf.translate.x -= 10
  2452. tipsForVideoScaling();
  2453. return TERMINATE
  2454. break
  2455. case 'ArrowUp':
  2456. playerConf.translate.y -= 10
  2457. tipsForVideoScaling();
  2458. return TERMINATE
  2459. break
  2460. case 'ArrowDown':
  2461. playerConf.translate.y += 10
  2462. tipsForVideoScaling();
  2463. return TERMINATE
  2464. break
  2465.  
  2466. }
  2467.  
  2468. }
  2469. // 防止其它無關組合鍵衝突
  2470. if (!keyAsm) {
  2471. let kControl = null
  2472. let newPBR, oldPBR, nv;
  2473. switch (pCode) {
  2474. // 方向鍵右→:快進3秒
  2475. case 'ArrowRight':
  2476. $hs.tuneCurrentTime($hs.skipStep);
  2477. return TERMINATE;
  2478. break;
  2479. // 方向鍵左←:後退3秒
  2480. case 'ArrowLeft':
  2481. $hs.tuneCurrentTime(-$hs.skipStep);
  2482. return TERMINATE;
  2483. break;
  2484. // 方向鍵上↑:音量升高 1%
  2485. case 'ArrowUp':
  2486. if ((player.muted && player.volume === 0) && player._volume > 0) {
  2487.  
  2488. player.muted = false;
  2489. player.volume = player._volume;
  2490. } else if (player.muted && (player.volume > 0 || !player._volume)) {
  2491. player.muted = false;
  2492. }
  2493. $hs.tuneVolume(0.01);
  2494. return TERMINATE;
  2495. break;
  2496. // 方向鍵下↓:音量降低 1%
  2497. case 'ArrowDown':
  2498.  
  2499. if ((player.muted && player.volume === 0) && player._volume > 0) {
  2500.  
  2501. player.muted = false;
  2502. player.volume = player._volume;
  2503. } else if (player.muted && (player.volume > 0 || !player._volume)) {
  2504. player.muted = false;
  2505. }
  2506. $hs.tuneVolume(-0.01);
  2507. return TERMINATE;
  2508. break;
  2509. // 空格鍵:暫停/播放
  2510. case 'Space':
  2511. $hs.switchPlayStatus();
  2512. return TERMINATE;
  2513. break;
  2514. // 按鍵X:減速播放 -0.1
  2515. case 'KeyX':
  2516. if (player.playbackRate > 0) {
  2517. $hs.tips(false);
  2518. $hs.setPlaybackRate(player.playbackRate - 0.1);
  2519. return TERMINATE
  2520. }
  2521. break;
  2522. // 按鍵C:加速播放 +0.1
  2523. case 'KeyC':
  2524. if (player.playbackRate < 16) {
  2525. $hs.tips(false);
  2526. $hs.setPlaybackRate(player.playbackRate + 0.1);
  2527. return TERMINATE
  2528. }
  2529.  
  2530. break;
  2531. // 按鍵Z:正常速度播放
  2532. case 'KeyZ':
  2533. $hs.tips(false);
  2534. oldPBR = player.playbackRate;
  2535. if (oldPBR != 1.0) {
  2536. player._playbackRate_z = oldPBR;
  2537. newPBR = 1.0;
  2538. } else if (player._playbackRate_z != 1.0) {
  2539. newPBR = player._playbackRate_z || 1.0;
  2540. player._playbackRate_z = 1.0;
  2541. } else {
  2542. newPBR = 1.0
  2543. player._playbackRate_z = 1.0;
  2544. }
  2545. $hs.setPlaybackRate(newPBR, 1)
  2546. return TERMINATE
  2547. break;
  2548. // 按鍵F:下一幀
  2549. case 'KeyF':
  2550. if (window.location.hostname === 'www.netflix.com') return /* netflix 的F鍵是FULLSCREEN的意思 */
  2551. $hs.tips(false);
  2552. if (!player.paused) player.pause()
  2553. player.currentTime += +(1 / playerConf.fps)
  2554. $hs.tips('Jump to: Next frame')
  2555. return TERMINATE
  2556. break;
  2557. // 按鍵D:上一幀
  2558. case 'KeyD':
  2559. $hs.tips(false);
  2560. if (!player.paused) player.pause()
  2561. player.currentTime -= +(1 / playerConf.fps)
  2562. $hs.tips('Jump to: Previous frame')
  2563. return TERMINATE
  2564. break;
  2565. // 按鍵E:亮度增加%
  2566. case 'KeyE':
  2567. $hs.tips(false);
  2568. nv = playerConf.setFilter('brightness', (v) => v + 0.1);
  2569. $hs.tips('Brightness: ' + dround(nv * 100) + '%')
  2570. return TERMINATE
  2571. break;
  2572. // 按鍵W:亮度減少%
  2573. case 'KeyW':
  2574. $hs.tips(false);
  2575. nv = playerConf.setFilter('brightness', (v) => v > 0.1 ? v - 0.1 : 0);
  2576. $hs.tips('Brightness: ' + dround(nv * 100) + '%')
  2577. return TERMINATE
  2578. break;
  2579. // 按鍵T:對比度增加%
  2580. case 'KeyT':
  2581. $hs.tips(false);
  2582. nv = playerConf.setFilter('contrast', (v) => v + 0.1);
  2583. $hs.tips('Contrast: ' + dround(nv * 100) + '%')
  2584. return TERMINATE
  2585. break;
  2586. // 按鍵R:對比度減少%
  2587. case 'KeyR':
  2588. $hs.tips(false);
  2589. nv = playerConf.setFilter('contrast', (v) => v > 0.1 ? v - 0.1 : 0);
  2590. $hs.tips('Contrast: ' + dround(nv * 100) + '%')
  2591. return TERMINATE
  2592. break;
  2593. // 按鍵U:飽和度增加%
  2594. case 'KeyU':
  2595. $hs.tips(false);
  2596. nv = playerConf.setFilter('saturate', (v) => v + 0.1);
  2597. $hs.tips('Saturate: ' + dround(nv * 100) + '%')
  2598. return TERMINATE
  2599. break;
  2600. // 按鍵Y:飽和度減少%
  2601. case 'KeyY':
  2602. $hs.tips(false);
  2603. nv = playerConf.setFilter('saturate', (v) => v > 0.1 ? v - 0.1 : 0);
  2604. $hs.tips('Saturate: ' + dround(nv * 100) + '%')
  2605. return TERMINATE
  2606. break;
  2607. // 按鍵O:色相增加 1 度
  2608. case 'KeyO':
  2609. $hs.tips(false);
  2610. nv = playerConf.setFilter('hue-rotate', (v) => v + 1);
  2611. $hs.tips('Hue: ' + nv + ' deg')
  2612. return TERMINATE
  2613. break;
  2614. // 按鍵I:色相減少 1 度
  2615. case 'KeyI':
  2616. $hs.tips(false);
  2617. nv = playerConf.setFilter('hue-rotate', (v) => v - 1);
  2618. $hs.tips('Hue: ' + nv + ' deg')
  2619. return TERMINATE
  2620. break;
  2621. // 按鍵K:模糊增加 0.1 px
  2622. case 'KeyK':
  2623. $hs.tips(false);
  2624. nv = playerConf.setFilter('blur', (v) => v + 0.1);
  2625. $hs.tips('Blur: ' + nv + ' px')
  2626. return TERMINATE
  2627. break;
  2628. // 按鍵J:模糊減少 0.1 px
  2629. case 'KeyJ':
  2630. $hs.tips(false);
  2631. nv = playerConf.setFilter('blur', (v) => v > 0.1 ? v - 0.1 : 0);
  2632. $hs.tips('Blur: ' + nv + ' px')
  2633. return TERMINATE
  2634. break;
  2635. // 按鍵Q:圖像復位
  2636. case 'KeyQ':
  2637. $hs.tips(false);
  2638. playerConf.filterReset();
  2639. $hs.tips('Video Filter Reset')
  2640. return TERMINATE
  2641. break;
  2642. // 按鍵S:畫面旋轉 90 度
  2643. case 'KeyS':
  2644. $hs.tips(false);
  2645. playerConf.rotate += 90
  2646. if (playerConf.rotate % 360 === 0) playerConf.rotate = 0;
  2647. if (!playerConf.videoHeight || !playerConf.videoWidth) {
  2648. playerConf.videoWidth = playerConf.domElement.videoWidth;
  2649. playerConf.videoHeight = playerConf.domElement.videoHeight;
  2650. }
  2651. if (playerConf.videoWidth > 0 && playerConf.videoHeight > 0) {
  2652.  
  2653.  
  2654. if ((playerConf.rotate % 180) == 90) {
  2655. playerConf.mFactor = playerConf.videoHeight / playerConf.videoWidth;
  2656. } else {
  2657. playerConf.mFactor = 1.0;
  2658. }
  2659.  
  2660.  
  2661. playerConf.cssTransform();
  2662.  
  2663. $hs.tips('Rotation:' + playerConf.rotate + ' deg')
  2664.  
  2665. }
  2666.  
  2667. return TERMINATE
  2668. break;
  2669. // 按鍵迴車,進入FULLSCREEN
  2670. case 'Enter':
  2671. //t.callFullScreenBtn();
  2672. break;
  2673. case 'KeyN':
  2674. $hs.pictureInPicture(player);
  2675. return TERMINATE
  2676. break;
  2677. case 'KeyM':
  2678. //console.log('m!', player.volume,player._volume)
  2679.  
  2680. if (player.volume >= 0) {
  2681.  
  2682. if (!player.volume || player.muted) {
  2683.  
  2684. let newVol = player.volume || player._volume || 0.5;
  2685. if (player.volume !== newVol) {
  2686. player.volume = newVol;
  2687. }
  2688. player.muted = false;
  2689. $hs.tips(false);
  2690. $hs.tips('Mute: Off', undefined);
  2691.  
  2692. } else {
  2693.  
  2694. player._volume = player.volume;
  2695. player._volume_p = player.volume;
  2696. //player.volume = 0;
  2697. player.muted = true;
  2698. $hs.tips(false);
  2699. $hs.tips('Mute: On', undefined);
  2700.  
  2701. }
  2702.  
  2703. }
  2704.  
  2705. return TERMINATE
  2706. break;
  2707. default:
  2708. // 按1-4設置播放速度 49-52;97-100
  2709. let numKey = +(event.key)
  2710.  
  2711. if (numKey >= 1 && numKey <= 4) {
  2712. $hs.tips(false);
  2713. $hs.setPlaybackRate(numKey, 1)
  2714. return TERMINATE
  2715. }
  2716. }
  2717.  
  2718. }
  2719. },
  2720.  
  2721. handlerPlayerLockedMouseMove: function(e) {
  2722. let player = $hs.player();
  2723. let rootNode = getRoot(player);
  2724.  
  2725. if (rootNode.pointerLockElement != player) {
  2726. player.removeEventListener('mousemove', $hs.handlerPlayerLockedMouseMove)
  2727. return;
  2728. }
  2729.  
  2730. let movementX = e.movementX || e.mozMovementX || e.webkitMovementX || 0,
  2731. movementY = e.movementY || e.mozMovementY || e.webkitMovementY || 0;
  2732.  
  2733. player.__xyOffset.x += movementX
  2734. player.__xyOffset.y += movementY
  2735. let ld = Math.sqrt(screen.width * screen.width + screen.height * screen.height) * .1
  2736. let md = Math.sqrt(player.__xyOffset.x * player.__xyOffset.x + player.__xyOffset.y * player.__xyOffset.y);
  2737. if (md > ld) $hs.playerActionLeave();
  2738.  
  2739. },
  2740.  
  2741. playerActionEnter: function() {
  2742. let player = $hs.player();
  2743.  
  2744. if (player) {
  2745. player.__requestPointerLock__();
  2746. player.__xyOffset = {
  2747. x: 0,
  2748. y: 0
  2749. };
  2750. player.addEventListener('mousemove', $hs.handlerPlayerLockedMouseMove)
  2751. }
  2752. },
  2753.  
  2754. playerActionLeave: function() {
  2755. let player = $hs.player();
  2756. if (player) player.removeEventListener('mousemove', $hs.handlerPlayerLockedMouseMove)
  2757. document.__exitPointerLock__();
  2758. },
  2759.  
  2760. /* 按鍵響應方法 */
  2761. handlerRootKeyDownEvent: function(event) {
  2762. if ($hs.intVideoInitCount > 0) {} else {
  2763. return;
  2764. }
  2765.  
  2766. // DOM Standard - either .key or .code
  2767. // Here we adopt .code (physical layout)
  2768.  
  2769. let pCode = event.code;
  2770. if (typeof pCode != 'string') return;
  2771. let player = $hs.player()
  2772. if (!player) return; // no video tag
  2773.  
  2774. let rootNode = getRoot(player);
  2775. let isRequiredListen = false;
  2776.  
  2777. let keyAsm = (event.shiftKey ? SHIFT : 0) | ((event.ctrlKey || event.metaKey) ? CTRL : 0) | (event.altKey ? ALT : 0);
  2778.  
  2779.  
  2780. if (document.fullscreenElement || rootNode.pointerLockElement) {
  2781. isRequiredListen = true;
  2782.  
  2783.  
  2784. if (!keyAsm && pCode == 'Escape') {
  2785. setTimeout(() => {
  2786. if (document.fullscreenElement) {
  2787. document.exitFullscreen();
  2788. } else if (document.pointerLockElement) {
  2789. $hs.playerActionLeave();
  2790. }
  2791. }, 700);
  2792. return;
  2793. }
  2794.  
  2795.  
  2796. }
  2797.  
  2798.  
  2799. const actionBoxRelation = $hs.getActionBoxRelationFromDOM(event.target)
  2800.  
  2801. if (actionBoxRelation) isRequiredListen = true;
  2802.  
  2803. //console.log('root key', event.target, actionBoxRelation, isRequiredListen)
  2804.  
  2805. if (!isRequiredListen) return;
  2806.  
  2807. //console.log('K01')
  2808.  
  2809. /* 切換插件的可用狀態 */
  2810. // Shift-`
  2811. if (keyAsm == SHIFT && pCode == 'Backquote') {
  2812. $hs.enable = !$hs.enable;
  2813. $hs.tips(false);
  2814. if ($hs.enable) {
  2815. $hs.tips('啟用h5Player插件')
  2816. } else {
  2817. $hs.tips('禁用h5Player插件')
  2818. }
  2819. // 阻止事件冒泡
  2820. event.stopPropagation()
  2821. event.preventDefault()
  2822. return false
  2823. }
  2824. if (!$hs.enable) {
  2825. consoleLog('h5Player 已禁用~')
  2826. return false
  2827. }
  2828.  
  2829. /* 非全局模式下,不聚焦則不執行快捷鍵的操作 */
  2830.  
  2831. if (!keyAsm && pCode == 'Enter') { //not NumberpadEnter
  2832. if (!rootNode.pointerLockElement && !document.fullscreenElement) {
  2833. if (rootNode.pointerLockElement != player) {
  2834. $hs.playerActionEnter();
  2835.  
  2836. // 阻止事件冒泡
  2837. event.stopPropagation()
  2838. event.preventDefault()
  2839. return false
  2840. }
  2841. } else if (rootNode.pointerLockElement && !document.fullscreenElement) {
  2842. $hs.playerActionLeave();
  2843.  
  2844. // 阻止事件冒泡
  2845. event.stopPropagation()
  2846. event.preventDefault()
  2847. return false
  2848. } else if (document.fullscreenElement) {
  2849. document.exitFullscreen();
  2850.  
  2851. // 阻止事件冒泡
  2852. event.stopPropagation()
  2853. event.preventDefault()
  2854. return false
  2855. }
  2856. }
  2857.  
  2858. let hv = (elm) => (elm && (elm == player || elm.contains(player)) ? elm : null);
  2859.  
  2860. let _checkingPass;
  2861.  
  2862. let plm = null;
  2863. if (rootNode.activeElement && !rootNode.pointerLockElement && !document.fullscreenElement) {
  2864. // the active element may or may not contains the player
  2865. // but the player box (player->parent->parent->...) shall contains the active element (and the player)
  2866. // so if the active element is inside the layoutbox, okay!
  2867. // ps. layoutbox may be much larger than the activeelement, then it is not overlapping case.
  2868.  
  2869. plm = rootNode.activeElement
  2870.  
  2871. //console.log('activeElement', plm)
  2872. _checkingPass = true
  2873. } else {
  2874. plm = hv(rootNode.pointerLockElement) || hv(document.fullscreenElement)
  2875. _checkingPass = !!plm
  2876. }
  2877.  
  2878.  
  2879. if (_checkingPass) {
  2880.  
  2881.  
  2882.  
  2883.  
  2884. let res = $hs.playerTrigger(player, event)
  2885. if (res == TERMINATE) {
  2886. event.stopPropagation()
  2887. event.preventDefault()
  2888. return false
  2889. }
  2890.  
  2891. }
  2892. },
  2893. /* 設置播放進度 */
  2894. setPlayProgress: function(player, curTime) {
  2895. if (!player) return
  2896. if (!curTime || Number.isNaN(curTime)) return
  2897. player.currentTime = curTime
  2898. if (curTime > 3) {
  2899. $hs.tips(false);
  2900. $hs.tips(`Playback Jumps to ${$hs.toolFormatCT(curTime)}`)
  2901. if (player.paused) player.play();
  2902. }
  2903. }
  2904. }
  2905.  
  2906. function makeFilter(arr, k) {
  2907. let res = ""
  2908. for (const e of arr) {
  2909. for (const d of e) {
  2910. res += " " + (1.0 * d * k).toFixed(9)
  2911. }
  2912. }
  2913. return res.trim()
  2914. }
  2915.  
  2916. function _add_filter(rootElm) {
  2917. let rootView = null;
  2918. if (rootElm && rootElm.nodeType > 0) {
  2919. while (rootElm.parentNode && rootElm.parentNode.nodeType === 1) rootElm = rootElm.parentNode;
  2920. rootView = rootElm.querySelector('body') || rootElm;
  2921. } else {
  2922. return;
  2923. }
  2924.  
  2925. if (rootView && rootView.querySelector && !rootView.querySelector('#_h5player_section_')) {
  2926.  
  2927. let svgFilterElm = document.createElement('section')
  2928. svgFilterElm.style.position = 'fixed';
  2929. svgFilterElm.style.left = '-999px';
  2930. svgFilterElm.style.width = '1px';
  2931. svgFilterElm.style.top = '-999px';
  2932. svgFilterElm.style.height = '1px';
  2933. svgFilterElm.id = '_h5player_section_'
  2934. let svgXML = `
  2935. <svg id='_h5p_image' version="1.1" xmlns="http://www.w3.org/2000/svg">
  2936. <defs>
  2937. <filter id="_h5p_sharpen1">
  2938. <feConvolveMatrix filterRes="100 100" style="color-interpolation-filters:sRGB" order="3" kernelMatrix="` + `
  2939. -0.3 -0.3 -0.3
  2940. -0.3 3.4 -0.3
  2941. -0.3 -0.3 -0.3`.replace(/[\n\r]+/g, ' ').trim() + `" preserveAlpha="true"/>
  2942. </filter>
  2943. <filter id="_h5p_unsharpen1">
  2944. <feConvolveMatrix style="color-interpolation-filters:sRGB;color-interpolation: sRGB;" order="5" kernelMatrix="` +
  2945. makeFilter([
  2946. [1, 4, 6, 4, 1],
  2947. [4, 16, 24, 16, 4],
  2948. [6, 24, -476, 24, 6],
  2949. [4, 16, 24, 16, 4],
  2950. [1, 4, 6, 4, 1]
  2951. ], -1 / 256) + `" preserveAlpha="false"/>
  2952. </filter>
  2953. <filter id="_h5p_unsharpen3_05">
  2954. <feConvolveMatrix style="color-interpolation-filters:sRGB;color-interpolation: sRGB;" order="3" kernelMatrix="` +
  2955. makeFilter(
  2956. [
  2957. [0.025, 0.05, 0.025],
  2958. [0.05, -1.1, 0.05],
  2959. [0.025, 0.05, 0.025]
  2960. ], -1 / .8) + `" preserveAlpha="false"/>
  2961. </filter>
  2962. <filter id="_h5p_unsharpen3_10">
  2963. <feConvolveMatrix style="color-interpolation-filters:sRGB;color-interpolation: sRGB;" order="3" kernelMatrix="` +
  2964. makeFilter(
  2965. [
  2966. [0.05, 0.1, 0.05],
  2967. [0.1, -1.4, 0.1],
  2968. [0.05, 0.1, 0.05]
  2969. ], -1 / .8) + `" preserveAlpha="false"/>
  2970. </filter>
  2971. <filter id="_h5p_unsharpen5_05">
  2972. <feConvolveMatrix style="color-interpolation-filters:sRGB;color-interpolation: sRGB;" order="5" kernelMatrix="` +
  2973. makeFilter(
  2974. [
  2975. [0.025, 0.1, 0.15, 0.1, 0.025],
  2976. [0.1, 0.4, 0.6, 0.4, 0.1],
  2977. [0.15, 0.6, -18.3, 0.6, 0.15],
  2978. [0.1, 0.4, 0.6, 0.4, 0.1],
  2979. [0.025, 0.1, 0.15, 0.1, 0.025]
  2980. ], -1 / 12.8) + `" preserveAlpha="false"/>
  2981. </filter>
  2982. <filter id="_h5p_unsharpen5_10">
  2983. <feConvolveMatrix style="color-interpolation-filters:sRGB;color-interpolation: sRGB;" order="5" kernelMatrix="` +
  2984. makeFilter(
  2985. [
  2986. [0.05, 0.2, 0.3, 0.2, 0.05],
  2987. [0.2, 0.8, 1.2, 0.8, 0.2],
  2988. [0.3, 1.2, -23.8, 1.2, 0.3],
  2989. [0.2, 0.8, 1.2, 0.8, 0.2],
  2990. [0.05, 0.2, 0.3, 0.2, 0.05]
  2991. ], -1 / 12.8) + `" preserveAlpha="false"/>
  2992. </filter>
  2993. <filter id="_h5p_unsharpen9_05">
  2994. <feConvolveMatrix style="color-interpolation-filters:sRGB;color-interpolation: sRGB;" order="9" kernelMatrix="` +
  2995. makeFilter(
  2996. [
  2997. [0.025, 0.2, 0.7, 1.4, 1.75, 1.4, 0.7, 0.2, 0.025],
  2998. [0.2, 1.6, 5.6, 11.2, 14, 11.2, 5.6, 1.6, 0.2],
  2999. [0.7, 5.6, 19.6, 39.2, 49, 39.2, 19.6, 5.6, 0.7],
  3000. [1.4, 11.2, 39.2, 78.4, 98, 78.4, 39.2, 11.2, 1.4],
  3001. [1.75, 14, 49, 98, -4792.7, 98, 49, 14, 1.75],
  3002. [1.4, 11.2, 39.2, 78.4, 98, 78.4, 39.2, 11.2, 1.4],
  3003. [0.7, 5.6, 19.6, 39.2, 49, 39.2, 19.6, 5.6, 0.7],
  3004. [0.2, 1.6, 5.6, 11.2, 14, 11.2, 5.6, 1.6, 0.2],
  3005. [0.025, 0.2, 0.7, 1.4, 1.75, 1.4, 0.7, 0.2, 0.025]
  3006. ], -1 / 3276.8) + `" preserveAlpha="false"/>
  3007. </filter>
  3008. <filter id="_h5p_unsharpen9_10">
  3009. <feConvolveMatrix style="color-interpolation-filters:sRGB;color-interpolation: sRGB;" order="9" kernelMatrix="` +
  3010. makeFilter(
  3011. [
  3012. [0.05, 0.4, 1.4, 2.8, 3.5, 2.8, 1.4, 0.4, 0.05],
  3013. [0.4, 3.2, 11.2, 22.4, 28, 22.4, 11.2, 3.2, 0.4],
  3014. [1.4, 11.2, 39.2, 78.4, 98, 78.4, 39.2, 11.2, 1.4],
  3015. [2.8, 22.4, 78.4, 156.8, 196, 156.8, 78.4, 22.4, 2.8],
  3016. [3.5, 28, 98, 196, -6308.6, 196, 98, 28, 3.5],
  3017. [2.8, 22.4, 78.4, 156.8, 196, 156.8, 78.4, 22.4, 2.8],
  3018. [1.4, 11.2, 39.2, 78.4, 98, 78.4, 39.2, 11.2, 1.4],
  3019. [0.4, 3.2, 11.2, 22.4, 28, 22.4, 11.2, 3.2, 0.4],
  3020. [0.05, 0.4, 1.4, 2.8, 3.5, 2.8, 1.4, 0.4, 0.05]
  3021. ], -1 / 3276.8) + `" preserveAlpha="false"/>
  3022. </filter>
  3023. <filter id="_h5p_grey1">
  3024. <feColorMatrix values="0.3333 0.3333 0.3333 0 0
  3025. 0.3333 0.3333 0.3333 0 0
  3026. 0.3333 0.3333 0.3333 0 0
  3027. 0 0 0 1 0"/>
  3028. <feColorMatrix type="saturate" values="0" />
  3029. </filter>
  3030. </defs>
  3031. </svg>
  3032. `;
  3033.  
  3034. svgFilterElm.innerHTML = svgXML.replace(/[\r\n\s]+/g, ' ').trim();
  3035.  
  3036. rootView.appendChild(svgFilterElm);
  3037. }
  3038.  
  3039. }
  3040.  
  3041. /**
  3042. * 某些網頁用了attachShadow closed mode,需要open才能獲取video標籤,例如百度雲盤
  3043. * 解決參考:
  3044. * https://developers.google.com/web/fundamentals/web-components/shadowdom?hl=zh-cn#closed
  3045. * https://stackoverflow.com/questions/54954383/override-element-prototype-attachshadow-using-chrome-extension
  3046. */
  3047.  
  3048. const initForShadowRoot = async (shadowRoot) => {
  3049. try {
  3050. if (shadowRoot && shadowRoot.nodeType > 0 && 'querySelectorAll' in shadowRoot) {
  3051. $hs.bindDocEvents(shadowRoot);
  3052.  
  3053. captureVideoEvents(shadowRoot);
  3054. shadowRoots.push(shadowRoot)
  3055. }
  3056. } catch (e) {
  3057. console.log('h5Player: initForShadowRoot failed')
  3058. }
  3059. }
  3060.  
  3061. function hackAttachShadow() { // attachShadow - DOM Standard
  3062.  
  3063. let _prototype_ = window && window.HTMLElement ? window.HTMLElement.prototype : null;
  3064. if (_prototype_ && typeof _prototype_.attachShadow == 'function') {
  3065.  
  3066. let _attachShadow = _prototype_.attachShadow
  3067.  
  3068. hackAttachShadow = null
  3069. _prototype_.attachShadow = function() {
  3070. let arg = [...arguments];
  3071. if (arg[0] && arg[0].mode) arg[0].mode = 'open';
  3072. let shadowRoot = _attachShadow.apply(this, arg);
  3073. initForShadowRoot(shadowRoot);
  3074. return shadowRoot
  3075. };
  3076.  
  3077. _prototype_.attachShadow.toString = () => _attachShadow.toString();
  3078.  
  3079. }
  3080.  
  3081. }
  3082.  
  3083. function hackCreateShadowRoot() { // createShadowRoot - Deprecated
  3084.  
  3085. let _prototype_ = window && window.HTMLElement ? window.HTMLElement.prototype : null;
  3086. if (_prototype_ && typeof _prototype_.createShadowRoot == 'function') {
  3087.  
  3088. let _createShadowRoot = _prototype_.createShadowRoot;
  3089.  
  3090. hackCreateShadowRoot = null
  3091. _prototype_.createShadowRoot = function() {
  3092. const shadowRoot = _createShadowRoot.apply(this, arguments);
  3093. initForShadowRoot(shadowRoot);
  3094. return shadowRoot;
  3095. };
  3096. _prototype_.createShadowRoot.toString = () => _createShadowRoot.toString();
  3097.  
  3098. }
  3099. }
  3100.  
  3101. /* 事件偵聽hack */
  3102. function hackEventListener() {
  3103. if (!window.EventTarget) return;
  3104. const eventTargetPrototype = window.EventTarget.prototype;
  3105. let _addEventListener = eventTargetPrototype.addEventListener;
  3106. let _removeEventListener = eventTargetPrototype.removeEventListener;
  3107. if (typeof _addEventListener == 'function' && typeof _removeEventListener == 'function') {} else return;
  3108. hackEventListener = null;
  3109.  
  3110. class Listeners {
  3111.  
  3112. constructor(dom, type) {
  3113. this._dom = dom;
  3114. this._type = type;
  3115. this.listenersCount = 0;
  3116. this.hashList = {};
  3117. }
  3118. get baseFunc() {
  3119. if (this._dom && this._type) {
  3120. return this._dom['on' + this._type];
  3121. }
  3122. }
  3123. get funcCount() {
  3124. if (this._dom && this._type) {
  3125. return (typeof this.baseFunc == 'function') * 1 + (this.listenersCount || 0)
  3126. }
  3127. }
  3128.  
  3129.  
  3130. }
  3131.  
  3132.  
  3133.  
  3134. let hackedEvtCount = 0;
  3135.  
  3136.  
  3137. let watchList = ['click'];
  3138.  
  3139. eventTargetPrototype.addEventListener = function() {
  3140.  
  3141. let arg = arguments
  3142. let type = arg[0]
  3143. let listener = arg[1]
  3144.  
  3145. if (!this || !(this instanceof EventTarget) || typeof type != 'string' || typeof listener != 'function') {
  3146. return _addEventListener.apply(this, arguments)
  3147. //unknown bug?
  3148. }
  3149.  
  3150. if (watchList.indexOf(type) < 0) return _addEventListener.apply(this, arguments);
  3151.  
  3152.  
  3153. let res;
  3154. res = _addEventListener.apply(this, arg)
  3155.  
  3156.  
  3157. let boolCapture = (arg[2] && typeof arg[2] == 'object') ? (arg[2].capture === true) : (arg[2] === true)
  3158.  
  3159. this._listeners = this._listeners || {}
  3160. this._listeners[type] = this._listeners[type] || new Listeners(this, type)
  3161. let uid = 100000 + (++hackedEvtCount);
  3162. let listenerObj = {
  3163. listener,
  3164. options: arg[2],
  3165. uid: uid,
  3166. useCapture: boolCapture
  3167. }
  3168. this._listeners[type].hashList[uid + ''] = listenerObj;
  3169. this._listeners[type].listenersCount++;
  3170. return res;
  3171. }
  3172. // hack removeEventListener
  3173. eventTargetPrototype.removeEventListener = function() {
  3174.  
  3175. let arg = arguments
  3176. let type = arg[0]
  3177. let listener = arg[1]
  3178.  
  3179. if (!this || !(this instanceof EventTarget) || typeof type != 'string' || typeof listener != 'function') {
  3180. return _removeEventListener.apply(this, arguments)
  3181. //unknown bug?
  3182. }
  3183.  
  3184. if (watchList.indexOf(type) < 0) return _removeEventListener.apply(this, arguments);
  3185.  
  3186. let boolCapture = (arg[2] && typeof arg[2] == 'object') ? (arg[2].capture === true) : (arg[2] === true)
  3187.  
  3188. let defaultRemoval = true;
  3189. if (this._listeners && this._listeners[type] && this._listeners[type].hashList) {
  3190. let hashList = this._listeners[type].hashList
  3191. for (let k in hashList) {
  3192. if (hashList[k].listener === listener && hashList[k].useCapture == boolCapture) {
  3193. delete hashList[k];
  3194. this._listeners[type].listenersCount--;
  3195. break;
  3196. }
  3197. }
  3198. }
  3199. if (defaultRemoval) return _removeEventListener.apply(this, arg);
  3200. }
  3201. eventTargetPrototype.addEventListener.toString = () => _addEventListener.toString();
  3202. eventTargetPrototype.removeEventListener.toString = () => _removeEventListener.toString();
  3203.  
  3204.  
  3205. }
  3206.  
  3207. function captureVideoEvents(rootDoc) {
  3208.  
  3209. var g = function(evt) {
  3210.  
  3211. var domElement = evt.target || this || null
  3212. if (domElement && domElement.nodeType == 1 && domElement.nodeName == "VIDEO") {
  3213. var video = domElement
  3214. if (!domElement.getAttribute('_h5ppid')) handlerVideoFound(video);
  3215. if (domElement.getAttribute('_h5ppid')) {
  3216. switch (evt.type) {
  3217. case 'loadedmetadata':
  3218. return $hs.handlerVideoLoadedMetaData.call(video, evt);
  3219. // case 'playing':
  3220. // return $hs.handlerVideoPlaying.call(video, evt);
  3221. // case 'pause':
  3222. // return $hs.handlerVideoPause.call(video, evt);
  3223. // case 'volumechange':
  3224. // return $hs.handlerVideoVolumeChange.call(video, evt);
  3225. }
  3226. }
  3227. }
  3228.  
  3229.  
  3230. }
  3231.  
  3232. // using capture phase
  3233. rootDoc.addEventListener('loadedmetadata', g, true);
  3234.  
  3235. }
  3236.  
  3237. function handlerVideoFound(video) {
  3238.  
  3239. if (!video) return;
  3240. if (video.getAttribute('_h5ppid')) return;
  3241. let alabel = video.getAttribute('aria-label')
  3242. if (alabel && typeof alabel == "string" && alabel.toUpperCase() == "GIF") return;
  3243.  
  3244.  
  3245. consoleLog('handlerVideoFound', video)
  3246.  
  3247. $hs.intVideoInitCount = ($hs.intVideoInitCount || 0) + 1;
  3248. let vpid = 'h5p-' + $hs.intVideoInitCount
  3249. consoleLog(' - HTML5 Video is detected -', `Number of Videos: ${$hs.intVideoInitCount}`)
  3250. if ($hs.intVideoInitCount === 1) $hs.fireGlobalInit();
  3251. video.setAttribute('_h5ppid', vpid)
  3252.  
  3253.  
  3254. playerConfs[vpid] = new PlayerConf();
  3255. playerConfs[vpid].domElement = video;
  3256. playerConfs[vpid].domActive = DOM_ACTIVE_FOUND;
  3257.  
  3258. let rootNode = getRoot(video);
  3259.  
  3260. if (rootNode.host) $hs.getPlayerBlockElement(video); // shadowing
  3261. let rootElm = rootNode.querySelector('head') || rootNode.querySelector('html') || document.documentElement //48763
  3262. _add_filter(rootElm) // either main document or shadow node
  3263.  
  3264.  
  3265.  
  3266. video.addEventListener('playing', $hs.handlerVideoPlaying, true);
  3267. video.addEventListener('pause', $hs.handlerVideoPause, true);
  3268. video.addEventListener('volumechange', $hs.handlerVideoVolumeChange, true);
  3269.  
  3270.  
  3271.  
  3272. }
  3273.  
  3274.  
  3275.  
  3276. hackAttachShadow()
  3277. hackCreateShadowRoot()
  3278. hackEventListener()
  3279.  
  3280.  
  3281. window.addEventListener('message', $hs.handlerWinMessage, false);
  3282. $hs.bindDocEvents(document);
  3283. captureVideoEvents(document);
  3284.  
  3285. let windowsLD = (function() {
  3286. let ls_res = [];
  3287. try {
  3288. ls_res = [!!window.localStorage, !!window.top.localStorage];
  3289. } catch (e) {}
  3290. try {
  3291. let winp = window;
  3292. let winc = 0;
  3293. while (winp !== window.top && winp && ++winc) winp = winp.parentNode;
  3294. ls_res.push(winc);
  3295. } catch (e) {}
  3296. return ls_res;
  3297. })();
  3298.  
  3299. consoleLogF('- h5Player Plugin Loaded -', ...windowsLD)
  3300.  
  3301. function isInCrossOriginFrame() {
  3302. let result = true;
  3303. try {
  3304. if (window.top.localStorage || window.top.location.href) result = false;
  3305. } catch (e) {}
  3306. return result
  3307. }
  3308.  
  3309. if (isInCrossOriginFrame()) consoleLog('cross origin frame detected');
  3310.  
  3311.  
  3312. })();

QingJ © 2025

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