Make BiliBili Grate Again

useful tweaks for bilibili.com

目前為 2021-08-29 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

let customStyle = `
.home-content {
    width: calc(100vw - 60px) !important;
    min-width: 1160px;
}

.home-content .center-panel {
    flex: 1 !important;
}

.home-content .center-panel .card .main-content {
    width: calc(100% - 120px) !important;
}
`

// 动态页面优化
if (location.host === "t.bilibili.com") {
    customStyle += `
#internationalHeader {
    position: sticky !important;
    top: 0;
}

.home-content .left-panel .scroll-content {
    top: 56px !important;
}

.home-content .right-panel .scroll-content {
    top: 64px !important;
}
`
}

// 去广告
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)
}

// 视频裁切
if (location.href.startsWith('https://www.bilibili.com/video/')) {
    customStyle += `
body[video-fit] .bilibili-player-video 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) {
        localStorage.setItem('mbga-video-fit-mode', enabled ? 'Y' : 'N')
        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').checked = localStorage.getItem('mbga-video-fit-mode') === 'Y'
        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)
    toggleMode(localStorage.getItem('mbga-video-fit-mode') === 'Y')
}

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