Created
May 22, 2026 01:53
-
-
Save YCF/45cd84409d32ff546c666d3f2a90f922 to your computer and use it in GitHub Desktop.
Tampermonkey - V2EX 修正银币余额为整数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name V2EX 修正银币余额为整数 | |
| // @namespace http://tampermonkey.net/ | |
| // @version 1.0 | |
| // @description 仅对 V2EX 生效,在 a.balance_area 内部精准定位银币图标,将小数强制转为整数。 | |
| // @author You | |
| // @match https://www.v2ex.com/* | |
| // @match https://v2ex.com/* | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| 'use strict'; | |
| function fixSilverBalance() { | |
| // 1. 缩小范围:在 a.balance_area 内部查找 alt="S" 的图片 | |
| // V2EX 的结构通常是:<a class="balance_area"> ... <img src="..." alt="S"> 30.5 </a> | |
| const silverImg = document.querySelector('a.balance_area img[alt="S"]'); | |
| if (silverImg) { | |
| // 2. 获取该图片后面的文本节点 (也就是那个数字) | |
| const textNode = silverImg.nextSibling; | |
| if (textNode && textNode.nodeType === Node.TEXT_NODE) { | |
| let currentText = textNode.textContent.trim(); | |
| // 3. 只有包含小数点的时候才处理,避免不必要的操作 | |
| if (currentText.includes('.')) { | |
| let num = parseFloat(currentText); | |
| if (!isNaN(num)) { | |
| // 4. 向下取整 | |
| let intNum = Math.floor(num); | |
| // 5. 更新文本内容 | |
| // 注意:这里直接赋值,保留了原本的空格结构 | |
| // textNode.textContent 会自动更新视图 | |
| textNode.textContent = ` ${intNum} `; | |
| console.log(`✅ V2EX 银币修正: ${num} -> ${intNum}`); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| // --- 启动策略 --- | |
| // 1. 页面加载完成后立即执行 | |
| window.addEventListener('load', fixSilverBalance); | |
| // 2. 针对 V2EX 的 AJAX 特性进行监听 | |
| // V2EX 点击链接通常是无刷新跳转,所以需要监听 DOM 变化 | |
| const observer = new MutationObserver(function(mutations) { | |
| // 简单防抖:如果已经有定时器在等,就先清掉 | |
| if (window.v2exBalanceTimer) { | |
| clearTimeout(window.v2exBalanceTimer); | |
| } | |
| // 延迟 100ms 执行,确保页面数据已经渲染完毕 | |
| window.v2exBalanceTimer = setTimeout(fixSilverBalance, 100); | |
| }); | |
| observer.observe(document.body, { | |
| childList: true, | |
| subtree: true, | |
| characterData: true | |
| }); | |
| // 3. 额外保险:监听页面 visibilitychange | |
| // 防止你切换标签页回来后数据没更新 | |
| document.addEventListener('visibilitychange', () => { | |
| if (!document.hidden) { | |
| setTimeout(fixSilverBalance, 500); | |
| } | |
| }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment