LIVE in Desktop Palyer

using desktop player watching Bilibili and Youtube live

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         LIVE in Desktop Palyer
// @namespace    http://tampermonkey.net/LIVEinDesktopPlayer
// @version      0.3.1
// @description  using desktop player watching Bilibili and Youtube live
// @author       luoyayu
// @match        https://live.bilibili.com/*
// @match        https://www.youtube.com/watch*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

// THE CODE USE `Potplayer` FOR EXAMPLE

// WINDOWS USER NOTICE
/* paste below code into a file named `potplayer.reg`, then click and run it

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\potplayer\shell\open\command]
@="cmd /k ( set \"var=%1\" & call set var=%%var:potplayer://=%% & call C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe %%var%%)"
 */

// Unix USER NOTICE
/*
 * Create a custom URL Protocol Handler for desktop player
 */

(function() {
  'use strict';
  const host = window.location.host;
  const int = window.setInterval(isPaused, 1000);
  const end_int = window.setTimeout(()=>{
      window.clearInterval(int);
  }, 1000*5);

  function jump2player(video_url) {
    const jump_link = document.createElement('a');
    jump_link.setAttribute('href', 'potplayer://' + video_url); // !! fill your custom url scheme !!
    jump_link.click();
  }

  function isPaused() {
    let d;

    if (host === 'www.youtube.com') {
      d = document.getElementsByClassName('ytp-play-button ytp-button')[0]; // btn
    } else if (host === 'live.bilibili.com') {
      d = document.getElementsByClassName('bilibili-live-player relative')[0]; // player
    }

    if (d !== undefined) {
      let state = undefined;

      if (host === 'www.youtube.com') {
        state = document.getElementsByClassName(
            'ytp-play-button ytp-button')[0].getElementsByClassName(
            'ytp-svg-fill')[0].getAttribute('d') ===
            'M 12,25 19,25 19,11 12,11 z M 19,25 26,25 26,11 19,11 z';
      } else if (host === 'live.bilibili.com') {
        state = d.getAttribute('data-video-state') === 'playing';
      }

      if (state === false) { // paused
        // window.clearInterval(int);
      } else {
        if (host === 'www.youtube.com') {
          d.click();
        } else if (host === 'live.bilibili.com') {
          document.getElementsByClassName('blpui-btn icon-btn')[0].click(); // btn
        }
      }
    }
  }

  if (host === 'www.youtube.com') {
    const is_live = document.getElementsByClassName(
        'ytp-live-badge')[0].getAttribute('disabled').length === 0;

    if (is_live) {
      jump2player(window.location.href);
    }
  } else {
    const cid = window.location.pathname.substr(1);

    if (!isNaN(cid)) {
      GM_xmlhttpRequest({
        method: 'GET',
        url: 'https://api.live.bilibili.com/room/v1/Room/playUrl?platform=h5&cid=' +
            cid,
        onload: xhr => {
          let ret = xhr.response;
          jump2player(JSON.parse(ret).data.durl[0].url);
        },
      });
    }
  }

})();