Young People in Yaohuo

青少年模式

目前为 2022-11-15 提交的版本。查看 最新版本

// ==UserScript==
// @name         Young People in Yaohuo
// @description  青少年模式
// @version      0.7
// @author       Polygon
// @match        https://yaohuo.me/*
// @icon         https://yaohuo.me/css/favicon.png
// @grant        GM_addStyle
// @grant        GM_info
// @run-at       document-end
// @namespace https://gf.qytechs.cn/users/788115
// ==/UserScript==

(function() {
    'use strict';
    // 添加一个style,PC端字体有点看不清
    GM_addStyle(`
        body, html {
            font-family: Arial, SimHei !important;
        }
    `)
    const keywords = [
        "小姐姐",
        "拼夕夕",
        "拼多多",
        "老车手",
        "老司机",
        "PDD",
        "女朋友",
        "戒撸"
    ]
    const usernames = [
        "张三"  // 示例,如果真有人叫张三别打我
    ]
    // 在record中读取k,读取过程中可能会初始化record[k]
    let getRecord = (itemid) => {
        if (localStorage.getItem("record-version") != GM_info.script.version) {
            localStorage.removeItem("record")
            alert("清空老版本record成功")
            localStorage.setItem("record-version", GM_info.script.version)
        }
        // 读取最新localStorage的record
        let record = localStorage.getItem("record")
        record = JSON.parse(record)
        if (record == null) {
            // 第一次初始化
            record = {}
        }
        if (!Object.keys(record).includes(itemid)) {
            record[itemid] = {
                "latest_time": null,  // 最近一次访问时间戳
                "latest_comment": 0,  // 上一次访问时的评论数量
                "latest_read": 0,  // 上一次访问时的阅读数量
                "count": 0,  // 访问次数
            }
        }
        return record
    }
    // 指定k,添加一个时间戳
    let recordAdd = (itemid, comment=null, read=null) => {
        let record
        record = getRecord(itemid)
        // 防止重复添加
        if (comment && read) {
            // 这俩应该在帖子打开后解析更新
            record[itemid]["latest_comment"] = comment
            record[itemid]["latest_read"] = read
        } else {
            // 没有这两个参数就只更新次数
            if (record[itemid]["latest_time"] && (new Date()).valueOf() - record[itemid]["latest_time"] < 1e3) return
            record[itemid]["latest_time"] = (new Date()).valueOf()
            record[itemid]["count"] ++
        }
        // 记录更新后record
        localStorage.setItem("record", JSON.stringify(record))
    }
    function timestampToTime(timestamp) {
        // https://www.byteblogs.com/article/259
        var date = new Date(timestamp)
        var Y = date.getFullYear() + '-'
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'
        var D = date.getDate() + ' '
        var h = date.getHours() + ':'
        var m = date.getMinutes() + ':'
        var s = date.getSeconds()
        return Y+M+D+h+m+s
    }
    let getTimeInfo = (t) => {
        let delta = ((new Date()).valueOf() - t) / 1000
        if (delta < 60) {
            // 小于60秒
            delta = `${parseInt(delta)}秒前`
        } else if (delta / (60 ** 2) < 1) {
            // 小于一小时
            delta = `${parseInt(delta / 60)}分前`
        } else if (delta / (60 ** 3) < 24) {
            // 小于一天
            delta = `${parseInt(delta / (60 ** 2))}时前`
        } else {
            // 直接显示日期
            delta = timestampToTime(t)
        }
        return delta
    }
    let parseElement = (ele) => {
        let itemid = ele.querySelector("a").href.match(/bbs-(\d+)/)[1]
        let [text, info, _] = ele.innerText.split("\n")
        let [username, comment, read] = info.split("/")
        comment = parseInt(comment.replace("回", ""))
        read = parseInt(read.replace("阅", ""))
        return [itemid, text, username, comment, read]
    }
    let validate = (text) => {
        return keywords.filter((keyword) => {
            return text.match(new RegExp(keyword, "gi"))
        }).length
    }
    let currentURL = window.location.href
    // 判断当前网址是否为主页
    if (currentURL == 'https://yaohuo.me/') {
        console.log("主页")
        // 主页移除关键词
        let items = []
        document.querySelectorAll('.list a').forEach((e) => {
            // 每个节点有两个关键信息:1.href;2.innerText
            let href = e.href
            let text = e.innerText
            if (validate(text)) {
                console.log(`remove ${text}`)
            } else {
                items.push(`${items.length+1}.<a href="${href}">${text}</a>`)
            }
        })
        document.querySelector('.list').innerHTML = items.join("<br>")
        // 添加监控点击
        document.querySelectorAll('.list>a').forEach((a) => {
            a.addEventListener("click", (e) => {
                let itemid = a.href.split("?")[0]
                recordAdd(itemid)
            })
        })
        // 添加搜索按钮监听
        document.querySelector("input[type=submit]").addEventListener("click", (e) => {
            let text = document.querySelector("input[type=text]").value
            console.log(text)
            if (validate(text)) {
                e.preventDefault()
                document.querySelector("input[type=text]").value = "请重新组织你的语言!"
                setTimeout(() => {
                    document.querySelector("input[type=text]").value = ""
                }, 1000);
            }
        });
    } else if (currentURL.startsWith('https://yaohuo.me/bbs/book_list.aspx?')) {
        // 可能是新帖,也可能是搜索页
        if (currentURL.includes('key=')) {
            console.log("搜索页")
            let searchText = currentURL.match(/key=(.+?)&/)[1]
            if (validate(searchText)) {
                setTimeout(() => {
                    alert("你可以遗忘屏蔽词,本脚本将永远不会!\nYou can forget about blocking words, this script will never!")
                }, 233);
            }
        } else {
            console.log("新帖页")
        }
        if (!document.querySelector("#KL_show_next_list")) {
            let div = document.createElement("div")
            div.setAttribute('id', 'KL_show_next_list')
            document.body.appendChild(div)
        }
        // 把body下的条目统一放到KL_show_next_list下,便于统一操作
        document.querySelector("#KL_show_next_list").style.display = ""
        document.querySelectorAll(".listdata").forEach((ele) => {
            document.querySelector("#KL_show_next_list").appendChild(ele.cloneNode(true))
            ele.remove()
        })
        let filterList = (mutations, observer) => {
            document.querySelectorAll("#KL_show_next_list>div").forEach((ele) => {
                // 每个节点有很多元素
                let [itemid, text, username, comment, read] = parseElement(ele)
                if (validate(text) || usernames.includes(username)) {
                    // 这是被过滤掉的e,需要移除
                    console.log(`remove ${text} - ${username}`)
                    ele.remove()
                } else {
                    // 这是保留下来的ele,先绑定一个点击事件
                    ele.querySelector("a").addEventListener("click", function(e) { 
                        // 更新次数
                        recordAdd(itemid)
                    })
                    let record = getRecord(itemid)[itemid]
                    if (record["count"] > 0) {
                        // 有浏览记录才处理
                        ele.style.position = "relative"
                        let add_comment = comment - record["latest_comment"]
                        let add_read = read - record["latest_read"]
                        add_comment = (add_comment > 99) ? "⋯" : add_comment
                        add_read = (add_read > 99) ? "⋯" : add_read
                        if (ele.querySelector("#record")) {
                            ele.querySelector("#count>span").innerText = record["count"]
                            ele.querySelector("#time_info").innerText = getTimeInfo(record["latest_time"])
                            ele.querySelector("#add_comment>span").innerText = add_comment
                            ele.querySelector("#add_read>span").innerText = add_read
                            if (add_comment > 0) {
                                ele.querySelector("#add_comment").style.display = 'inline-block'
                            } else {
                                ele.querySelector("#add_comment").style.display = 'none'
                            }
                            if (add_read > 0) {
                                ele.querySelector("#add_read").style.display = 'inline-block'
                            } else {
                                ele.querySelector("#add_read").style.display = 'none'
                            }
                            
                        } else {
                            let div = document.createElement('div')
                            div.setAttribute("id", "record")
                            div.style = `
                                position: absolute;
                                right: 8px;
                                top: 8px;
                                color: #999;
                                font-size: 10px;
                            `
                            div.innerHTML = `
                                <div id="count" style="
                                    display: inline-block;
                                    text-align: center;
                                    line-height: 15px;
                                    width: 15px;
                                    height: 15px;
                                    background-color: #FF7D7D;
                                    border-radius: 50%;
                                ">
                                    <span style="font-size: 12px; color: white;">${record["count"]}</span>
                                </div>
                                <div id="add_comment" style="
                                    display: inline-block;
                                    text-align: center;
                                    line-height: 15px;
                                    width: 15px;
                                    height: 15px;
                                    background-color: #54BAB9;
                                    border-radius: 50%;
                                    display: ${(add_comment==0) ? 'none' : ''};
                                ">
                                    <span style="font-size: 12px; color: white;">${add_comment}</span>
                                </div>
                                <div id="add_read" style="
                                    display: inline-block;
                                    text-align: center;
                                    line-height: 15px;
                                    width: 15px;
                                    height: 15px;
                                    background-color: #827397;
                                    border-radius: 50%;
                                    display: ${(add_read==0) ? 'none' : ''};
                                ">
                                    <span style="font-size: 12px; color: white;">${add_read}</span>
                                </div>
                                <div style="
                                    width: 50px;
                                    display: inline-block;
                                    text-align: right;
                                ">
                                <span id="time_info">${getTimeInfo(record["latest_time"])}</span>
                                </div>
                            `
                            ele.appendChild(div)
                        }
                    }
                }
            })
        }
        filterList(null, null)
        var observer = new MutationObserver(filterList)
        var node = document.querySelector('#KL_show_tip')
        if (node) {
            observer.observe(node, {childList: true})
        }
        setInterval(() => {
            // 5s一次更新时间
            filterList(null, null)
        }, 1e3)
    } else if (currentURL.search(/bbs-\d+/) != -1) { 
        setTimeout(() => {            
            let itemid = currentURL.match(/bbs-(\d+)/)[1]
            try {
                let comment = parseInt(document.querySelector(".noafter").innerText.match(/(\d+)/)[1])
            } catch {
                let comment = 0
            }
            let read = parseInt(document.querySelector(".content").innerText.match(/阅(\d+)/)[1])
            recordAdd(itemid, comment, read)
            console.log(itemid, comment, read)
        }, 233);
    }
})();

QingJ © 2025

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