OK了,效果:
// ==UserScript==
// @name Level 3 我来了
// @namespace http://tampermonkey.net/
// @version 2024-03-08
// @description try to take over the world!
// @author wen
// @match https://linux.do/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=linux.do
// @grant window.onurlchange
// @require https://scriptcat.org/lib/513/2.0.0/ElementGetter.js
// ==/UserScript==
(function() {
'use strict';
// 定义需要达到的各项统计指标的阈值
const need_days_visited = 50;
const need_topics_read = 500;
const need_likes_given = 30;
const need_likes_received = 20;
const need_posts_read = 10;
// 获取过去30天的帖子数量
async function get_about(){
let response = await fetch(`https://linux.do/about.json`, {
"headers": {
"Accept": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
},
"method": "Get",
});
let data = await response.json();
return data.about.stats.posts_30_days;
};
// 更新指定元素的内容和颜色
// 如果元素的原始值低于阈值,那么元素的颜色将被设置为红色
// 如果元素的原始值达到或超过阈值,那么元素的颜色将被设置为绿色
function updateDiv(div, need_value) {
if (div.getAttribute("title") != null) {
const titleValue = parseInt(div.getAttribute("title").replace(",", ""));
div.innerHTML = titleValue + " / " + need_value;
div.style.color = titleValue < need_value ? "red" : "green";
} else {
const innerHTMLValue = parseInt(div.innerHTML.replace(",", ""));
div.setAttribute("title", innerHTMLValue);
div.innerHTML = innerHTMLValue + " / " + need_value;
div.style.color = innerHTMLValue < need_value ? "red" : "green";
}
}
// 添加对应于等级3的条件
function add_lv3_conditions(posts_30_days){
// 更新访问天数
elmGetter.get('li.stats-days-visited > div > span > span').then(div => {
updateDiv(div, need_days_visited);
});
// 更新已读帖子数
elmGetter.get('li.stats-posts-read > div > span > span').then(div => {
updateDiv(div, Math.min(parseInt(parseInt(posts_30_days) / 4), 20000));
});
// 更新浏览的话题数
elmGetter.get('li.stats-topics-entered > div > span > span').then(div => {
updateDiv(div, need_topics_read);
});
// 更新已送出的喜欢数
elmGetter.get('li.stats-likes-given > a > div > span > span').then(div => {
updateDiv(div, need_likes_given);
});
// 更新已收到的喜欢数
elmGetter.get('li.stats-likes-received > div > span > span').then(div => {
updateDiv(div, need_likes_received);
});
// 更新创建的帖子数
elmGetter.get('li.stats-post-count > a > div > span > span').then(div => {
updateDiv(div, need_posts_read);
});
}
// 获取过去30天的帖子数量,然后添加等级3的条件
get_about().then((posts_30_days)=>{
if (window.onurlchange === null) {
// 如果URL变化,那么更新等级3的条件
window.addEventListener('urlchange', (info) => {
var regex = /^https:\/\/linux\.do\/u\/[^\/]+\/summary$/;
if(regex.test(info.url)){
add_lv3_conditions(posts_30_days);
}
});
};
// 更新等级3的条件
add_lv3_conditions(posts_30_days);
});
})();