通过 “/” 键控制弹幕开关,也可通过更改 ”danmu“ 变量 来修改热键
当前为
// ==UserScript==
// @name BiliBili B站 一键开关弹幕 / HotKey to flip DanMu
// @namespace http://tampermonkey.net/
// @version v0.2
// @description 通过 “/” 键控制弹幕开关,也可通过更改 ”danmu“ 变量 来修改热键
// @author Ben
// @match *://*.bilibili.com/*
// @grant none
// @require https://cdn.staticfile.org/jquery/2.1.4/jquery.min.js
// ==/UserScript==
// code reference : https://github.com/akiirui/userscript/blob/bilibili-danmaku-disabler/userscript.js
var danMu = 191; //"/",可以自己更改
const SELECTOR_NATIVE = {
on: "input:checked[class='bui-switch-input']",
off: "input:not(:checked)[class='bui-switch-input']",
};
const SELECTOR_EMBED = {
on: "div[class~='bilibili-player-video-btn-danmaku'][data-text='打开弹幕']",
off: "div[class~='bilibili-player-video-btn-danmaku'][data-text='关闭弹幕']",
};
const IS_EMBED = document.location.hostname === "player.bilibili.com";
const SELECTOR = IS_EMBED ? SELECTOR_EMBED : SELECTOR_NATIVE;
// Skip Charge Support
function skipCharge() {
const skip = () => {
document
.getElementsByClassName("bilibili-player-video-btn-next")[0]
.click();
};
const videoElementA = document.querySelector("video");
const videoElementB = document.querySelector("bwp-video");
if (videoElementA) {
videoElementA.onended = skip;
} else if (videoElementB) {
videoElementB.onended = skip;
}
}
// Disable danmaku
function disableDanmaku() {
const button = document.querySelector(SELECTOR.on);
if (button) {
//document.querySelector(".bui-switch-input").click();
button.click();
skipCharge();
}
if (!document.querySelector(SELECTOR.off)) {
setTimeout(disableDanmaku, 500);
}
}
function enableDanmaku() {
const button = document.querySelector(SELECTOR.off);
if (button) {
button.click();
skipCharge();
}
if (!document.querySelector(SELECTOR.on)) {
setTimeout(enableDanmaku, 500);
}
}
// Disable danmaku with PJAX detector
function disableDanmakuPJAX() {
const obServer = new MutationObserver(disableDanmaku);
const obTarget = document.getElementById("bilibili-player");
const obOption = { childList: true };
disableDanmaku();
obServer.observe(obTarget, obOption);
}
function enableDanmakuPJAX() {
const obServer = new MutationObserver(enableDanmaku);
const obTarget = document.getElementById("bilibili-player");
const obOption = { childList: true };
enableDanmaku();
obServer.observe(obTarget, obOption);
}
// Redirect `bilibili.com/s/video/*` to `bilibili.com/video/*`
if (location.href.includes("/s/video/")) {
location.replace(location.href.replace("/s/video/", "/video/"));
}
$(document).ready(function() {
$(document).keydown(function(event){ //调用键盘编码,按了键盘回调keydown里的function(event)函数,event就是你按的那个按键的code码
switch(event.keyCode){
case danMu:
if( $(".bui-switch-input").is(':checked') ){
// Run disabler
IS_EMBED ? disableDanmaku() : disableDanmakuPJAX();
}else{
IS_EMBED ? enableDanmaku() : enableDanmakuPJAX();
}
break;
}
});
});