直接上效果图:
下面是代码:
代码
// ==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();
})();