// ==UserScript== // @name 哔哩哔哩(bilibili.com)播放页调整 // @license GPL-3.0 License // @namespace https://greasyfork.org/zh-CN/scripts/415804-bilibili%E6%92%AD%E6%94%BE%E9%A1%B5%E8%B0%83%E6%95%B4-%E8%87%AA%E7%94%A8 // @version 0.8.0 // @description 1.自动定位到播放器(进入播放页,可自动定位到播放器,可设置偏移量及是否在点击主播放器时定位);2.可设置是否自动选择最高画质;3.可设置播放器默认模式; // @author QIAN // @match *://*.bilibili.com/video/* // @match *://*.bilibili.com/bangumi/play/* // @require https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js // @require https://cdn.jsdelivr.net/npm/sweetalert2@11.3.6/dist/sweetalert2.all.min.js // @resource swalStyle https://cdn.jsdelivr.net/npm/sweetalert2@11.3.6/dist/sweetalert2.min.css // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @grant GM_getResourceText // @grant GM.info // @supportURL https://github.com/QIUZAIYOU/Bilibili-VideoPage-Adjustment // @homepageURL https://github.com/QIUZAIYOU/Bilibili-VideoPage-Adjustment // @icon https://www.bilibili.com/favicon.ico?v=1 // @downloadURL none // ==/UserScript== $(function () { const utils = { getValue (name) { return GM_getValue(name) }, setValue (name, value) { GM_setValue(name, value) }, exist (selecter) { return $(selecter).length >= 1 }, addStyle (id, tag, css) { tag = tag || 'style' const doc = document const styleDom = doc.getElementById(id) if (styleDom) return const style = doc.createElement(tag) style.rel = 'stylesheet' style.id = id tag === 'style' ? (style.innerHTML = css) : (style.href = css) document.head.appendChild(style) }, sleep (time) { return new Promise((resolve) => setTimeout(resolve, time)); }, getScrollTop () { var scroll_top = 0 if (document.documentElement && document.documentElement.scrollTop) { scroll_top = document.documentElement.scrollTop } else if (document.body) { scroll_top = document.body.scrollTop } return scroll_top }, documentHidden(){ var hidden if (typeof document.hidden !== "undefined") { hidden = "hidden"; } else if (typeof document.mozHidden !== "undefined") { hidden = "mozHidden"; } else if (typeof document.msHidden !== "undefined") { hidden = "msHidden"; } else if (typeof document.webkitHidden !== "undefined") { hidden = "webkitHidden"; } return document[hidden] } } const main = { initValue () { const value = [ { name: 'player_type', value: 'video' }, { name: 'offset_top', value: 7 }, { name: 'player_offset_top', value: 160 }, { name: 'is_vip', value: false }, { name: 'click_player_auto_locate', value: true }, { name: 'current_screen_mod', value: 'normal' }, { name: 'selected_screen_mod', value: 'widescreen' }, { name: 'auto_select_video_highest_quality', value: true }, { name: 'contain_quality_4k', value: false } ] value.forEach((v) => { if (utils.getValue(v.name) === undefined) { utils.setValue(v.name, v.value) } }) }, autoLocation () { const offset_top = utils.getValue('offset_top') const click_player_auto_locate = utils.getValue( 'click_player_auto_locate' ) const player_type = utils.getValue('player_type') if (player_type === 'video') { if (utils.exist('#playerWrap #bilibili-player')) { const player_offset_top = $('#playerWrap').offset().top utils.setValue('player_offset_top', player_offset_top) // console.log('播放页调整:',player_offset_top,offset_top) console.log('播放页调整:第一次自动定位') $('html,body').scrollTop(player_offset_top - offset_top) const checkAutoLocationStatus = setInterval(function(){ const document_scroll_top = $(document).scrollTop() const success = document_scroll_top === player_offset_top - offset_top if(success){ clearInterval(checkAutoLocationStatus) $('html,body').scrollTop(player_offset_top - offset_top) console.log('播放页调整:自动定位成功') $('body').css('overflow', 'unset') }else{ console.log( '播放页调整:自动定位失败,继续尝试', '\n', '-----------------', '\n', '当前顶部偏移量:'+ document_scroll_top, '\n', '播放器顶部偏移量:' + player_offset_top, '\n', '设置偏移量:' + offset_top, '\n', '期望偏移量:' + (player_offset_top - offset_top) ) $('html,body').scrollTop(player_offset_top - offset_top) } },1000) if (click_player_auto_locate) { $('#bilibili-player').on('click', function () { $('html,body').scrollTop(player_offset_top - offset_top) }) } } } if (player_type === 'bangumi') { if (utils.exist('#player_module #bilibili-player')) { const player_offset_top = $('#player_module').offset().top utils.setValue('player_offset_top', player_offset_top) $('html,body').scrollTop(player_offset_top - offset_top) const checkAutoLocationStatus = setInterval(function(){ const document_scroll_top = $(document).scrollTop() const success = document_scroll_top === player_offset_top - offset_top if(success){ clearInterval(checkAutoLocationStatus) $('html,body').scrollTop(player_offset_top - offset_top) console.log('播放页调整:自动定位成功') $('body').css('overflow', 'unset') }else{ console.log('播放页调整:自动定位失败,继续尝试', '\n', '-----------------', '\n', '当前顶部偏移量:'+ document_scroll_top, '\n', '播放器顶部偏移量:' + player_offset_top, '\n', '设置偏移量:' + offset_top, '\n', '期望偏移量:' + (player_offset_top - offset_top) ) $('html,body').scrollTop(player_offset_top - offset_top) } },1000) if (click_player_auto_locate) { $('#bilibili-player').on('click', function () { $('html,body').scrollTop(player_offset_top - offset_top) }) } } } }, getCurrentPlayerTypeAndScreenMod () { const currentUrl = window.location.href const player_type = utils.getValue('player_type') if (currentUrl.includes('www.bilibili.com/video')) { utils.setValue('player_type', 'video') } if (currentUrl.includes('www.bilibili.com/bangumi/play')) { utils.setValue('player_type', 'bangumi') } if (player_type === 'video') { if (utils.exist('#playerWrap #bilibili-player')) { const playerDataScreen = $('.bpx-player-container').attr('data-screen') || "NULL" // console.log('播放页调整:' + screenMod) const screenModObserver = new MutationObserver(function (mutations) { mutations.forEach(function (mutation) { if (playerDataScreen.includes('wide')) { utils.setValue('current_screen_mod', 'widescreen') } if (playerDataScreen.includes('web')) { utils.setValue('current_screen_mod', 'webfullscreen') } }) }) screenModObserver.observe($('#bilibili-player')[0], { attributes: true }) } } if (player_type === 'bangumi') { if (utils.exist('#player_module #bilibili-player')) { const playerDataScreen = $( '#bilibili-player .bpx-player-container' ).attr('data-screen') const screenModObserver = new MutationObserver(function (mutations) { mutations.forEach(function (mutation) { if (playerDataScreen === 'normal') { utils.setValue('current_screen_mod', 'normal') } if (playerDataScreen === 'wide') { utils.setValue('current_screen_mod', 'widescreen') } if (playerDataScreen === 'web') { utils.setValue('current_screen_mod', 'webfullscreen') } }) }) screenModObserver.observe($('#bilibili-player')[0], { attributes: true }) } } }, autoSelectScreenMod () { const player_type = utils.getValue('player_type') const current_screen_mod = utils.getValue('current_screen_mod') const selected_screen_mod = utils.getValue('selected_screen_mod') $('#bilibili-player').addClass('bilibili-videopage-adjustment') if (player_type === 'video') { if (utils.exist('#playerWrap #bilibili-player')) { // console.log('播放页调整:','current_screen_mod, selected_screen_mod); const playerDataScreen = $('.bpx-player-container').attr('data-screen') || "NULL" if ( selected_screen_mod === 'normal' && current_screen_mod !== 'normal' ) { $('.bpx-player-ctrl-wide-leave').click() } if ( selected_screen_mod === 'widescreen' && current_screen_mod !== 'widescreen' && !playerDataScreen.includes('wide') ) { $('.bpx-player-ctrl-wide-enter').click() console.log('播放页调整:第一次切换:宽屏') const checkClickStatus = setInterval(function(){ const success = $('.bpx-player-container').attr('data-screen').includes('wide') if(success){ clearInterval(checkClickStatus) console.log('播放页调整:宽屏切换成功') }else{ $('.bpx-player-ctrl-wide-enter').click() console.log('播放页调整:宽屏切换失败,继续尝试') } },1000) // await utils.sleep(1000) // alert('已自动切换宽屏') } if ( selected_screen_mod === 'webfullscreen' && current_screen_mod !== 'webfullscreen' && !playerDataScreen.includes('web') ) { $('.bpx-player-ctrl-web-enter').click() console.log('播放页调整:第一次切换:网页全屏') const checkClickStatus = setInterval(function(){ const success = $('.bpx-player-container').attr('data-screen').includes('web') if(success){ clearInterval(checkClickStatus) console.log('播放页调整:网页全屏切换成功') }else{ $('.bpx-player-ctrl-web-enter').click() console.log('播放页调整:网页全屏切换失败,继续尝试') } },1000) } } } if (player_type === 'bangumi') { if (utils.exist('#player_module #bilibili-player')) { // console.log('播放页调整:','b', current_screen_mod, selected_screen_mod); const playerDataScreen = $( '#bilibili-player .bpx-player-container' ).attr('data-screen') if ( selected_screen_mod === 'normal' && current_screen_mod !== 'normal' ) { $('.squirtle-controller-wrap-right .squirtle-video-item.active').click() } if ( selected_screen_mod === 'widescreen' && current_screen_mod !== 'widescreen' && playerDataScreen !== 'wide' ) { $('.squirtle-widescreen-wrap .squirtle-video-widescreen').click() console.log('播放页调整:第一次切换:宽屏') const checkClickStatus = setInterval(function(){ const success = $('#bilibili-player .bpx-player-container').attr('data-screen').includes('wide') if(success){ clearInterval(checkClickStatus) console.log('播放页调整:宽屏切换成功') }else{ $('.squirtle-widescreen-wrap .squirtle-video-widescreen').click() console.log('播放页调整:宽屏切换失败,继续尝试') } },700) } if ( selected_screen_mod === 'webfullscreen' && current_screen_mod !== 'webfullscreen' && playerDataScreen !== 'web' ) { $('.squirtle-video-item.squirtle-video-pagefullscreen').click() console.log('播放页调整:第一次切换:网页全屏') const checkClickStatus = setInterval(function(){ const success = $('#bilibili-player').attr('class').includes('full-screen') if(success){ clearInterval(checkClickStatus) console.log('播放页调整:网页全屏切换成功') }else{ $('.squirtle-video-item.squirtle-video-pagefullscreen').click() console.log('播放页调整:网页全屏切换失败,继续尝试') } },1000) } } } $('#bilibili-player').removeClass('bilibili-videopage-adjustment') }, autoSelectVideoHightestQuality () { const player_type = utils.getValue('player_type') const is_vip = utils.getValue('is_vip') const contain_quality_4k = utils.getValue('contain_quality_4k') const auto_select_video_highest_quality = utils.getValue( 'auto_select_video_highest_quality' ) if (auto_select_video_highest_quality) { if (is_vip) { if (contain_quality_4k) { if (player_type === 'video') { if (utils.exist('#playerWrap #bilibili-player')) { $('.bpx-player-ctrl-quality > ul > li').eq(0).click() console.log('播放页调整:VIP最高画质(包含4K)切换成功') } } if (player_type === 'bangumi') { if (utils.exist('#player_module #bilibili-player')) { $('.squirtle-quality-wrap >.squirtle-video-quality > ul > li').eq(0).click() console.log('播放页调整:VIP最高画质(包含4K)切换成功') } } } else { if (player_type === 'video') { if (utils.exist('#playerWrap #bilibili-player')) { const qualityValue = $('.bpx-player-ctrl-quality > ul > li').filter(function () { return !$(this).children('span.bpx-player-ctrl-quality-text').text().includes('4K') }) qualityValue.eq(0).click() console.log('播放页调整:VIP最高画质(不包含4K)切换成功') } } if (player_type === 'bangumi') { if (utils.exist('#player_module #bilibili-player')) { const qualityValue = $('.squirtle-quality-wrap > .squirtle-video-quality > ul > li').filter(function () { return !$(this).children('.squirtle-quality-text-c').children('.squirtle-quality-text').text().includes('4K') }) qualityValue.eq(0).click() console.log('播放页调整:VIP最高画质(不包含4K)切换成功') } } } } else { if (player_type === 'video') { if (utils.exist('#playerWrap #bilibili-player')) { const selectVipItemLength = $( '.bpx-player-ctrl-quality > ul > li' ).children('.bilibili-player-bigvip').length $('.bpx-player-ctrl-quality > ul > li').eq(selectVipItemLength).click() console.log('播放页调整:非VIP最高画质切换成功') } } if (player_type === 'bangumi') { if (utils.exist('#player_module #bilibili-player')) { const selectVipItemLength = $( '.squirtle-quality-wrap >.squirtle-video-quality > ul > li' ).children('.squirtle-bigvip').length $('.squirtle-quality-wrap >.squirtle-video-quality > ul > li') .eq(selectVipItemLength) .click() console.log('播放页调整:非VIP最高画质切换成功') } } } } }, registerMenuCommand () { GM_registerMenuCommand('设置', () => { const html = `
-> 请如实勾选,否则影响自动选择清晰度 -> 参考值:顶部导航栏吸顶时为 71 ,否则为 7
播放器默认模式
-> 若遇到不能自动选择播放器模式可尝试点击重置 -> 网络条件好时可以启用此项,自动选择最高画质时将选择4K画质,否则选择除4K外最高画质。
` Swal.fire({ title: '播放页调整设置', html, icon: 'info', showCloseButton: true, showDenyButton: true, confirmButtonText: '保存', denyButtonText: '重置', footer: '
如果发现脚本不能用,说明你的播放页面已经更新为新版。
目前此脚本不适用新版播放页面, 因为我的两个号都还没收到新版播放页面的推送, 所以暂时没法适配, 等我收到更新后会第一时间适配。

夜间哔哩 - 检查更新
' }).then((res) => { res.isConfirmed && location.reload(true) if (res.isConfirmed) { location.reload(true) } else if (res.isDenied) { utils.setValue('current_screen_mod', 'normal') location.reload(true) } }) $('#Is-Vip').change((e) => { utils.setValue('is_vip', e.target.checked) if (e.target.checked === true) { $('.4k').css('display', 'none!important') } else { $('.4k').css('display', 'none!important') } }) $('#Top-Offset').change((e) => { utils.setValue('offset_top', e.target.value) }) $('#Click-Player-Auto-Location').change((e) => { utils.setValue('click_player_auto_locate', e.target.checked) // console.log('播放页调整:',utils.getValue('click_player_auto_locate')) }) $('#Auto-Quality').change((e) => { utils.setValue('auto_select_video_highest_quality', e.target.checked) }) $('#Quality-4K').change((e) => { utils.setValue('contain_quality_4k', e.target.checked) }) $('input[name="Screen-Mod"]').click(function () { utils.setValue('selected_screen_mod', $(this).val()) // console.log('播放页调整:',utils.getValue('selected_screen_mod')); }) }) }, addPluginStyle () { const style = ` .swal2-popup{width: 34em !important;padding: 1.25em !important;} .swal2-html-container{margin: 0 !important;padding: 16px 5px 0 !important;width: 100% !important;box-sizing: border-box !important;} .swal2-footer{flex-direction: column !important;} .swal2-close{top: 5px !important;right: 3px !important;} .swal2-actions{margin: 7px auto 0 !important;} .swal2-styled.swal2-confirm{background-color: #23ADE5 !important;} .swal2-icon.swal2-info.swal2-icon-show{display: none !important;} .player-adjustment-container,.swal2-container { z-index: 999999999 !important;} .player-adjustment-popup { font-size: 14px !important } .player-adjustment-setting-label { display: flex !important;align-items: center !important;justify-content: space-between !important;padding-top: 20px !important; } .player-adjustment-setting-checkbox { width: 16px !important;height: 16px !important; } .player-adjustment-setting-tips{width: 100% !important;display: flex !important;align-items: center !important;padding: 5px !important;margin-top: 10px !important;background: #f5f5f5 !important;box-sizing: border-box !important;color: #666 !important;border-radius: 2px !important;text-align: left !important;} .player-adjustment-setting-tips svg{margin-right: 5px !important} label.player-adjustment-setting-label input{border: 1px solid #cecece!important;background: #ffffff!important;} label.player-adjustment-setting-label input:checked{border-color: #1986b3!important;background: #23ADE5!important;} ` if (document.head) { utils.addStyle( 'swal-pub-style', 'style', GM_getResourceText('swalStyle') ) utils.addStyle('player-adjustment-style', 'style', style) } const headObserver = new MutationObserver(() => { utils.addStyle( 'swal-pub-style', 'style', GM_getResourceText('swalStyle') ) utils.addStyle('player-adjustment-style', 'style', style) }) headObserver.observe(document.head, { childList: true, subtree: true }) }, applySetting () { const checkDocumentHidden = setInterval(async () => { if(await !utils.documentHidden()){ clearInterval(checkDocumentHidden) console.log( ' ' + GM.info.script.name, '\n', '脚本作者:' + GM.info.script.author, '\n', '-----------------', '\n', 'player_type: ' + utils.getValue('player_type'), '\n', 'offset_top: ' + utils.getValue('offset_top'), '\n', 'player_offset_top: ' + utils.getValue('player_offset_top'), '\n', 'is_vip: ' + utils.getValue('is_vip'), '\n', 'click_player_auto_locate: ' + utils.getValue('click_player_auto_locate'), '\n', 'current_screen_mod: ' + utils.getValue('current_screen_mod'), '\n', 'selected_screen_mod: ' + utils.getValue('selected_screen_mod'), '\n', 'auto_select_video_highest_quality: ' + utils.getValue('auto_select_video_highest_quality') ) let applyed = false const applyChanges = setInterval(async () => { await utils.sleep(2000); const player_type = utils.getValue('player_type') const selected_screen_mod = utils.getValue('selected_screen_mod') if (player_type === 'video') { if (utils.exist('#playerWrap #bilibili-player')) { // const playerClass = $('#bilibili-player').attr('class') if (utils.exist('.bpx-player-control-bottom')) { if(!applyed){ main.insertLocateButton() main.autoSelectScreenMod() main.autoSelectVideoHightestQuality() main.autoLocation() applyed = true }else{ $("#viewbox_report").attr("style","height:106px!important") $(".wide-members").attr("style","height: 99px; overflow: hidden; padding: 10px; box-sizing: border-box;margin-top: -18px;") clearInterval(applyChanges) } } } } if (player_type === 'bangumi') { if (utils.exist('#player_module #bilibili-player')) { const playerDataScreen = $( '#bilibili-player .bpx-player-container' ).attr('data-screen') if (utils.exist('.squirtle-controller-wrap')) { if(!applyed){ main.insertLocateButton() main.autoSelectScreenMod() main.autoLocation() main.autoSelectVideoHightestQuality() applyed = true }else{ clearInterval(applyChanges) } } } } }, 1000) } else { console.log("播放页调整:当前标签未激活,正在重试") } },500) }, insertLocateButton () { const player_type = utils.getValue('player_type') if (player_type === 'video') { const locateButtonHtml = `
` const floatNav = $('.float-nav-exp .nav-menu') const locateButton = $('.float-nav-exp .nav-menu .item.locate') const offset_top = utils.getValue('offset_top') const player_offset_top = utils.getValue('player_offset_top') $('.fixed-nav').css('bottom','274px') floatNav.prepend(locateButtonHtml) locateButton.not(':first-child').remove() floatNav.on('click', '.locate', function () { $('html,body').scrollTop(player_offset_top - offset_top) // console.log('播放页调整:',player_offset_top,offset_top); }) } if (player_type === 'bangumi') { const locateButtonHtml = `
` const floatNav = $('.nav-tools') const locateButton = $('.nav-tools .tool-item.locate') const offset_top = utils.getValue('offset_top') const player_offset_top = utils.getValue('player_offset_top') floatNav.prepend(locateButtonHtml) locateButton.not(':first-child').remove() floatNav.on('click', '.locate', function () { $('html,body').scrollTop(player_offset_top - offset_top) // console.log('播放页调整:',player_offset_top,offset_top); }) } }, autoCancelMute () { const player_type = utils.getValue('player_type') if (player_type === 'video') { const muteObserver = setInterval(() => { const cancelMuteButtn = $('.bpx-player-ctrl-muted-icon') const cancelMuteButtnDisplay = cancelMuteButtn.css('display') if (cancelMuteButtnDisplay === 'block') { cancelMuteButtn.click() console.log('播放页调整:已自动取消静音'); } if (cancelMuteButtnDisplay === 'none') { clearInterval(muteObserver) } }, 1000) } if (player_type === 'bangumi') { const muteObserver = setInterval(() => { const cancelMuteButtn = $('.squirtle-volume-wrap .squirtle-volume .squirtle-volume-icon') const cancelMuteButtnClass = cancelMuteButtn.attr('class') if (cancelMuteButtnClass.includes('squirtle-volume-mute-state')) { cancelMuteButtn.click() console.log('播放页调整:已自动取消静音'); } if (cancelMuteButtnClass.includes('squirtle-volume-active-state')) { clearInterval(muteObserver) } }, 1000) } }, playerLoadStateWatcher () { const player_type = utils.getValue('player_type') if (player_type === 'video') { if (utils.exist('#playerWrap #bilibili-player')) { const playerLoadStateWatcher = setInterval(() => { const playerVideoLength = $('.bpx-player-video-wrap').children().length // console.log('播放页调整:',playerVideoLength); if (playerVideoLength === 0) { location.reload(true) } else { clearInterval(playerLoadStateWatcher) // console.log('播放页调整:获取到播放器,开始应用设置'); } }, 1000) } } if (player_type === 'bangumi') { if (utils.exist('#player_module #bilibili-player')) { const playerLoadStateWatcher = setInterval(() => { const playerVideoLength = $('.bpx-player-video-wrap').children().length // console.log('播放页调整:',playerVideoLength); if (playerVideoLength === 0) { location.reload(true) } else { clearInterval(playerLoadStateWatcher) } }, 1500) } } }, isTopWindow () { return window.self === window.top }, init () { $("body").css("overflow", "hidden"); this.initValue(); this.addPluginStyle(); this.playerLoadStateWatcher() this.getCurrentPlayerTypeAndScreenMod(); this.applySetting(); this.playerLoadStateWatcher(); this.autoCancelMute(); this.isTopWindow() && this.registerMenuCommand(); window.history.pushState = () => { console.log("播放页调整:当前页面链接发生变化,重新应用定位设置"); main.autoLocation() } } } main.init() })