Greasy Fork is available in English.

Bilibili JSON Extractor with Auto-Cookie

Extracts JSON data from a Bilibili request starting with 'list?media_id=' and automatically includes site cookies, shows button only on Favorites page

// ==UserScript==
// @name         Bilibili JSON Extractor with Auto-Cookie
// @namespace    https://space.bilibili.com/398910090
// @version      1.0
// @description  Extracts JSON data from a Bilibili request starting with 'list?media_id=' and automatically includes site cookies, shows button only on Favorites page
// @author       You
// @match        https://www.bilibili.com/*
// @match        https://space.bilibili.com/*/favlist*
// @grant        none
// ==/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) {
        const addButton = document.createElement('button');
        addButton.textContent = '获取当前界面的json';
        addButton.style.position = 'fixed';
        addButton.style.top = '50px';
        addButton.style.right = '10px';
        addButton.style.zIndex = '9999';

        document.body.appendChild(addButton);

        // 点击按钮时执行的函数
        addButton.addEventListener('click', 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 数据
                const jsonPage = window.open();
                jsonPage.document.write('<pre>' + JSON.stringify(data, null, 2) + '</pre>');
            })
            .catch(error => {
                console.error('Error fetching JSON:', error);
            });
        });
    } else {
        // 隐藏按钮(在非收藏夹界面)
        const hideButton = () => {
            addButton.style.display = 'none';
        };

        // 执行隐藏按钮的函数
        hideButton();
    }
})();