Bilibili 收藏夹json查看与保存

仅在非私密收藏夹界面可用,私密收藏夹不可用,可查看和保存收藏夹界面的json

// ==UserScript==
// @name         Bilibili 收藏夹json查看与保存
// @namespace    https://space.bilibili.com/398910090
// @version      1.4
// @description  仅在非私密收藏夹界面可用,私密收藏夹不可用,可查看和保存收藏夹界面的json
// @author       Ace
// @match        https://www.bilibili.com/*
// @match        https://space.bilibili.com/*/favlist*
// @grant        GM_registerMenuCommand
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
    'use strict';

    // 获取当前页面的所有 cookie
    const cookies = document.cookie;
    console.log('Cookies:', cookies);

    // 判断是否在收藏夹界面
    const isFavoritesPage = window.location.href.includes('https://space.bilibili.com/') && window.location.href.includes('/favlist');

    // 仅在收藏夹页面添加菜单选项
    if (isFavoritesPage) {
        // 提取 JSON 并显示在页面
        GM_registerMenuCommand("查看 Json 数据", function() {
            // 查找匹配的请求
            const matchingRequests = performance.getEntriesByType('resource')
                .filter(entry => entry.name.includes('list?media_id='));

            if (matchingRequests.length === 0) {
                console.log('No matching requests found.');
                return;
            }

            // 获取第一个匹配请求的 URL
            const jsonURL = matchingRequests[0].name;

            // 发送请求获取 JSON 数据
            fetch(jsonURL, {
                headers: {
                    'Cookie': cookies // 将获取到的 cookie 添加到请求头中
                }
            })
            .then(response => response.json())
            .then(data => {
                // 在页面新标签页中显示 JSON 数据
                const jsonPage = window.open();
                jsonPage.document.write('<pre>' + JSON.stringify(data, null, 2) + '</pre>');
            })
            .catch(error => {
                console.error('Error fetching JSON:', error);
            });
        });

        // 提取 JSON 并保存到本地文件
        GM_registerMenuCommand("保存 JSON 数据", function() {
            // 查找匹配的请求
            const matchingRequests = performance.getEntriesByType('resource')
                .filter(entry => entry.name.includes('list?media_id='));

            if (matchingRequests.length === 0) {
                console.log('No matching requests found.');
                return;
            }

            // 获取第一个匹配请求的 URL
            const jsonURL = matchingRequests[0].name;

            // 发送请求获取 JSON 数据
            fetch(jsonURL, {
                headers: {
                    'Cookie': cookies // 将获取到的 cookie 添加到请求头中
                }
            })
            .then(response => response.json())
            .then(data => {
                // 在这里处理获取到的 JSON 数据
                console.log('JSON Data:', data);
                // 调用保存 JSON 文件的功能
                promptForFileName(data);
            })
            .catch(error => {
                console.error('Error fetching JSON:', error);
            });
        });
    }

    // 弹出提示框询问是否修改文件名
    function promptForFileName(data) {
        // 读取存储的文件名,如果没有则使用默认文件名
        let defaultFileName = GM_getValue("customFileName", 'favlist_data.json');

        // 询问用户是否修改文件名
        if (window.confirm(`当前文件名为 ${defaultFileName},是否修改文件名?`)) {
            // 如果用户选择"确定",则提示输入新的文件名
            let userFileName = window.prompt("请输入新的文件名:", defaultFileName);

            // 如果用户输入了新的文件名,则保存新的文件名
            if (userFileName && userFileName !== defaultFileName) {
                GM_setValue("customFileName", userFileName); // 保存用户设置的文件名
                defaultFileName = userFileName;
            }
        }

        // 保存 JSON 文件
        saveJSONToFile(data, defaultFileName);
    }

    // 保存 JSON 数据到本地文件
    function saveJSONToFile(data, fileName) {
        // 创建一个 Blob 对象
        const jsonBlob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });

        // 创建一个临时的 URL
        const url = URL.createObjectURL(jsonBlob);

        // 创建一个下载链接
        const link = document.createElement('a');
        link.href = url;
        link.download = fileName; // 设置下载文件名

        // 自动点击链接进行下载
        link.click();

        // 释放 URL 对象
        URL.revokeObjectURL(url);
    }
})();

QingJ © 2025

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