Streamscharts Direct Links

Extract direct video links from streamscharts

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Streamscharts Direct Links
// @namespace    https://streamscharts.com/
// @version      1.0.0
// @description  Extract direct video links from streamscharts
// @author       You
// @match        https://streamscharts.com/top-live-channels*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=streamscharts.com
// @grant        none
// @license      0BSD
// ==/UserScript==

(function () {
  'use strict';

  let trs = document.querySelectorAll("#table-screen table > tbody tr");
  for (const tr of trs) {
    let aChannel = tr.querySelector("td:nth-child(3) a");
    let aLive = tr.querySelector("td:nth-child(8) a");
    let btnEmbed = tr.querySelector("td:nth-child(7) > button[x-on\\:click]");

    let rawChannelHref = aChannel.href;
    let channelHref = new URL(rawChannelHref);
    let channelPlatform = channelHref.searchParams.get('platform') || 'twitch';
    let channelID = channelHref.pathname.split('/').at(2);

    // console.log({channelPlatform, channelID}); // TODO: log

    let attr = btnEmbed?.getAttribute('x-on:click');
    let match = attr?.match(/'(http[^']+)'/);
    let embedUrl = match ? match[1] : null;

    // console.log({embedUrl}); // TODO: log

    if (!channelID) {
      continue;
    }

    aChannel.target="_blank";
    aLive.target="_blank";

    if (channelPlatform === "chzzk") {
      aChannel.href = `https://chzzk.naver.com/${channelID}`;
      aLive.href = `https://chzzk.naver.com/live/${channelID}`;
    } else if (channelPlatform === "youtube") {
      aChannel.href = `https://www.youtube.com/channel/${channelID}`;
      let _embedUrl = new URL(embedUrl);
      let liveID = _embedUrl.pathname.split('/').at(2);
      aLive.href = liveID
        ? `https://www.youtube.com/watch?v=${liveID}`
        : embedUrl;
    } else if (channelPlatform === "twitch") {
      aChannel.href = `https://twitch.tv/${channelID}`;
      aLive.href = aChannel.href;
    } else if (channelPlatform === "kick") {
      aChannel.href = `https://kick.com/${channelID}`;
      aLive.href = aChannel.href;
    } else if (channelPlatform === "afreecatv") {
      aChannel.href = `https://www.sooplive.co.kr/station/${channelID}`;
      let livePath = new URL(embedUrl).pathname.split('/').slice(1, 3).join('/');
      aLive.href = `https://play.sooplive.co.kr/${livePath}`;
    } else if (channelPlatform === "soop") {
      aChannel.href = `https://www.sooplive.com/${channelID}`;
      aLive.href = aChannel.href;
    } else if (channelPlatform === "nimo") {
      aChannel.href = `https://www.nimo.tv/user/${channelID}`;
      aLive.href = `https://www.nimo.tv/${channelID}`;
    } else if (channelPlatform === "trovo") {
      aChannel.href = `https://trovo.live/s/${channelID}`;
      aLive.href = aChannel.href;
    } else if (channelPlatform === "steam") {

      // TODO: steam uses numeric ID instead of text username, it needs API call to extract it
      // aChannel.href = `https://steamcommunity.com/id/${channelID}`;
      // aLive.href = `https://steamcommunity.com/broadcast/watch/${channelID}`;

      aChannel.href = `https://steamcommunity.com/?subsection=broadcasts`;
      aLive.href = `https://steamcommunity.com/?subsection=broadcasts`;
    } else if (channelPlatform === "bigo") {
      aChannel.href = `https://www.bigo.tv/user/${channelID}`;
      aLive.href = `https://www.bigo.tv/${channelID}`;
    }
  }
})();