Youtube Interface Modification

將聊天室與直播位置切換

目前為 2023-12-12 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Youtube Interface Modification
// @namespace    https://github.com/RutsuLun
// @version      1.3
// @description  將聊天室與直播位置切換
// @author       Rutsu Lun
// @match        https://www.youtube.com/watch?*
// @icon         https://www.google.com/s2/favicons?domain=youtube.com
// @license      Only Share
// @grant        GM.registerMenuCommand
// ==/UserScript==

(function () {
    GM.registerMenuCommand('呼叫', Lun_createBtnList);
    GM.registerMenuCommand('介面', Lun_loayoutSwitch);
    GM.registerMenuCommand('emoji調整', Lun_removeEmojiTag);
    GM.registerMenuCommand('移除愛心', hideShitHard);
    // GM.registerMenuCommand('表符', Lun_emojiMenuChenge);
})();

const btnListSetting = [
    { id: 'Lun_loayoutSwitch', name: '介面', method: Lun_loayoutSwitch, },
    { id: 'Lun_emojiMenuChenge', name: '表符', method: Lun_removeEmojiTag, },
    { id: 'Lun_emojiMenuChenge', name: '移除愛心', method: hideShitHard, },
]
const btnListCss = 'position: absolute;top: 0;left: 0;'

function Lun_loayoutSwitch() {
    document.getElementById('columns').style.cssText == '' ? document.getElementById('columns').style.cssText = 'flex-direction: row-reverse;' : document.getElementById('columns').style.cssText = '';
    document.querySelector('ytd-player').style.cssText += 'border-radius: 0;'
}

function Lun_removeEmojiTag() {
    const iframe = document.getElementById('chatframe');
    if (iframe.contentDocument) {
        var iframeDocument = iframe.contentDocument;
        var category = iframeDocument.getElementById('category-buttons');
        var search = iframeDocument.getElementById('search-panel');
        var emoji = iframeDocument.querySelector('yt-emoji-picker-renderer');
        category.style.cssText += 'display:none;'
        search.style.cssText += 'display:none;'
        emoji.style.cssText += 'margin: -5px -24px !important'
    }
}

function Lun_emojiMenuChenge() {
    const iframe = document.getElementById('chatframe');
    if (iframe.contentDocument) {
        var iframeDocument = iframe.contentDocument;
        var targetElement = iframeDocument.querySelector('yt-emoji-picker-renderer[floating-emoji-picker]');
        targetElement.style.cssText = 'min-height: 400px';
    }
}

function hideShitHard() {
    const iframe = document.getElementById('chatframe');
    if (iframe.contentDocument) {
        var iframeDocument = iframe.contentDocument;
        var targetElement = iframeDocument.querySelector('#reaction-control-panel');
        targetElement.style.cssText = 'display:none;'
    }
}

function Lun_createBtnList() {
    const chat = document.getElementById('secondary');
    if (chat && document.querySelector('Lun_btnList') == null) {
        const btnList = document.createElement('span');
        btnList.id = 'Lun_btnList';
        btnList.style = btnListCss;
        chat.append(btnList);
        btnListSetting.forEach(b => {
            let btn = document.createElement('button');
            btn.id = b.id;
            btn.innerText = b.name;
            btnList.append(btn);
            btn.addEventListener('click', b.method);
        });
    }
}

const main = function () {
    // Lun_emojiMenuChenge();
    console.log('載入完畢');
    Lun_createBtnList();
}

const injectScript = function (frameWindow) {
    main()
}

const retrieveChatFrameWindow = function () {
    if (window.location.pathname === "/live_chat" || window.location.pathname === "/live_chat_replay") return window;
    for (let i = 0; i < window.frames.length; i++) {
        try {
            if (window.frames[i].location) {
                let pathname = window.frames[i].location.pathname;
                if (pathname === "/live_chat" || pathname === "/live_chat_replay") return frames[i];
            }
        } catch (ex) { }
    }
}

const tryBrowserIndependentExecution = function () {
    let destinationFrameWindow = retrieveChatFrameWindow();
    if (!destinationFrameWindow || !destinationFrameWindow.document || destinationFrameWindow.document.readyState != "complete") {
        setTimeout(tryBrowserIndependentExecution, 1000);
        return;
    }
    if (destinationFrameWindow.channelResolverInitialized) return;
    injectScript(destinationFrameWindow);
    destinationFrameWindow.channelResolverInitialized = true;
}

if (ytInitialPlayerResponse.videoDetails.isLiveContent) {
    console.log('直播')
    tryBrowserIndependentExecution();
} else {
    console.log('正常影片')
}