显示众议页评论笔记

先这样再那样,就能看到了

目前為 2023-07-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name         显示众议页评论笔记
// @namespace    http://tampermonkey.net/
// @version      0.3.2
// @description  先这样再那样,就能看到了
// @author       em233333
// @match        https://www.bilibili.com/judgement/case-detail/*
// @match        https://www.bilibili.com/judgement/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @require	     https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @grant        none
// @license      MIT
// ==/UserScript==

// 匹配文本中是否包含{}
const regex = /\{.+?\}/

setInterval(() => {
    if ($('.btn_wrap').length == 0) {
        // 添加额外样式
        addStyle()
        // 完全自动化,但封ip风险高
        // getCaseInfo()
        // 需要手动点击查看
        // 异步执行,保证在评论内容加载后再执行
        setTimeout(() => getKeyComment(), 10)
    }
}, 3 * 1000)

// getAllRes()

function getAllRes() {
    // 保存原始的fetch函数
    const originalFetch = window.fetch;

    // 创建代理fetch函数
    function proxyFetch(url, options) {
        return originalFetch(url, options)
            .then(response => {
                // 在响应完成后处理数据
                response.clone().json().then(data => {
                    console.log(response.url, data);
                    getUsefulRes(response.url, data)
                })
            })
    }

    // 替换原始的fetch函数
    window.fetch = proxyFetch;
}



function getUsefulRes(url, data) {
    let current_caseId = null
    try {
        if (url.includes('https://api.bilibili.com/x/credit/v2/jury/case/info?case_id')) {
            const title = $('.case-detail .fjw-case-basic>.video>.b_fade').text()
            if (title.length) {
                if (data.code) throw data.message
                if (data.data.title == title) {
                    current_caseId = data.data.case_id
                } else throw '数据不适用于当前页'
            } else {
                const params = new URLSearchParams(new URL(url))
                current_caseId = params.get('case_id')
            }
        }
    } catch (err) {
        console.error('获取众议详情错误!', err);
    }
    console.log(current_caseId);
}



/**
 * 遍历页面中的评论内容,找出包含笔记的评论
 */
function getKeyComment() {
    $('.comment-item').get().forEach(e => {
        const content = $(e).find('.b_text').text()
        const matches = (content).match(regex)
        if (matches) {
            const id = (matches[0].split(':')[1].slice(0, -1))
            const btn_wrap = $(`<div class="btn_wrap"></div>`)
            const display_dyn_img = $('<span>查看图片</span>')
            const target_dyn = $(`<span onclick="window.open('https://t.bilibili.com/${id}', '_blank')">查看动态页面</span>`)
            btn_wrap.append(display_dyn_img)
            btn_wrap.append(target_dyn)
            display_dyn_img.on('click', () => {
                dynInfo(id)
                    .then(img_list => {
                        display_dyn_img.remove()
                        addImg(img_list, $(e))
                    })
            })
            $(e).append(btn_wrap)
        }
    })
}


/**
 * 通过api获取案件信息(如果请求过快会暂时失效)
 * @param {string} case_id 案件id
 * @returns {void} 
 */
function getCaseInfo(case_id = getId()) {
    fetch('https://api.bilibili.com/x/credit/v2/jury/case/info?case_id=' + case_id, {
        // 携带cookie
        credentials: 'include'
    })
        .then(res => res.json())
        .then(json => {
            if (json.code) throw json.message
            // 案件详情
            try {
                const case_info = json.data.case_info
                // 评论列表 | 单个评论
                displayCommentsNote(case_info.comments || [case_info.comment])
            } catch (err) {
                console.error(json);
                throw err
            }
        })
        .catch(err => console.error('获取案件信息时出错:', err))
}

function displayCommentsNote(comments_list) {
    comments_list.forEach(e => {
        const matches = (e.content).match(regex)
        try {
            if (matches) {
                let id = (matches[0].split(':')[1].slice(0, -1))
                dynInfo(id)
                    .then(imgs => {
                        addImg(imgs, $('.comment-item').eq(index))
                    })
            }
        } catch (err) {
            console.error(`处理"${e.content}"时出了问题`, e);
        }
    })
}

/**
 * 通过api获取动态(笔记)信息(请求过快会失效)
 * @param {string} id 动态id
 * @returns {Promise} 该动态的图片数组
 */
function dynInfo(id) {
    return new Promise(resolve => {
        fetch('https://api.bilibili.com/x/polymer/web-dynamic/v1/detail?id=' + id)
            .then(res => res.json())
            .then(json => {
                if (json.code) throw json.message
                try {
                    const imgs = json.data.item.modules.module_dynamic.major.draw.items;
                    let img_list = []
                    imgs.forEach(e => {
                        img_list.push(e.src)
                    })
                    resolve(img_list)
                } catch (err) {
                    console.error(json);
                    throw err
                }
            })
            .catch(err => console.error('获取笔记图片时出错:', err))
    })
}

function addStyle() {
    if (!$('.display_judgement_note_img').length) {
        let styleNode = $('<style class="display_judgement_note_img">')
        // 弹性布局下换行
        styleNode.append('.comment-item {flex-wrap: wrap} .dyn_img_wrap {order: 1; border-bottom: 1px solid #eee; margin-bottom: 1em}')
        styleNode.append('.dyn_img {width: 30% ! important;}')
        // 显示图片的开关
        styleNode.append('.btn_wrap {color: #999;font-size: 0.8em; white-space: nowrap; cursor: pointer; width: 96%; text-align: right;} .btn_wrap * {margin: 0 0.5em} .btn_wrap *:hover {background: #eee; color: #666}')
        $('head').append(styleNode)
    }
}

function addImg(img_list, item) {
    let wrap = $('<div class="dyn_img_wrap"></div>')
    img_list.forEach(e => {
        wrap.append(`<img class="dyn_img" src="${e}">`)
    })
    item.append(wrap)
}

/**
 * 通过拆分url获取页面(案件)id
 * @returns {string} 案件id
 */
function getId() {
    const pathArray = location.href.split("/");
    const lastPathWithQuery = pathArray[pathArray.length - 1];
    const id = lastPathWithQuery.split("?")[0];
    return id
}

QingJ © 2025

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