前情提要:突发奇想 让shared chat通过编写代码计算大善人榜数据总量
下载网页再离线统计号池数量也太慢了,苦一苦油猴哥,帮我们来个一键统计
效果:
代码:
// ==UserScript==
// @name 号池真实数量统计
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 自动统计contributors下的数量之和
// @author Your Name
// @match https://shared.oaifree.com/dashboard
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 创建按钮并添加到页面的 header 中,放在原有按钮的左侧
function addButton() {
const header = document.querySelector('.header'); // 找到 header 元素
if (header) {
const button = document.createElement('button');
button.textContent = '号池真实数量';
button.id = 'chat-btn'; // 使用和现有按钮相同的ID样式
button.className = 'login-btn'; // 应用相同的类
const existingButton = document.getElementById('chat-btn'); // 查找现有的按钮
if (existingButton) {
header.insertBefore(button, existingButton); // 在现有按钮前插入新按钮
} else {
header.appendChild(button); // 如果没有找到现有按钮,就添加到 header 最后
}
button.addEventListener('click', calculateTotal);
}
}
// 定义一个函数用于提取和计算所有数量
function calculateTotal() {
const numbers = document.querySelectorAll('#contributors .list-item span:nth-child(2)');
let sum = 0;
numbers.forEach(num => {
const value = parseInt(num.textContent.trim(), 10);
if (!isNaN(value)) {
sum += value;
}
});
alert(`大善人总贡献号池: ${sum}`);
}
// 页面加载完成后添加按钮
window.addEventListener('load', addButton);
})();