Greasy Fork is available in English.

B站自动字幕提取

简单读取b站字幕

// ==UserScript==
// @name         B站自动字幕提取
// @namespace    http://tampermonkey.net/
// @version      2024-03-05
// @description  简单读取b站字幕
// @author       琛哥
// @match        https://www.bilibili.com/video/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
// 创建浮动窗口
function addXMLRequestCallback(callback) {
        var oldSend, i;
        if (XMLHttpRequest.callbacks) {
            XMLHttpRequest.callbacks.push(callback);
        } else {
            XMLHttpRequest.callbacks = [callback];
            oldSend = XMLHttpRequest.prototype.send;
            XMLHttpRequest.prototype.send = function () {
                for (i = 0; i < XMLHttpRequest.callbacks.length; i++) {
                    XMLHttpRequest.callbacks[i](this);
                }
                return oldSend.apply(this, arguments);
            };
        }
    }
    addXMLRequestCallback(function (xhr) {
        xhr.addEventListener("load", function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                //console.log(xhr.responseURL);
                if(xhr.responseURL.includes("https://aisubtitle.hdslb.com/bfs/ai_subtitle/prod/")) {
                    console.log("true");
                    console.log(xhr);
                    // 将 responseText 解析为 JSON 对象
                    var jsonObject = JSON.parse(xhr.responseText);

                    // 获取 body 数组中所有 "content" 字段的值,并拼接成一个长字符串
                    var concatenatedContent = jsonObject.body.map(item => item.content).join(",");
                    createFloatingDiv(concatenatedContent);
                    console.log(concatenatedContent);
                }
            }
        });
    });

       // 创建浮动窗口
    function createFloatingDiv(content) {
        // 创建 div 元素
        var floatingDiv = document.createElement('div');
        floatingDiv.innerHTML = content;
        floatingDiv.id = 'floatingDiv';
        document.body.appendChild(floatingDiv);

        // 设置样式
        floatingDiv.style.position = 'fixed';
        floatingDiv.style.top = '50px';
        floatingDiv.style.left = '50px';
        floatingDiv.style.border = '1px solid #ccc';
        floatingDiv.style.backgroundColor = '#fff';
        floatingDiv.style.padding = '10px';
        floatingDiv.style.cursor = 'move';
        floatingDiv.style.zIndex = '9999'; // 置于顶层

        // 添加拖动功能
        makeDraggable(floatingDiv);

        // 添加控制按钮
        addControlButtons(floatingDiv);
    }

    // 添加拖动功能
    function makeDraggable(element) {
        var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
        if (document.getElementById(element.id + "header")) {
            // 如果有标题栏,标题栏作为拖动的手柄
            document.getElementById(element.id + "header").onmousedown = dragMouseDown;
        } else {
            // 否则,整个元素作为拖动的手柄
            element.onmousedown = dragMouseDown;
        }

        function dragMouseDown(e) {
            e = e || window.event;
            e.preventDefault();
            // 获取鼠标当前位置
            pos3 = e.clientX;
            pos4 = e.clientY;
            document.onmouseup = closeDragElement;
            // 拖动时调用 dragElement 函数
            document.onmousemove = elementDrag;
        }

        function elementDrag(e) {
            e = e || window.event;
            e.preventDefault();
            // 计算鼠标移动距离
            pos1 = pos3 - e.clientX;
            pos2 = pos4 - e.clientY;
            pos3 = e.clientX;
            pos4 = e.clientY;
            // 设置元素新位置
            element.style.top = (element.offsetTop - pos2) + "px";
            element.style.left = (element.offsetLeft - pos1) + "px";
        }

        function closeDragElement() {
            // 停止拖动
            document.onmouseup = null;
            document.onmousemove = null;
        }
    }

    // 添加控制按钮
    function addControlButtons(element) {
        var closeButton = document.createElement('button');
        closeButton.innerHTML = '×';
        closeButton.style.position = 'absolute';
        closeButton.style.top = '0';
        closeButton.style.left = '0';
        closeButton.onclick = function() {
            element.remove();
        };
        element.appendChild(closeButton);

        var minimizeButton = document.createElement('button');
        minimizeButton.innerHTML = '-';
        minimizeButton.style.position = 'absolute';
        minimizeButton.style.top = '0';
        minimizeButton.style.left = '20px';
        minimizeButton.onclick = function() {
            element.style.display = 'none';
        };
        element.appendChild(minimizeButton);

        var maximizeButton = document.createElement('button');
        maximizeButton.innerHTML = '+';
        maximizeButton.style.position = 'absolute';
        maximizeButton.style.top = '0';
        maximizeButton.style.left = '40px';
        maximizeButton.onclick = function() {
            element.style.display = 'block';
        };
        element.appendChild(maximizeButton);

        var copyButton = document.createElement('button');
        copyButton.innerHTML = 'Copy';
        copyButton.style.position = 'absolute';
        copyButton.style.top = '0';
        copyButton.style.left = '60px';
        copyButton.onclick = function() {
            copyToClipboard(element.innerText);
        };
        element.appendChild(copyButton);
    }

    // 复制文本到剪贴板
    function copyToClipboard(text) {
        var textarea = document.createElement("textarea");
        textarea.value = text;
        document.body.appendChild(textarea);
        textarea.select();
        document.execCommand("copy");
        document.body.removeChild(textarea);
        alert("内容已复制到剪贴板");
    }


    // Your code here...
})();