【油猴脚本】新功能,水神封号查询

直接上效果图:

image

下面是代码:

代码
// ==UserScript==
// @name         水神封号查询
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  在指定位置添加“查询水指数”按钮和显示水指数的元素,并根据水指数分配封号
// @author       nulluser
// @icon         https://cdn.linux.do/uploads/default/optimized/3X/7/d/7de31932a4fd533496cfe35979a4d9d995bb5c63_2_180x180.png
// @match        https://linux.do/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const days = 15

    // Function to fetch water index data
    async function fetchWaterIndex(username) {
        const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        const userActionUrl = `https://linux.do/user_actions.json?offset=0&limit=5000&username=${username}&filter=4,5`;
        const fetchOptions = {
            headers: {
                "accept": "application/json, text/javascript, */*; q=0.01",
                "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
                "discourse-logged-in": "true",
                "discourse-present": "true",
                "x-csrf-token": csrfToken,
                "x-requested-with": "XMLHttpRequest"
            },
            method: "GET",
            mode: "cors",
            credentials: "include"
        };

        try {
            const response = await fetch(userActionUrl, fetchOptions);
            if (!response.ok) {
                throw new Error('Network response was not ok.');
            }
            const data = await response.json();
            const waterIndex = countRecentEntries(data.user_actions, days);
            return waterIndex;
        } catch (error) {
            console.error('Error fetching water index:', error);
            return null;
        }
    }

    // Function to count recent entries within a specified number of days
    function countRecentEntries(actions, days) {
        const now = new Date();
        const fifteenDaysAgo = new Date(now.setDate(now.getDate() - days));
        let count = 0;

        actions.forEach(entry => {
            const entryDate = new Date(entry.created_at);
            if (entryDate >= fifteenDaysAgo) {
                count++;
            }
        });

        return count;
    }

    // Function to assign title based on water index
    function assignTitle(waterIndex) {
        const fh_list = ["干巴巴", "水滴", "小溪", "江河", "激流", "洪水"];
        const water_value = waterIndex / (15*days);

        if (water_value > 2) {
            return "碧波潭龙王的女婿";
        } else if (water_value > 1.5 && water_value <= 2) {
            return fh_list[5];
        } else if (water_value > 1.1 && water_value <= 1.5) {
            return fh_list[4];
        } else if (water_value > 0.9 && water_value <= 1.1) {
            return fh_list[3];
        } else if (water_value > 0.5 && water_value <= 0.9) {
            return fh_list[2];
        } else if (water_value > 0.1 && water_value <= 0.5) {
            return fh_list[1];
        } else if (water_value <= 0.1) {
            return fh_list[0];
        } else {
            return "未知";
        }
    }

    // Function to add button and display elements
    function addButtonAndDisplay() {
        const targetElement = document.querySelector('.location-and-website');

        if (targetElement && !document.getElementById('waterIndexButton')) {
            const username_href = targetElement.parentElement.parentElement.getElementsByClassName("card-huge-avatar")[0].href.split("/");
            const username = username_href[username_href.length-1];

            // Create button
            const button = document.createElement('button');
            button.id = 'waterIndexButton';
            button.innerText = '水神封号';
            button.style.marginLeft = '10px';
            button.style.color = 'gray';
            button.style.cursor = 'pointer';
            button.addEventListener('click', async () => {
                const waterIndex = await fetchWaterIndex(username);
                const title = assignTitle(waterIndex);
                display.innerText = (waterIndex !== null) ? `${title} 指数: ${waterIndex}` : '获取失败';
            });

            // Create display span
            const display = document.createElement('span');
            display.id = 'waterIndexDisplay';
            display.style.marginLeft = '10px';
            display.style.fontWeight = 'bold';

            // Append button and display to target element
            targetElement.appendChild(button);
            targetElement.appendChild(display);
        }
    }

    // MutationObserver to detect changes in the DOM and call addButtonAndDisplay function
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes) {
                addButtonAndDisplay();
            }
        });
    });

    // Start observing the document body for changes
    observer.observe(document.body, { childList: true, subtree: true });

    // Initial call in case the element is already present
    addButtonAndDisplay();
})();
18 个赞

支持

不过碧波潭龙王的女婿是谁

13 个赞

大水哥 的确 配得上这个称号 碧波潭龙王的女婿

5 个赞

水神猫猫头

2 个赞

我觉得我要把我的查询插件接入这个

5 个赞

九头虫 万圣公主的老公

西海龙王三太子(唐僧的坐骑)的情敌

3 个赞

水神蛤蟆猫头男

1 个赞

亮点:
image


又是一个零头都没大帅哥多的指数

不是自动出啊,还需要点一下……装上还以为不能用……

:tieba_018:

拉取的数据量比较大,如果自动获取会对服务器产生负载。

按需点击。

2 个赞

水神 :+1:

顶你上去

知道原因,我只是感叹一下

:tieba_002:

1 个赞

可以的,很严谨

1 个赞

是不是应该再细化一下,我这500多分的怎么好意思跟大帅哥近4000分的一个级别……

1 个赞

最近15天,日均超过30条发帖,就算这个女婿级别了

3 个赞

你确定有考虑大帅哥这种反物质灌水机么……

:rofl:

1 个赞

大水哥实力强悍

1 个赞

好玩

1 个赞