模组树游戏修改,油猴脚本
本文作者为 @lyc。
// ==UserScript==// @name 树修改// @namespace http://tampermonkey.net/// @version 1.5// @description 通用 TMT 树修改:改层级数值、微调、里程碑入栈(按实际 key 对齐,记住上次选择)// @match *...
// ==UserScript==
// @name 树修改
// @namespace http://tampermonkey.net/
// @version 1.5
// @description 通用 TMT 树修改:改层级数值、微调、里程碑入栈(按实际 key 对齐,记住上次选择)
// @match *://*/*
// @grant unsafeWindow
// @grant GM_addStyle
// @run-at document-idle
// ==/UserScript==
(function () {
'use strict';
const W = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window;
const WARN_KEY = 'tmt_tree_editor_warned_v1';
const MEM_KEY = 'tmt_tree_editor_memory_v1';
function waitForTMT(cb, tries = 0) {
if (typeof W.player !== 'undefined' && typeof W.layers !== 'undefined') return cb();
if (tries > 100) return console.warn('[树修改] 没检测到 player/layers');
setTimeout(() => waitForTMT(cb, tries + 1), 200);
}
// 记忆读写
function loadMemory() {
try {
const raw = localStorage.getItem(MEM_KEY);
if (!raw) return {};
return JSON.parse(raw) || {};
} catch (e) {
return {};
}
}
function saveMemory(obj) {
try {
localStorage.setItem(MEM_KEY, JSON.stringify(obj));
} catch (e) {}
}
GM_addStyle(`
#tmt-ball-container {
position: fixed; left: 0; top: 50%; transform: translateY(-50%);
z-index: 100000; font-family: monospace; user-select: none;
display: flex; flex-direction: column; align-items: flex-start;
gap: 6px; transition: left .3s;
}
#tmt-ball-container:hover { left: 6px; }
#tmt-ball-main {
width: 46px; height: 46px; border-radius: 50%;
background: rgba(30, 120, 220, 0.85); color: #fff;
font-size: 12px; font-weight: bold;
display: flex; align-items: center; justify-content: center;
cursor: pointer; box-shadow: -3px 4px 12px -5px black;
transition: all .3s; opacity: .6;
}
#tmt-ball-container:hover #tmt-ball-main { opacity: .9; }
#tmt-ball-main:hover { background: rgba(50, 150, 255, 0.95); }
.tmt-ball-menu {
display: flex; flex-direction: column; gap: 5px;
max-height: 0; overflow: hidden; transition: max-height .35s ease;
}
#tmt-ball-container:hover .tmt-ball-menu { max-height: 200px; }
.tmt-ball-item {
width: 40px; height: 40px; border-radius: 50%;
background: rgba(30, 120, 220, 0.6); color: #fff; font-size: 11px;
display: flex; align-items: center; justify-content: center;
cursor: pointer; box-shadow: -3px 4px 12px -5px black;
opacity: 0; transform: translateX(-10px); transition: all .3s;
}
#tmt-ball-container:hover .tmt-ball-item { opacity: .7; transform: translateX(0); }
.tmt-ball-item:hover { opacity: 1 !important; background: rgba(50, 150, 255, 0.9); }
#tmt-edit-panel {
position: fixed; left: 60px; top: 50%; transform: translateY(-50%);
z-index: 100001; background: rgba(20, 20, 30, 0.96); color: #ddd;
font-family: monospace; font-size: 12px; border-radius: 8px; padding: 12px;
width: 300px; box-shadow: 0 4px 20px rgba(0,0,0,0.6); display: none;
}
#tmt-edit-panel.open { display: block; }
#tmt-edit-panel h3 { margin: 0 0 8px; font-size: 13px; color: #6cf; }
#tmt-edit-panel h4 { margin: 8px 0 4px; font-size: 12px; color: #9cf; }
#tmt-edit-panel .row { display: flex; gap: 6px; margin-bottom: 8px; }
#tmt-edit-panel button {
flex: 1; padding: 6px; border: none; border-radius: 4px;
background: rgba(80, 140, 220, 0.6); color: #fff; cursor: pointer; font-size: 12px;
transition: all .2s;
}
#tmt-edit-panel button:hover:not(:disabled) { background: rgba(80, 140, 220, 0.9); }
#tmt-edit-panel button:disabled {
background: rgba(80, 80, 80, 0.4); color: #666; cursor: not-allowed;
}
#tmt-edit-panel button.active {
background: rgba(80, 200, 120, 0.85);
color: #fff;
box-shadow: 0 0 0 2px rgba(80, 200, 120, 0.5);
}
#tmt-edit-panel select,
#tmt-edit-panel input[type="text"] {
width: 100%; padding: 5px; border-radius: 4px; border: 1px solid #456;
background: #1a1a28; color: #ddd; font-family: monospace; font-size: 12px;
box-sizing: border-box; margin-bottom: 8px;
}
#tmt-edit-panel .hint { color: #888; font-size: 10px; margin-bottom: 6px; }
#tmt-edit-panel .close {
position: absolute; right: 8px; top: 6px; cursor: pointer;
color: #888; font-size: 14px;
}
#tmt-edit-panel .close:hover { color: #fff; }
#tmt-edit-panel .fine-grid {
display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; margin-bottom: 8px;
}
#tmt-edit-panel .fine-grid label {
font-size: 10px; color: #9cf; display: block; margin-bottom: 2px;
}
#tmt-edit-panel .fine-grid input { margin-bottom: 0; }
#tmt-edit-panel .checkbox-row {
display: flex; align-items: center; gap: 6px; margin-bottom: 8px;
font-size: 11px; color: #9cf;
}
#tmt-edit-panel .checkbox-row input { width: auto; margin: 0; }
#tmt-edit-panel .milestone-list {
max-height: 100px; overflow-y: auto; background: #1a1a28; border: 1px solid #456;
border-radius: 4px; padding: 6px; margin-bottom: 8px; font-size: 11px; color: #9cf;
white-space: pre-wrap; word-break: break-all;
}
`);
function createBall() {
if (document.getElementById('tmt-ball-container')) return;
const container = document.createElement('div');
container.id = 'tmt-ball-container';
container.innerHTML = `
<div class="tmt-ball-menu">
<div class="tmt-ball-item" data-act="info" title="查看 player">i</div>
<div class="tmt-ball-item" data-act="edit" title="修改">改</div>
</div>
<div id="tmt-ball-main">树修改</div>
`;
document.body.appendChild(container);
const panel = document.createElement('div');
panel.id = 'tmt-edit-panel';
panel.innerHTML = `
<span class="close" title="关闭">×</span>
<h3>树修改</h3>
<div class="hint">选择要操作的对象:</div>
<div class="row">
<button data-mode="points" class="active">数值</button>
<button data-mode="milestone">里程碑</button>
</div>
<div id="tmt-points-mode">
<div class="hint">选择层级(含 player.points 总数值):</div>
<select id="tmt-layer-select-points"></select>
<h4>模式</h4>
<div class="row">
<button id="tmt-mode-normal" class="active">普通(输入数值)</button>
<button id="tmt-mode-fine">微调(sign/layer/mag)</button>
</div>
<div id="tmt-normal-mode">
<div class="hint">输入新数值:</div>
<input id="tmt-value-input" placeholder="如 1e100" />
</div>
<div id="tmt-fine-mode" style="display:none">
<div class="hint">分别调整 sign / layer / mag(已填入当前值):</div>
<div class="fine-grid">
<div><label>sign</label><input id="tmt-sign-input" placeholder="1" /></div>
<div><label>layer</label><input id="tmt-layer-input" placeholder="0" /></div>
<div><label>mag</label><input id="tmt-mag-input" placeholder="1e15" /></div>
</div>
</div>
</div>
<div id="tmt-milestone-mode" style="display:none">
<div class="hint">选择层级(有 milestones 的):</div>
<select id="tmt-layer-select-milestone"></select>
<div class="hint">当前 <span id="tmt-milestone-layer-name">?</span>.milestones:</div>
<div class="milestone-list" id="tmt-milestone-list">(加载中)</div>
<div class="hint">选择要解锁到的里程碑(按实际 key 列出):</div>
<select id="tmt-milestone-select" multiple size="6"></select>
<div class="hint">或输入自定义数字(逗号分隔,只接受存在的 key):</div>
<input id="tmt-milestone-input" placeholder="如 3,5,7" />
<div class="checkbox-row">
<input type="checkbox" id="tmt-milestone-fill" checked />
<label for="tmt-milestone-fill">自动补全前面的里程碑</label>
</div>
</div>
<div class="checkbox-row">
<input type="checkbox" id="tmt-keep-open" />
<label for="tmt-keep-open">改完后保持面板打开</label>
</div>
<div class="checkbox-row">
<input type="checkbox" id="tmt-show-alert" />
<label for="tmt-show-alert">改完后弹出提示</label>
</div>
<div class="row">
<button id="tmt-apply">应用</button>
</div>
`;
document.body.appendChild(panel);
const pointsLayerSelect = panel.querySelector('#tmt-layer-select-points');
const milestoneLayerSelect = panel.querySelector('#tmt-layer-select-milestone');
const milestoneLayerName = panel.querySelector('#tmt-milestone-layer-name');
const valueInput = panel.querySelector('#tmt-value-input');
const signInput = panel.querySelector('#tmt-sign-input');
const layerInput = panel.querySelector('#tmt-layer-input');
const magInput = panel.querySelector('#tmt-mag-input');
const normalMode = panel.querySelector('#tmt-normal-mode');
const fineMode = panel.querySelector('#tmt-fine-mode');
const fineBtn = panel.querySelector('#tmt-mode-fine');
const normalBtn = panel.querySelector('#tmt-mode-normal');
const keepOpen = panel.querySelector('#tmt-keep-open');
const showAlert = panel.querySelector('#tmt-show-alert');
const pointsBtn = panel.querySelector('button[data-mode="points"]');
const milestoneBtn = panel.querySelector('button[data-mode="milestone"]');
const pointsModeEl = panel.querySelector('#tmt-points-mode');
const milestoneModeEl = panel.querySelector('#tmt-milestone-mode');
const milestoneList = panel.querySelector('#tmt-milestone-list');
const milestoneSelect = panel.querySelector('#tmt-milestone-select');
const milestoneInput = panel.querySelector('#tmt-milestone-input');
const milestoneFill = panel.querySelector('#tmt-milestone-fill');
let usePoints = 'points';
let mode = 'normal';
let currentPointsLayer = null;
let currentMilestoneLayer = null;
// 加载记忆
let memory = loadMemory();
if (memory.usePoints) usePoints = memory.usePoints;
if (memory.mode) mode = memory.mode;
if (memory.pointsLayer) currentPointsLayer = memory.pointsLayer;
if (memory.milestoneLayer) currentMilestoneLayer = memory.milestoneLayer;
if (typeof memory.milestoneFill === 'boolean') milestoneFill.checked = memory.milestoneFill;
if (typeof memory.keepOpen === 'boolean') keepOpen.checked = memory.keepOpen;
if (typeof memory.showAlert === 'boolean') showAlert.checked = memory.showAlert;
function persistMemory() {
memory = {
usePoints,
mode,
pointsLayer: currentPointsLayer,
milestoneLayer: currentMilestoneLayer,
milestoneFill: milestoneFill.checked,
keepOpen: keepOpen.checked,
showAlert: showAlert.checked
};
saveMemory(memory);
}
function getByPath(path) {
const parts = path.split('.');
let obj = W;
for (let i = 0; i < parts.length - 1; i++) {
obj = obj[parts[i]];
if (!obj) return undefined;
}
return obj[parts[parts.length - 1]];
}
function getCurrentPointsValue() {
if (!currentPointsLayer) return undefined;
if (currentPointsLayer === '__root__') return W.player?.points;
return W.player?.[currentPointsLayer]?.points;
}
function isFineTunable(v) {
if (!v || typeof v !== 'object') return false;
if (typeof v.layer !== 'undefined' || typeof v.mag !== 'undefined' || typeof v.sign !== 'undefined') return true;
if (v.array && Array.isArray(v.array) && v.sign !== undefined) return true;
return false;
}
function setActive(btn, active) {
if (active) btn.classList.add('active');
else btn.classList.remove('active');
}
function setPointsMode(val) {
usePoints = val;
setActive(pointsBtn, val === 'points');
setActive(milestoneBtn, val === 'milestone');
pointsModeEl.style.display = val === 'points' ? '' : 'none';
milestoneModeEl.style.display = val === 'milestone' ? '' : 'none';
if (val === 'points') {
fillPointsLayerSelect();
fillFineInputs();
} else {
fillMilestoneLayerSelect();
fillMilestoneList();
}
persistMemory();
}
function setMode(val) {
mode = val;
setActive(normalBtn, val === 'normal');
setActive(fineBtn, val === 'fine');
normalMode.style.display = val === 'normal' ? '' : 'none';
fineMode.style.display = val === 'fine' ? '' : 'none';
persistMemory();
}
function fillFineInputs() {
const v = getCurrentPointsValue();
if (isFineTunable(v)) {
signInput.value = v.sign;
layerInput.value = v.layer !== undefined ? v.layer : (v.array ? v.array[1] : 0);
magInput.value = v.mag !== undefined ? v.mag : (v.array ? v.array[0] : 0);
fineBtn.disabled = false;
fineBtn.style.opacity = '1';
fineBtn.title = '';
} else {
signInput.value = '';
layerInput.value = '';
magInput.value = '';
fineBtn.disabled = true;
fineBtn.style.opacity = '0.5';
fineBtn.title = '该字段不是 Decimal 格式,无法微调';
if (mode === 'fine') setMode('normal');
}
}
function fillPointsLayerSelect() {
pointsLayerSelect.innerHTML = '';
// 第一项:总 player.points
const optRoot = document.createElement('option');
optRoot.value = '__root__';
optRoot.textContent = 'player.points(总数值)';
pointsLayerSelect.appendChild(optRoot);
// 各层级 points
const keys = Object.keys(W.player).filter(k => {
const v = W.player[k];
return v && typeof v === 'object' && v.points !== undefined;
});
keys.forEach(k => {
const opt = document.createElement('option');
opt.value = k;
opt.textContent = k + '.points';
pointsLayerSelect.appendChild(opt);
});
// 恢复记忆或默认
const candidates = ['__root__', ...keys];
if (currentPointsLayer && candidates.includes(currentPointsLayer)) {
pointsLayerSelect.value = currentPointsLayer;
} else {
currentPointsLayer = '__root__';
pointsLayerSelect.value = '__root__';
}
}
function getMilestoneKeys(layerKey) {
const def = W.layers?.[layerKey]?.milestones;
if (!def) return [];
if (Array.isArray(def)) return def.map((_, i) => String(i));
if (typeof def === 'object') return Object.keys(def);
return [];
}
function getPlayerMilestones(layerKey) {
const p = W.player?.[layerKey];
if (!p) return null;
if (Array.isArray(p.milestones)) return p.milestones;
if (Array.isArray(p.milestone)) return p.milestone;
return null;
}
function fillMilestoneLayerSelect() {
const keys = Object.keys(W.layers || {}).filter(k => {
const def = W.layers[k];
return def && def.milestones && (Array.isArray(def.milestones) || typeof def.milestones === 'object');
});
milestoneLayerSelect.innerHTML = '';
keys.forEach(k => {
const opt = document.createElement('option');
opt.value = k;
opt.textContent = k + '(' + getMilestoneKeys(k).length + ' 个)';
milestoneLayerSelect.appendChild(opt);
});
if (keys.length) {
if (currentMilestoneLayer && keys.includes(currentMilestoneLayer)) {
milestoneLayerSelect.value = currentMilestoneLayer;
} else {
currentMilestoneLayer = keys.includes('s') ? 's' : keys[0];
milestoneLayerSelect.value = currentMilestoneLayer;
}
} else {
currentMilestoneLayer = null;
}
}
function fillMilestoneList() {
if (!currentMilestoneLayer) {
milestoneLayerName.textContent = '?';
milestoneList.textContent = '(没有层级定义 milestones)';
milestoneSelect.innerHTML = '';
return;
}
milestoneLayerName.textContent = currentMilestoneLayer;
const m = getPlayerMilestones(currentMilestoneLayer);
if (!m) {
milestoneList.textContent = '(player.' + currentMilestoneLayer + '.milestones 不存在)';
milestoneSelect.innerHTML = '';
return;
}
milestoneList.textContent = m.map((v, i) => i + ': ' + v).join('\n');
const keys = getMilestoneKeys(currentMilestoneLayer);
milestoneSelect.innerHTML = '';
keys.forEach(k => {
const opt = document.createElement('option');
opt.value = k;
opt.textContent = '里程碑 ' + k + (m.includes(k) ? '(已解锁)' : '');
if (m.includes(k)) opt.style.color = '#6c6';
milestoneSelect.appendChild(opt);
});
}
function openPanel() {
// 恢复记忆里的模式
setPointsMode(usePoints);
setMode(mode);
valueInput.value = '';
milestoneInput.value = '';
panel.classList.add('open');
}
function closePanel() { panel.classList.remove('open'); }
panel.querySelector('.close').addEventListener('click', closePanel);
pointsBtn.addEventListener('click', () => setPointsMode('points'));
milestoneBtn.addEventListener('click', () => setPointsMode('milestone'));
pointsLayerSelect.addEventListener('change', () => {
currentPointsLayer = pointsLayerSelect.value;
fillFineInputs();
persistMemory();
});
milestoneLayerSelect.addEventListener('change', () => {
currentMilestoneLayer = milestoneLayerSelect.value;
fillMilestoneList();
persistMemory();
});
normalBtn.addEventListener('click', () => setMode('normal'));
fineBtn.addEventListener('click', () => {
if (fineBtn.disabled) return;
setMode('fine');
fillFineInputs();
});
milestoneFill.addEventListener('change', persistMemory);
keepOpen.addEventListener('change', persistMemory);
showAlert.addEventListener('change', persistMemory);
function warnOnce() {
if (localStorage.getItem(WARN_KEY)) return true;
const ok = confirm('确定要修改吗?\n误触可能破坏存档,建议先导出备份。\n\n(此提示只显示一次)');
if (ok) localStorage.setItem(WARN_KEY, '1');
return ok;
}
function afterApply(msg) {
if (typeof W.updateTemp === 'function') W.updateTemp();
if (typeof W.save === 'function') W.save();
if (showAlert.checked) alert(msg);
if (!keepOpen.checked) closePanel();
}
panel.querySelector('#tmt-apply').addEventListener('click', () => {
if (!warnOnce()) return;
try {
if (usePoints === 'points') {
if (!currentPointsLayer) { alert('没有可用的 points 层级'); return; }
const path = currentPointsLayer === '__root__'
? 'player.points'
: 'player.' + currentPointsLayer + '.points';
const parts = path.split('.');
let obj = W;
for (let i = 0; i < parts.length - 1; i++) {
obj = obj[parts[i]];
if (!obj) throw new Error('路径无效:' + path);
}
const last = parts[parts.length - 1];
const oldVal = obj[last];
if (mode === 'normal') {
const val = valueInput.value.trim();
if (!val) { alert('请输入数值'); return; }
if (oldVal && typeof oldVal === 'object' && typeof oldVal.layer !== 'undefined') {
const Ctor = oldVal.constructor;
obj[last] = Ctor.fromString ? Ctor.fromString(val) : new Ctor(val);
} else if (oldVal && oldVal.array && Array.isArray(oldVal.array)) {
const num = parseFloat(val);
if (isNaN(num)) throw new Error('不是数字');
oldVal.array[0] = Math.log10(num);
} else {
if (typeof W.Decimal !== 'undefined') obj[last] = new W.Decimal(val);
else obj[last] = val;
}
} else {
if (!isFineTunable(oldVal)) { alert('该字段不是 Decimal 格式,无法微调'); return; }
const s = signInput.value.trim();
const l = layerInput.value.trim();
const m = magInput.value.trim();
if (s !== '') oldVal.sign = parseInt(s, 10) || 1;
if (l !== '') oldVal.layer = parseInt(l, 10) || 0;
if (m !== '') { const num = parseFloat(m); if (isNaN(num)) throw new Error('mag 不是数字'); oldVal.mag = num; }
}
afterApply('已改 ' + path);
} else {
if (!currentMilestoneLayer) { alert('没有可用的里程碑层级'); return; }
const m = getPlayerMilestones(currentMilestoneLayer);
if (!m) { alert('player.' + currentMilestoneLayer + '.milestones 不存在'); return; }
const keys = getMilestoneKeys(currentMilestoneLayer);
if (keys.length === 0) { alert('该层没有里程碑定义'); return; }
const targets = [];
Array.from(milestoneSelect.selectedOptions).forEach(opt => {
if (keys.includes(opt.value)) targets.push(opt.value);
});
const raw = milestoneInput.value.trim();
if (raw) {
raw.split(/[\s,]+/).filter(Boolean).forEach(x => {
if (keys.includes(x)) targets.push(x);
else console.warn('[树修改] 忽略不存在的里程碑 ID:', x);
});
}
if (targets.length === 0) { alert('请选择或输入有效的里程碑 ID'); return; }
if (milestoneFill.checked) {
// 补全前面:整体替换为 keys.slice(0, maxIdx + 1)
let maxIdx = 0;
targets.forEach(t => {
const i = keys.indexOf(t);
if (i > maxIdx) maxIdx = i;
});
m.length = 0;
keys.slice(0, maxIdx + 1).forEach(k => m.push(k));
fillMilestoneList();
afterApply('已解锁 ' + currentMilestoneLayer + ' 里程碑到 ' + keys[maxIdx]);
} else {
// 只加选中的,不补前置
const added = [];
targets.forEach(t => {
if (!m.includes(t)) {
m.push(t);
added.push(t);
}
});
fillMilestoneList();
afterApply(added.length
? ('已添加 ' + currentMilestoneLayer + ' 里程碑:' + added.join(', '))
: '没有新里程碑(已存在)');
}
}
} catch (e) {
alert('出错:' + e.message);
}
});
container.querySelectorAll('.tmt-ball-item').forEach(item => {
item.addEventListener('click', (e) => {
e.stopPropagation();
const act = item.dataset.act;
if (act === 'info') {
console.log('[树修改] player:', W.player);
console.log('[树修改] layers:', W.layers);
alert('player 存在,key 数量:' + Object.keys(W.player).length + '\n详情看控制台');
}
if (act === 'edit') {
if (!warnOnce()) return;
openPanel();
}
});
});
console.log('[树修改] 悬浮球已生成');
}
waitForTMT(createBall);
})();

















微信打赏支持
支付宝打赏支持






