Young People in Yaohuo

青少年模式

Από την 14/11/2022. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Young People in Yaohuo
// @description  青少年模式
// @version      0.6
// @author       Polygon
// @match        https://yaohuo.me/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        GM_addStyle
// @grant        GM_info
// @run-at       document-end
// @namespace https://greasyfork.org/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 = (k) => {
        if (localStorage.getItem("record-version") != GM_info.script.version) {
            localStorage.removeItem("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(k)) {
            record[k] = {"latest_time": null, "count": 0}
        }
        return record
    }
    // 指定k,添加一个时间戳
    let recordAdd = (k) => {
        let record
        record = getRecord(k)
        if (record[k]["latest_time"] - (new Date()).valueOf() < 1e3) return 
        record[k]["latest_time"] = (new Date()).valueOf()
        record[k]["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 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((e) => {
            e.addEventListener("click", (e) => {
                recordAdd(k)
            })
        })
        // 添加搜索按钮监听
        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.querySelectorAll("#KL_show_next_list").length == 0) {
            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((e) => {
            document.querySelector("#KL_show_next_list").appendChild(e.cloneNode(true))
            e.remove()
        })
        let filterList = (mutations, observer) => {
            let items = []
            document.querySelectorAll("#KL_show_next_list>div").forEach((e) => {
                // 每个节点有很多元素
                let text, info, time
                [text, info, time] = e.innerText.split("\n")
                let username = info.split("/")[0]
                if (validate(text) || usernames.includes(username)) {
                    // 这是被过滤掉的e,需要移除
                    console.log(`remove ${text} - ${username}`)
                    e.remove()
                } else {
                    // 这是保留下来的e,需要判断浏览次数
                    let k = e.querySelector("a").href.split("?")[0]
                    e.querySelector("a").addEventListener("click", (e) => {
                        recordAdd(k)
                    })
                    let record = getRecord(k)[k]
                    if (record["count"] > 0) {
                        e.style.position = "relative"
                        // 有浏览记录才处理
                        e.querySelectorAll("#record").forEach(e=>e.remove())
                        e.innerHTML += `
                            <div id="record" style="
                                position: absolute;
                                right: 8px;
                                top: 8px;
                                color: #999;
                                font-size: 10px;
                            ">
                                <div style="
                                    display: inline-block;
                                    text-align: center;
                                    line-height: 15px;
                                    width: 15px;
                                    height: 15px;
                                    background-color: #FF6464;
                                    border-radius: 50%;
                                ">
                                <span style="font-size: 12px; color: white;">${record["count"]}</span>
                                </div>
                                <span>${getTimeInfo(record["latest_time"])}</span>
                            </div>
                        `
                        e.querySelector("a").addEventListener("click", (e) => {
                            recordAdd(k)
                        })
                    }
                }
            })
        }
        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)
    }
})();