Greasy Fork

Greasy Fork is available in English.

SakuraDanmaku 樱花弹幕

yhdm, but with Danmaku from Bilibili 让樱花动漫加载 Bilibili 弹幕

当前为 2022-11-29 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         SakuraDanmaku 樱花弹幕
// @namespace    https://muted.top/
// @version      0.9.2
// @description  yhdm, but with Danmaku from Bilibili  让樱花动漫加载 Bilibili 弹幕
// @author       MUTED64
// @match        *://*.yhdmp.cc/vp/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_addElement
// @connect      api.bilibili.com
// @icon         https://www.yhdmp.cc/yxsf/yh_pic/favicon.ico
// @require      https://bowercdn.net/c/danmaku-2.0.4/dist/danmaku.dom.min.js
// @require      http://greasyfork.icu/scripts/454443-sakuradanmakuclasses/code/SakuraDanmakuClasses.js?version=1122392
// @license      GPLv3
// @run-at       document-end
// ==/UserScript==

"use strict";

async function loadDanmaku() {
  const keyword = document
    .querySelector("title")
    .textContent.replace(/ 第[0-9]+集.*/gi, "")
    .replace(/ Part ?[0-9]+.*/, "");
  const episode = Number(
    document
      .querySelector("body div.gohome > span")
      .textContent.replace(/[^0-9]+/gi, "")
  );
  const autoLoadedMessage = `自动加载弹幕:\n${keyword} 第${episode}集\n如果不是你想要的,请手动填入对应番剧`;
  const loadFailedMessage = `加载弹幕失败:\n${keyword} 第${episode}集\n请检查B站是否存在对应剧集`;
  let danmaku;
  let danmakuLoader;
  let danmakuDOM;

  const iframeDocument = iframe.contentWindow.document;
  const container = iframeDocument.querySelector("div.dplayer-video-wrap");
  const video = iframeDocument.querySelector("div.dplayer-video-wrap > video");
  const iconsBar = iframeDocument.querySelector(
    "div.dplayer-controller > div.dplayer-icons.dplayer-icons-right"
  );

  try {
    danmaku = await loadFromBilibili(keyword, episode);
    await addDialog(autoLoadedMessage);
  } catch(e) {
    console.log(e);
    await addDialog(loadFailedMessage);
  }

  function addDialog(message) {
    GM_addElement(document.body, "div", {
      style:
        "position:fixed;\
              top:50%;\
              left:1em;\
              transform:translateY(-50%);\
              background-color:rgba(0,0,0,0.7);\
              color:white;\
              font:1em sans-serif;\
              padding:1em;\
              border-radius:1em;\
              line-height:1.5;\
              min-width:17em;",
      class: "danmakuChoose",
    });

    document.querySelector(
      ".danmakuChoose"
    ).innerHTML = `<pre style="margin:0">${message}</pre>
    <div style="display:flex;justify-content:space-between;margin:1em 0;"><label for="keyword">番剧名称</label><input style="border-radius:0.2em;padding:0 0.2em;" id="keyword" value="${keyword}"/></div>
    <div style="display:flex;justify-content:space-between;margin:1em 0;"><label for="episode">剧集数</label><input style="border-radius:0.2em;padding:0 0.2em;" id="episode" value="${episode}"/></div>
    <button id="manualDanmakuButton" style="width:100%;margin-bottom:0.2em;">确认</button>
    <div style="display:flex;justify-content:space-between;align-items:center;margin:2em 0 0 0;"><p style="flex:3;display:inline-flex;">或手动上传XML弹幕文件</p><button id="uploadXMLButton" style="flex:1;">选择</button></div>`;

    document
      .querySelector("#manualDanmakuButton")
      .addEventListener("click", () => {
        const keyword = document.querySelector("#keyword").value;
        const episode = document.querySelector("#episode").value;
        reloadDanmaku(keyword, episode);
      });

    document.querySelector("#uploadXMLButton").addEventListener("click", () => {
      const input = document.createElement("input");
      input.type = "file";
      input.accept = "text/xml";
      input.addEventListener("change", () => {
        const reader = new FileReader();
        reader.onload = () => {
          const xml = reader.result;
          reloadDanmaku(null, null, xml);
        };
        reader.readAsText(input.files[0]);
      });
      input.click();
    });
  }

  async function loadFromBilibili(keyword, episode, xml) {
    while (!container || !video){
      await wait(1);
    }
    load();

    function wait(seconds) {
      return new Promise(resolve => {
        setTimeout(resolve, seconds * 1000);
      });
    } 

    async function load(){
      danmakuLoader = await new DanmakuLoader(keyword, episode, container, video);
      danmaku = await danmakuLoader.showDanmaku(xml);
      danmakuDOM = await danmakuLoader.container.lastElementChild;
      await new DanmakuSettings(danmakuLoader, danmakuDOM, iconsBar, iframeDocument);
      return danmaku;
    }
  }

  async function reloadDanmaku(keyword, episode, xml) {
    if (danmaku) {
      danmaku.destroy();
    }
    await loadFromBilibili(keyword, episode, xml);
  }
}

const iframe = document.querySelector("iframe");
iframe.onload = loadDanmaku;