Greasy Fork is available in English.

b站ip开盒

显示b站评论的ip地址

// ==UserScript==
// @name         b站ip开盒
// @namespace    http://quhou-ip-kaihe.top/
// @version      1.0.2
// @description  显示b站评论的ip地址
// @author       You
// @match        https://www.bilibili.com/video/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @license      MIT
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';
    console.log("inject!,如果脚本失效请联系作者!")
    function handleIP(data){
        return data.map(r=>{
            r.member.uname += ` (${getIPText(r)})  `
            r.replies.map(r=>{
                r.member.uname += ` (${getIPText(r)})  `
                return r
            })
            return r
        })
    }
    function handleTopIP(data){
        data.member.uname += ` (${getIPText(data)})  `
        data.replies.map(r=>{
            r.member.uname += ` (${getIPText(r)})  `
            return r
        })
        return data
    }
    function handleTopRepliesIP(data){
        return data.map(r=>{
            r.member.uname += ` (${getIPText(r)})  `
            r.replies.map(r=>{
                r.member.uname += ` (${getIPText(r)})  `
                return r
            })
           return r
        })
    }
    function getIPText(data){
      return data.reply_control.location || "IP属地:未显示"
    }
    const _fetch = window.fetch
    window.fetch = function(...params){
        if(params[0].includes("//api.bilibili.com/x/v2/reply/wbi/main")){
            return new Promise(async (reslove,reject)=>{
                _fetch(...params).then(async response=>{
                    try{
                        const originalData = await response.json();
                        const processedData = {
                            ...originalData,
                            processed: true,
                            timestamp: new Date().toISOString()
                        };
                        if(processedData.data.top && processedData.data.top.upper){
                            processedData.data.top.upper = handleTopIP(processedData.data.top.upper)
                            processedData.data.top_replies = handleTopRepliesIP(processedData.data.top_replies)
                        }
                        processedData.data.replies = handleIP(processedData.data.replies)

                        const newResponse = new Response(JSON.stringify(processedData), {
                            status: response.status,
                            statusText: response.statusText,
                            headers: {
                                'Content-Type': 'application/json',
                            }
                        });
                        return reslove(newResponse)
                    }catch(err){
                        return reject(err)
                    }
                })
            })
        }
        else if(params[0].includes("//api.bilibili.com/x/v2/reply/reply")){
            return new Promise(async (reslove,reject)=>{
                _fetch(...params).then(async response=>{
                    try{
                        const originalData = await response.json();
                        const processedData = {
                            ...originalData,
                            processed: true,
                            timestamp: new Date().toISOString()
                        };
                        processedData.data.replies = handleIP(processedData.data.replies)
                        const newResponse = new Response(JSON.stringify(processedData), {
                            status: response.status,
                            statusText: response.statusText,
                            headers: {
                                'Content-Type': 'application/json',
                            }
                        });
                        return reslove(newResponse)
                    }catch(err){
                        return reject(err)
                    }
                })
            })
        }
        else{
            return _fetch(...params)
        }

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