B站笔记快捷键←↓→

在B站笔记中:打开笔记快捷键:Ctrl+Shift+↑,关闭笔记快捷键:Ctrl+Shift+↓,笔记窗口切换到视频窗口:Ctrl+Shift+←,视频窗口切换到笔记窗口:Ctrl+Shift+→,截图+时间戳快捷键:Ctrl+↓,时间戳+截图快捷键:Ctrl+↑,时间戳快捷键:Ctrl+←,截图快捷键:Ctrl+→,下载视频笔记Ctrl+Shift+S

目前為 2022-12-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name         B站笔记快捷键←↓→
// @namespace    https://space.bilibili.com/1208812226
// @version      2.5
// @description  在B站笔记中:打开笔记快捷键:Ctrl+Shift+↑,关闭笔记快捷键:Ctrl+Shift+↓,笔记窗口切换到视频窗口:Ctrl+Shift+←,视频窗口切换到笔记窗口:Ctrl+Shift+→,截图+时间戳快捷键:Ctrl+↓,时间戳+截图快捷键:Ctrl+↑,时间戳快捷键:Ctrl+←,截图快捷键:Ctrl+→,下载视频笔记Ctrl+Shift+S
// @author       大王鹅鹅鹅
// @match        http*://www.bilibili.com/video/*
// @match        https://www.bilibili.com/medialist/*
// @icon         https://static.hdslb.com/images/favicon.ico
// @grant        none
// @license      AGPL License
// ==/UserScript==

(function () {
  "use strict";
  // JS监听键盘快捷键事件
  document.addEventListener("keydown", function (event) {
    if (event.shiftKey && event.keyCode == 38) {//打开笔记——快捷键:Ctrl+Shift+↑
      if (event.ctrlKey) {
        document.querySelector("div.note-btn.note-btn__blue").click();
        document
          .querySelector("div.note-list-footer > div.note-list-btn")
          .click();
        var count = 2;
        var timeId = setInterval(function () {
          count--;
          if (count <= 0) {
            clearInterval(timeId);
          }
          initFocusDown();
        }, 600);
      }
    }

    if (event.shiftKey && event.keyCode == 40) {//关闭笔记——快捷键:Ctrl+Shift+↓
      if (event.ctrlKey) {
        document
          .querySelector("i.bili-note-iconfont.iconiconfont_icon_close")
          .click();
      }
    }

    if (event.shiftKey && event.keyCode == 37) {//回到视频——快捷键:Ctrl+Shift+←
      if (event.ctrlKey) {
        var pNodel = document.querySelector(
          "div.editor-innter.ql-container.ql-snow > div.ql-editor"
        );
        pNodel.blur();
      }
    }
    if (event.shiftKey && event.keyCode == 39) {//回到笔记——快捷键:Ctrl+Shift+→
      if (event.ctrlKey) {
        var el = document.querySelector(
          "div.editor-innter.ql-container.ql-snow > div.ql-editor"
        );
        var scrollHeight = el.scrollHeight;
        el.scrollTo(0, scrollHeight);
        el.focus();
        if (
          typeof window.getSelection != "undefined" &&
          typeof document.createRange != "undefined"
        ) {
          var range = document.createRange();
          range.selectNodeContents(el);
          range.collapse(false);
          var sel = window.getSelection();
          sel.removeAllRanges();
          sel.addRange(range);
        } else if (typeof document.body.createTextRange != "undefined") {
          var textRange = document.body.createTextRange();
          textRange.moveToElementText(el);
          textRange.collapse(false);
          textRange.select();
        }
      }
    }

    if (event.ctrlKey && event.keyCode == 37) {//时间戳——快捷键:Ctrl+←
      if (!event.shiftKey) {
        document.querySelector("i.bili-note-iconfont.iconicon_flag_L").click();
        setTimeout(function () {
          document
            .querySelector(
              "div.dialog-btn.tag-dialog__btn--confirm:nth-child(2)"
            )
            .click();
        }, 5 * 100);
      }
    }

    if (event.ctrlKey && event.keyCode == 38) {//时间戳+截图——快捷键:Ctrl+↑
      if (!event.shiftKey) {
          imageAfterTime();
      }
    }

    if (event.ctrlKey && event.keyCode == 39) {//截图——快捷键:Ctrl+→
      if (!event.shiftKey) {
        document.querySelector("i.bili-note-iconfont.iconcapture-app").click();
        setTimeout(function () {
          var parentNode = document.querySelector(
            "div.editor-innter.ql-container.ql-snow > div.ql-editor"
          );
          var scrollHeight = parentNode.scrollHeight;
          parentNode.scrollTo(0, scrollHeight);
          parentNode.focus();
        }, 500);
      }
    }

    if (event.ctrlKey && event.keyCode == 40) {//截图+时间戳——快捷键:Ctrl+↓
      if (!event.shiftKey) {
       timeAfterImage();
      }
    }

   if (event.shiftKey && event.keyCode == 83) {//下载笔记为markdown格式——快捷键:Ctrl+Shift+S
      if (event.ctrlKey) {
        openNote();
      }
    }

  });



  function initFocusDown() {
    document.querySelector("div.note-btn.note-btn__blue:nth-child(2)").click();
    document.querySelector("div.note-list-footer > div.note-list-btn").click();
    var el = document.querySelector(
      "div.editor-innter.ql-container.ql-snow > div.ql-editor"
    );
    var scrollHeight = el.scrollHeight;
    el.scrollTo(0, scrollHeight);
    el.focus();
    if (
      typeof window.getSelection != "undefined" &&
      typeof document.createRange != "undefined"
    ) {
      var range = document.createRange();
      range.selectNodeContents(el);
      range.collapse(false);
      var sel = window.getSelection();
      sel.removeAllRanges();
      sel.addRange(range);
    } else if (typeof document.body.createTextRange != "undefined") {
      var textRange = document.body.createTextRange();
      textRange.moveToElementText(el);
      textRange.collapse(false);
      textRange.select();
    }
  }

  function openNote() {
    const editor = document.querySelector(".active-note")
      ? document.querySelector(".active-note").querySelector(".ql-editor")
      : null;
      if(!editor){alert("请先打开视频笔记,快捷键:Ctrl+Shift+↑");return;}
    const isBlank = editor && !editor.classList.contains("ql-blank");

    if (isBlank) {
      const noteContent = getNoteContent(editor);
      downloadMD(noteContent);
    } else {
      alert("笔记为空,无法下载");
    }
  }

  function getNoteContent(editor) {
    if (
      editor.nodeType === 1 &&
      editor.childNodes &&
      editor.childNodes.length > 0
    ) {
      return getNodes2Array(editor.childNodes);
    }
    return "";
  }

  function getNodes2Array(nodes) {
    const contents = [];
    for (let i = 0; i < nodes.length; i++) {
      const node = nodes[i];
      if (node.nodeType == 1) {
        if (node.nodeName == "P") {
          contents.push(node2string(node));
        } else if (node.nodeName == "OL" || node.nodeName == "UL") {
          contents.push(getLiContent(node));
        } else if (node.nodeName == "DIV") {
          contents.push(divImgContent(node));
        } else {
          contents.push(node.textContent);
        }
      }
    }
    return contents;
  }

  function getLiContent(node) {
    let allLiContent = "";
    let nodeName = node.nodeName;
    let liNodes = node.childNodes;
    let indentLevels = {};

    if (liNodes && liNodes.length > 0) {
      for (let li of liNodes) {
        let liclass = li.getAttribute("class");
        let indentLevel = 0;
        if (liclass) {
          let match = liclass.match(/ql-indent-(\d+)/);
          if (match) {
            indentLevel = parseInt(match[1]);
          }
        }

        indentLevels[indentLevel] = (indentLevels[indentLevel] || 0) + 1;

        let indent = "";
        for (let i = 0; i < indentLevel; i++) {
          indent += "\t";
        }

        allLiContent +=
          indent +
          (nodeName == "OL" ? indentLevels[indentLevel] + ". " : "- ") +
          node2string(li) +
          "\n";
      }
    }

    return allLiContent;
  }

  function divImgContent(node) {
    if (!node.classList || !node.classList.contains("ql-image-preview")) {
      return;
    }

    let img = node.querySelector("img");
    let src = img ? "https:" + img.getAttribute("src") : "";
    if (!src.includes("http")) {
      return;
    }

    let content = src ? src : "";
    if (!content.match(/api.bilibili.com\/x\/note\/image/g)) {
      content = "![](" + content + ")";
    }
    return content;
  }

  function node2string(node) {
    let nodes = node.childNodes;
    if (!nodes || nodes.length <= 0) return "";
    let line = "";

    for (let i = 0; i < nodes.length; i++) {
      let child = nodes[i];
      let tag = child.nodeName;
      let myContents = "";

      if (
        (child.classList && child.classList.contains("ql-tag-blot")) ||
        tag === "DIV"
      ) {
        //时间戳
        let timeText = child.querySelector(".time-tag-item__text");
        let desc = child.querySelector(".time-tag-item__desc");
        let p = child.getAttribute("data-index");
        let second = child.getAttribute("data-seconds");
        let pUrl = window.location.href;
        let match = pUrl.match(
          /(https\:\/\/www.bilibili.com\/video\/)(BV|av)[a-zA-Z0-9]+(?=[\/\?])?/g
        );
        pUrl = match ? match[0] : pUrl;

        let title =
          "🚩" +
          timeText.firstChild.textContent +
          (desc ? " " + desc.textContent : "");
        let time = `${pUrl}?p=${p}&t=${second}`;

        myContents += `[${title}](${time})`;
      } else {
        if (!child.children || child.children.length == 0) {
          myContents += child.textContent;
        } else {
          myContents += node2string(child);
        }

        //针对文字ql-bg
        let className = "";
        if (child.classList) {
          let myClassList = child.classList;
          for (let a = 0; a < myClassList.length; a++) {
            if (myClassList[a].match(/ql-bg/)) {
              className += "background-color:" + myClassList[a].slice(-7) + ";";
            }
            if (myClassList[a].match(/ql-color/)) {
              className += "color:" + myClassList[a].slice(-7) + ";";
            }
            if (myClassList[a].match(/ql-size/)) {
              className +=
                "font-size:" +
                myClassList[a].slice(8, myClassList[a].length) +
                ";";
            }
          }
        }

        let font1 = className != "" ? "<font style='" + className + "'>" : "";
        let font2 = className != "" ? "</font>" : "";
        myContents = font1 + myContents + font2;

        if (tag == "U" || tag == "S" || tag == "STRONG") {
          myContents =
            "<" +
            tag.toLowerCase() +
            ">" +
            myContents +
            "</" +
            tag.toLowerCase() +
            ">";
        } else if (tag == "BR") {
          myContents += "\n";
        } else {
          myContents += "";
        }
      }
      line += myContents;
    }
    return line;
  }
    function getNowTime(){
        var now = new Date();
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();
		hours = hours < 10 ? '0' + hours : hours;
        minutes = minutes < 10 ? '0' + minutes : minutes;
        seconds = seconds < 10 ? '0' + seconds : seconds;

        var dateString = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
        return dateString;
    }

    function getInfo(){
        let allContent ="";
        try{
            const username = document.querySelector(".username").firstChild.textContent.trim();
            const pudate_text = document.querySelector(".pudate-text").firstChild.textContent.trim();
            const views = document.querySelector("span.view.item").getAttribute("title");
            let pUrl = window.location.href;
            let match = pUrl.match(/(https\:\/\/www.bilibili.com\/video\/)(BV|av)[a-zA-Z0-9]+(?=[\/\?])?/g);
            pUrl = match ? match[0] : pUrl;
            allContent = "---\ntags: bilibili_note\nurl: "+pUrl+"\nup: "+username+"\nup_date: "+pudate_text+"\ndown_time: "+getNowTime()+"\nviews: "+views+"\n---\n\n";
        }catch(e){
            let pUrl = window.location.href;
            let match = pUrl.match(/(https\:\/\/www.bilibili.com\/video\/)(BV|av)[a-zA-Z0-9]+(?=[\/\?])?/g);
            pUrl = match ? match[0] : pUrl;
            allContent = "---\ntags: bilibili_note\nurl: "+pUrl+"\ndown_time: "+getNowTime()+"\n---\n\n";
        }
    return allContent;
    }

  function downloadMD(notes) {
    let allContent =getInfo();
    let bid = window.location.href.match(
      /(?<=bilibili.com\/video\/)(BV|av)[a-zA-Z0-9]+(?=[\/\?])?/g
    );

    for (var i = 0; i < notes.length; i++) {
      if (notes[i] != "") {
        let images_id = notes[i].match(
          /(?<=api.bilibili.com\/x\/note\/image\?image_id=)\d{6}/g
        );
        if (images_id) {
          allContent +=
            "![](" + bid[0] + "_" + images_id[0] + ".jpg)" + "\n";
          //下载图片
          downloadImages(notes[i], bid[0]);
        } else {
          allContent += notes[i] + "\n";
        }
      } else {
        allContent += "\n";
      }
    }
    //下载md
    let video_title = document.querySelector(".video-title");
    let v_title = video_title ? video_title.textContent : new Date().getTime();
    setTimeout(function () {
      downloadFiles("bilibili_" + v_title + ".md", allContent);
    }, 1000);
  }

  async function downloadImages(src, bid) {
    let res = await fetch(src, {
      method: "get",
      credentials: src.includes("bilibili.com") ? "include" : "omit",
    });

    if (!res.ok) return;

    let blob = await res.blob();
    let fileName = src.slice(-6);
    downloadFiles(bid + "_" + fileName + ".jpg", blob);
  }


  function downloadFiles(fileName, myblob) {
    let blob = new Blob([myblob]);
    let a = document.createElement("a");
    a.href = URL.createObjectURL(blob);
    a.download = fileName;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  }


   function imageAfterTime() {//时间戳+截图 ↑
        new Promise((resolve,reject)=>{
                document.querySelector('i.bili-note-iconfont.iconicon_flag_L').click();
                return resolve();
            }).then(()=>{
                setTimeout( function(){
                    document.querySelector('div.dialog-btn.tag-dialog__btn--confirm:nth-child(2)').click();
                },500);
            });

            new Promise((resolve,reject)=>{
                 setTimeout( function(){
                    document.querySelector('i.bili-note-iconfont.iconcapture-app').click();
                },1000);
                return resolve();
            }).then(()=>{
                setTimeout( function(){
                 var parentNode=document.querySelector('div.editor-innter.ql-container.ql-snow > div.ql-editor');
                 var childN=parentNode.childNodes;
                 var scrollHeight =parentNode.scrollHeight;
                 parentNode.scrollTo(0,scrollHeight);
                 parentNode.focus();
                    for(var i=1;childN.length-1;i++){
                            if(childN[i].innerHTML=="<br>" && childN[i].previousSibling.innerHTML!="<br>" && childN[i].previousSibling.nodeName == "P" && childN[i].nextSibling.nodeName == "DIV"){
                                childN[i].remove();
                            }

                    }

              },1100);
            });

    }

    function timeAfterImage(){//截图+时间 ↓
          document.querySelector('i.bili-note-iconfont.iconcapture-app').click();
            new Promise((resolve,reject)=>{
                document.querySelector('i.bili-note-iconfont.iconicon_flag_L').click();
                return resolve();
            }).then(()=>{
                setTimeout( function(){
                    document.querySelector('div.dialog-btn.tag-dialog__btn--confirm:nth-child(2)').click();
                    var parentNode=document.querySelector('div.editor-innter.ql-container.ql-snow > div.ql-editor');
                    var childN=parentNode.childNodes;
                    for(var i=0;childN.length;i++){
                        if(childN[i].innerHTML=="<br>" && childN[i].nextSibling.innerHTML!="<br>" && childN[i].nextSibling.nodeName == "P" && childN[i].previousSibling.nodeName == "DIV"){
                           childN[i].remove();
                        }
                    }
                },1500);
            });

    }


     function toEnd(el) {
         var scrollHeight = el.scrollHeight;
         el.scrollTo(0, scrollHeight);
         el.focus();
         if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
             var range = document.createRange();
             range.selectNodeContents(el);
             range.collapse(false);
             var sel = window.getSelection();
             sel.removeAllRanges();
             sel.addRange(range);
         } else if (typeof document.body.createTextRange != "undefined") {
             var textRange = document.body.createTextRange();
             textRange.moveToElementText(el);
             textRange.collapse(false);
             textRange.select();
         }
     }


})();

QingJ © 2025

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