Twitch - Redirect Live Videos to Popout Player Version

Redirect Twitch broadcasts to the minimal popout player version

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Twitch - Redirect Live Videos to Popout Player Version
// @namespace    TwitchLiveVidPopoutPlayerRedirect
// @description  Redirect Twitch broadcasts to the minimal popout player version
// @version      1.3
// @author       Kai Krause <[email protected]>
// @match        http://*.twitch.tv/*
// @match        https://*.twitch.tv/*
// @run-at       document-end
// ==/UserScript==

setInterval(() => {
	// check whether the page has text reading that the channel is "live"
	let pageUrl = window.location.href;
	if (pageUrl.match(/\.tv\/[A-Za-z0-9]/gmi) === null) return;

	let isLive = false;
	let p = document.getElementsByTagName("p");
	for (let i = 0; i < p.length; i++) {
		if (p[i].textContent.toLowerCase() === "live") {
			isLive = true;
			break;
		}
	}
	if (!isLive) return;

	// check whether the video player's controls are visible
	let isPlayerVisible = false;
	let divs = document.getElementsByTagName("div");
	for (let i = 0; i < divs.length; i++) {
		if (divs[i].getAttribute("data-a-target") === "player-controls") {
			isPlayerVisible = true;
			break;
		}
	}
	if (!isPlayerVisible) return;

	let videoPath = window.location.pathname;
	if (videoPath.endsWith("/")) videoPath = videoPath.slice(0, -1);

	// only continue if channel name, and not twitch.tv/channelName/otherPage
	let backslashCount = videoPath.match(/[/]/gm);
	if (backslashCount.length > 1) return

	// redirect
	let popoutUrl = "https://player.twitch.tv/?parent=twitch.tv&player=popout&channel=" + videoPath.substr(1);
	window.location.replace(popoutUrl);
}, 3000);