Make BiliBili Grate Again

useful tweaks for bilibili.com

目前為 2022-04-13 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Make BiliBili Grate Again
// @namespace    https://www.kookxiang.com/
// @version      1.3.2
// @description  useful tweaks for bilibili.com
// @author       kookxiang
// @match        https://*.bilibili.com/*
// @grant        unsafeWindow
// ==/UserScript==

let customStyle = ``

// 动态页面优化
if (location.host === "t.bilibili.com") {
    customStyle += `
body[wide] #app {
    display: flex;
}

body[wide] .bili-dyn-home--member {
    box-sizing: border-box;
    padding: 0 10px;
    width: 100%;
    flex: 1;
}

body[wide] main {
    margin: 0 8px;
    flex: 1;
    overflow: auto;
    width: initial;
}

#wide-mode-switch {
    margin-left: 0;
    margin-right: 20px;
}
`
    if (!localStorage.WIDE_OPT_OUT) {
        document.body.setAttribute('wide', 'wide')
    }
    window.addEventListener('load', function(){
        const tabContainer = document.querySelector('.bili-dyn-list-tabs__list')
        const placeHolder = document.createElement('div')
        placeHolder.style.flex = 1
        const switchButton = document.createElement('a')
        switchButton.id = 'wide-mode-switch'
        switchButton.className = 'bili-dyn-list-tabs__item'
        switchButton.textContent = '宽屏模式'
        switchButton.addEventListener('click', function(e) {
            e.preventDefault()
            if (localStorage.WIDE_OPT_OUT) {
                localStorage.removeItem('WIDE_OPT_OUT')
                document.body.setAttribute('wide', 'wide')
            } else {
                localStorage.setItem('WIDE_OPT_OUT', '1')
                document.body.removeAttribute('wide')
            }
        })
        tabContainer.appendChild(placeHolder)
        tabContainer.appendChild(switchButton)
    })
}

// 去广告
if (unsafeWindow.__INITIAL_STATE__ && unsafeWindow.__INITIAL_STATE__.locsData) {
    for (const key in unsafeWindow.__INITIAL_STATE__.locsData) {
        if (!Array.isArray(unsafeWindow.__INITIAL_STATE__.locsData[key])) {
            continue;
        }
        unsafeWindow.__INITIAL_STATE__.locsData[key] = unsafeWindow.__INITIAL_STATE__.locsData[key].filter(x => !x.is_ad)
    }
}

// 修复文章区复制
if (location.href.startsWith('https://www.bilibili.com/read/cv')) {
    unsafeWindow.original.reprint = "1"
    document.querySelector('.article-holder').classList.remove("unable-reprint")
    document.querySelector('.article-holder').addEventListener('copy', e => e.stopImmediatePropagation(), true)
}

// 去 P2P CDN
if (location.href.startsWith('https://www.bilibili.com/video/') || location.href.startsWith('https://www.bilibili.com/bangumi/play/')) {
    let cdnDomain

    try {
        [ cdnDomain ] = document.head.innerHTML.match(/up[\w-]+\.bilivideo\.com/)
    } catch(e) {}

    (function(open) {
        unsafeWindow.XMLHttpRequest.prototype.open = function() {
            try {
                const urlObj = new URL(arguments[1]);
                if (urlObj.hostname.endsWith(".mcdn.bilivideo.cn")) {
                    urlObj.host = cdnDomain || 'upos-sz-mirrorcoso1.bilivideo.com'
                    urlObj.port = 443
                    console.warn(`更换视频源: ${urlObj.host}`);
                    arguments[1] = urlObj.toString()
                } else if (urlObj.hostname.endsWith(".szbdyd.com")) {
                    urlObj.host = urlObj.searchParams.get('xy_usource');
                    urlObj.port = 443;
                    console.warn(`更换视频源: ${urlObj.host}`);
                    arguments[1] = urlObj.toString();
                }
            } finally {
                return open.apply(this, arguments)
            }
        };
    })(unsafeWindow.XMLHttpRequest.prototype.open);

}

// 视频裁切
if (location.href.startsWith('https://www.bilibili.com/video/')) {
    customStyle += `
body[video-fit] .bilibili-player-video video,
body[video-fit] .bilibili-player-video bwp-video {
    object-fit: cover;
}

.bilibili-player-video-btn-setting-left-fit-mode {
    display: flex;
    width: 100%;
    height: 32px;
    line-height: 32px;
}

.bilibili-player-video-btn-setting-box.bui-panel .bui-panel-wrap,
.bilibili-player-video-btn-setting-box.bui-panel .bui-panel-item {
    min-height: 172px !important;
}
`
    let timer;
    function toggleMode(enabled) {
        if (enabled) {
            document.body.setAttribute('video-fit', '')
        } else {
            document.body.removeAttribute('video-fit')
        }
    }
    function injectButton() {
        if (!document.querySelector('.bilibili-player-video-btn-setting-left')) {
            return
        }
        clearInterval(timer)
        const parent = document.querySelector('.bilibili-player-video-btn-setting-left')
        const item = document.createElement('div')
        item.className = 'bilibili-player-video-btn-setting-left-fit-mode bui bui-switch'
        item.innerHTML = '<input class="bui-switch-input" type="checkbox"><label class="bui-switch-label"><span class="bui-switch-name">裁切模式</span><span class="bui-switch-body"><span class="bui-switch-dot"><span></span></span></span></label>'
        parent.insertBefore(item, document.querySelector('.bilibili-player-video-btn-setting-left-more'))
        document.querySelector('.bilibili-player-video-btn-setting-left-fit-mode input').addEventListener('change', e => toggleMode(e.target.checked))
        document.querySelector('.bilibili-player-video-btn-setting-box .bui-panel-item').style.height = ''
    }
    timer = setInterval(injectButton, 200)
}

const style = document.createElement('style')
style.textContent = customStyle
document.head.appendChild(style)

QingJ © 2025

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