将相同报价的单子合并到一起,如果中间有自己的单子,则从该处隔开
当前为
// ==UserScript==
// @name 更好的商店界面
// @namespace http://tampermonkey.net/
// @version 0.2
// @description 将相同报价的单子合并到一起,如果中间有自己的单子,则从该处隔开
// @author jxxzs
// @match https://www.milkywayidle.com/game*
// @grant none
// @run-at document-idle
// @require https://code.jquery.com/jquery-3.6.0.min.js
// ==/UserScript==
(async () => {
'use strict';
// 监听 DOM 中某个元素出现,触发回调
function OnElementAppear(selector, callback) {
// 监听整个 DOM
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (!(node instanceof HTMLElement)) return;
// 如果新节点本身匹配
if ($(node).is(selector)) {
requestAnimationFrame(() => callback($(node)));
}
// 如果新节点里面包含匹配的元素
const $matches = $(node).find(selector);
if ($matches.length) {
$matches.each((_, el) => {
requestAnimationFrame(() => callback($(el)));
});
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
}
// 处理 panel
// 把 "1.5K" "2M" "9999K" 转换成实际数字
function ParseFormattedNumber(str) {
str = str.trim().toUpperCase();
const units = { "K": 1e3, "M": 1e6, "B": 1e9 };
const match = str.match(/^([\d\.]+)([KMB]?)$/);
if (!match) return parseFloat(str) || 0;
let value = parseFloat(match[1]);
let unit = match[2];
if (unit && units[unit]) {
value *= units[unit];
}
return value;
}
// 把实际数字转成 "1.0K" "2.3M" "9.9B"
function FormatNumberShort(num) {
if (num >= 1e9) {
let val = num / 1e9;
return val >= 10 ? Math.round(val) + "B" : val.toFixed(1) + "B";
}
if (num >= 1e6) {
let val = num / 1e6;
return val >= 10 ? Math.round(val) + "M" : val.toFixed(1) + "M";
}
if (num >= 1e3) {
let val = num / 1e3;
return val >= 10 ? Math.round(val) + "K" : val.toFixed(1) + "K";
}
return String(num);
}
// 处理面板
function HandlePanel($panel) {
let lastPrice = null;
let lastRow = null;
$panel.find('tr').each(function () {
const $tr = $(this);
const $tds = $tr.find('td');
// 第 1 个 td → count
let countText = $tds.eq(0).clone().children().remove().end().text().trim();
let count = ParseFormattedNumber(countText);
// 判断是否是 "我的订单"
const mineText = $tds.eq(0).find('div.MarketplacePanel_mine__3aG9I').text().trim();
const isMine = mineText.length > 0;
// 第 2 个 td → price
let priceText = $tds.eq(1).find('span').first().text().trim();
let price = ParseFormattedNumber(priceText);
if (!isMine && lastPrice !== null && price === lastPrice) {
// 合并逻辑:累加到上一行 count
let prevText = lastRow.find('td').eq(0).clone().children().remove().end().text().trim();
let prevCount = ParseFormattedNumber(prevText);
let newCount = prevCount + count;
lastRow.find('td').eq(0).contents().filter(function () {
return this.nodeType === 3; // 只修改纯文本
}).first().replaceWith(FormatNumberShort(newCount));
// 删除当前行
$tr.remove();
} else {
// 不合并,更新参考行
lastPrice = price;
lastRow = $tr;
if (isMine) {
lastPrice = null;
lastRow = null;
}
}
});
}
// 主逻辑
(async () => {
// 添加出现回调函数
OnElementAppear('div.MarketplacePanel_orderBooksContainer__B4YE-', () => {
const $panels = $('table.MarketplacePanel_orderBookTable__3zzrv');
HandlePanel($panels.eq(0));
HandlePanel($panels.eq(1));
});
})();
})();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址