// ==UserScript== // @name A Dark Room(小黑屋/暗室/黑暗房间) 游戏资源辅助提示信息优化版 // @namespace http://tampermonkey.net/ // @version 1.9.2.3 // @description 优化蓝图功能 gamesurl https://adarkroom.doublespeakgames.com/?lang=zh_cn // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; // 提示信息框 function showSuccess(message, duration = 3000) { const div = document.createElement("div"); div.innerText = message; div.style.cssText = ` position: fixed; top: 10%; /* 距离顶部 1/5 */ left: 50%; /* 水平居中 */ transform: translateX(-50%); background: #67C23A; /* 成功绿色 */ color: #fff; padding: 12px 24px; border-radius: 6px; font-size: 16px; z-index: 99999; box-shadow: 0 2px 8px rgba(0,0,0,0.3); transition: opacity 0.3s; `; document.body.appendChild(div); setTimeout(() => { div.style.opacity = "0"; setTimeout(() => div.remove(), 200); }, duration); } // 全局变量 let isExpanded = true; let dragOffsetX = 0; let dragOffsetY = 0; let isDragging = false; let state; let populationInput; // 全局保存人口输入框引用 let cooldownSpeedMultiplier = 1; // 冷却加速倍数 // 冷却加速核心逻辑 function overrideCooldownFunction() { if (window.Button && window.Button.cooldown && !window.Button.originalCooldown) { window.Button.originalCooldown = window.Button.cooldown; window.Button.cooldown = function(btn, option) { var cd = btn.data("cooldown"); // 关键修复:先检查boosted是否为函数再调用,避免类型错误 if (typeof btn.data('boosted') === 'function' && btn.data('boosted')()) { cd /= 2; } var id = 'cooldown.' + btn.attr('id'); if (cd > 0) { if (typeof option == 'number') { cd = option; } var start, left; switch (option) { case 'state': if (!$SM.get(id)) { return; } start = Math.min($SM.get(id), cd); left = (start / cd).toFixed(4); break; default: start = cd; left = 1; } window.Button.clearCooldown(btn); if (window.Button.saveCooldown) { $SM.set(id, start); // 清除可能存在的旧计时器,避免多重计时 if (btn.data('countdown')) { window.Engine.clearInterval(btn.data('countdown')); } btn.data('countdown', window.Engine.setInterval(function() { const current = $SM.get(id, true) || 0; if (current <= 0) { window.Engine.clearInterval(btn.data('countdown')); btn.data('countdown', null); return; } $SM.set(id, current - 0.5, true); }, 500)); } var time = start; // 应用自定义加速倍数 if (cooldownSpeedMultiplier > 0) { time /= cooldownSpeedMultiplier; } else if (window.Engine?.options?.doubleTime) { time /= 2; } // 获取冷却元素,确保存在 const cooldownElem = $('div.cooldown', btn); if (cooldownElem.length === 0) { // 如果没有冷却元素,创建一个 btn.append('
'); } $('div.cooldown', btn).width(left * 100 + "%") .animate({ width: '0%' }, time * 1000, 'linear', function() { // 动画结束后强制清除冷却状态 window.Button.clearCooldown(btn, true); btn.removeClass('disabled'); btn.data('onCooldown', false); // 清除计时器 if (btn.data('countdown')) { window.Engine.clearInterval(btn.data('countdown')); btn.data('countdown', null); } }); btn.addClass('disabled'); btn.data('onCooldown', true); } }; console.log("[A Dark Room 辅助] 冷却加速功能已注入"); } } // 更新冷却加速倍数 function updateCooldownSpeed(speed) { cooldownSpeedMultiplier = Math.max(0.5, Math.min(speed, 100)); showSuccess(`冷却加速已设置为 ${cooldownSpeedMultiplier}x`); } function injectButton() { if (document.getElementById("buttonContainer")) return; // 初始化游戏状态 initState(); overrideCooldownFunction(); // 资源容器检查 let resourceContainers = [ window.game && window.game.resources, window.room && window.room.resources, window.resources ]; let hasResources = (state && state.stores && "wood" in state.stores) || resourceContainers.some(c => c && "wood" in c); if (!hasResources) return; // 按钮样式函数 function styleButton(btn, color = "#28a745") { btn.style.margin = "5px 0"; btn.style.padding = "6px 10px"; btn.style.border = "none"; btn.style.borderRadius = "6px"; btn.style.cursor = "pointer"; btn.style.background = color; btn.style.color = "white"; btn.style.fontSize = "14px"; btn.style.boxShadow = "0 2px 6px rgba(0,0,0,0.3)"; btn.style.transition = "background 0.2s ease"; btn.style.width = "100%"; btn.style.pointerEvents = "auto"; } // 创建主容器 const mainContainer = document.createElement("div"); mainContainer.id = "mainContainer"; mainContainer.style.position = "fixed"; mainContainer.style.top = "10px"; mainContainer.style.right = "10px"; mainContainer.style.zIndex = 9999; mainContainer.style.background = "rgba(30,30,30,0.9)"; mainContainer.style.borderRadius = "8px"; mainContainer.style.boxShadow = "0 4px 12px rgba(0,0,0,0.4)"; mainContainer.style.pointerEvents = "auto"; document.body.appendChild(mainContainer); // 标题栏 const titleBar = document.createElement("div"); titleBar.style.padding = "8px 12px"; titleBar.style.background = "#2d3436"; titleBar.style.color = "white"; titleBar.style.borderRadius = "8px 8px 0 0"; titleBar.style.cursor = "move"; titleBar.style.display = "flex"; titleBar.style.justifyContent = "space-between"; titleBar.style.alignItems = "center"; mainContainer.appendChild(titleBar); const titleText = document.createElement("span"); titleText.innerText = "游戏辅助工具"; titleText.style.fontWeight = "bold"; titleBar.appendChild(titleText); const toggleBtn = document.createElement("button"); toggleBtn.innerText = "−"; toggleBtn.style.width = "24px"; toggleBtn.style.height = "24px"; toggleBtn.style.padding = "0"; toggleBtn.style.background = "#636e72"; toggleBtn.style.border = "none"; toggleBtn.style.borderRadius = "4px"; toggleBtn.style.color = "white"; toggleBtn.style.cursor = "pointer"; titleBar.appendChild(toggleBtn); // 内容容器 const contentContainer = document.createElement("div"); contentContainer.id = "contentContainer"; contentContainer.style.display = "flex"; contentContainer.style.flexDirection = "row"; contentContainer.style.padding = "10px"; contentContainer.style.gap = "10px"; contentContainer.style.pointerEvents = "auto"; mainContainer.appendChild(contentContainer); // 左列:资源操作 + 冷却加速控制 const leftColumn = document.createElement("div"); leftColumn.style.display = "flex"; leftColumn.style.flexDirection = "column"; leftColumn.style.minWidth = "140px"; contentContainer.appendChild(leftColumn); // 一键加满资源 const maxBtn = document.createElement("button"); maxBtn.innerText = "💰 一键加满资源"; styleButton(maxBtn); leftColumn.appendChild(maxBtn); maxBtn.onclick = function() { updateState(); if (state.stores) { Object.keys(state.stores).forEach(k => { if (typeof state.stores[k] === 'number' && state.stores[k] < 999999) { state.stores[k] = 999999; } }); } const rootResources = ["cured meat", "torch", "waterskin", "bone spear", "rucksack", "l armour", "bolas", "cask", "wagon", "i armour", "iron sword", "bullets", "medicine", "rifle", "steel sword", "water tank", "convoy", "s armour", "charm", "alienalloy", "laserrifle", "energycell", "grenade", "energyblade", "fluidrecycler", "cargodrone", "hypo", "kineticarmour", "glowstone", "stim", "disruptor", "plasmarifle"]; rootResources.forEach(key => { if (state[key] === undefined || (typeof state[key] === 'number' && state[key] < 999999)) { state[key] = 999999; } }); saveState(); showSuccess('资源已加满!'); }; // 快速收集木头 let gatherBtn = document.createElement("button"); gatherBtn.innerText = "🪵 开始收集木头"; styleButton(gatherBtn, "#007bff"); leftColumn.appendChild(gatherBtn); let gatherTimer = null, isGathering = false; gatherBtn.onclick = function() { if (!isGathering) { gatherBtn.innerText = "🪵 结束收集木头"; gatherBtn.style.background = "#dc3545"; isGathering = true; gatherTimer = setInterval(() => { const gatherBtnElem = document.getElementById('gatherButton') || document.querySelector('.gather-button'); if (gatherBtnElem && gatherBtnElem.classList.contains('disabled')) { gatherBtnElem.classList.remove('disabled'); } gatherBtnElem?.click(); }, 100); showSuccess("已开启自动收集木头"); } else { gatherBtn.innerText = "🪤 开始收集木头"; gatherBtn.style.background = "#007bff"; isGathering = false; clearInterval(gatherTimer); showSuccess("已停止自动收集木头"); } }; // 快速查看陷阱 let trapBtn = document.createElement("button"); trapBtn.innerText = "🪤 开始查看陷阱"; styleButton(trapBtn, "#17a2b8"); leftColumn.appendChild(trapBtn); let trapTimer = null, isTrapping = false; trapBtn.onclick = function() { if (!isTrapping) { trapBtn.innerText = "🪤 结束查看陷阱"; trapBtn.style.background = "#dc3545"; isTrapping = true; trapTimer = setInterval(() => { const trapBtnElem = document.getElementById('trapsButton') || document.querySelector('.traps-button'); if (trapBtnElem && trapBtnElem.classList.contains('disabled')) { trapBtnElem.classList.remove('disabled'); } trapBtnElem?.click(); }, 100); showSuccess("已开启自动查看陷阱"); } else { trapBtn.innerText = "🪤 开始查看陷阱"; trapBtn.style.background = "#17a2b8"; isTrapping = false; clearInterval(trapTimer); showSuccess("已停止自动查看陷阱"); } }; // 重置资源 const resetBtn = document.createElement("button"); resetBtn.innerText = "♻️ 重置资源"; styleButton(resetBtn, "#fd7e14"); leftColumn.appendChild(resetBtn); resetBtn.onclick = function() { if (confirm("确定要重置所有资源吗?")) { updateState(); if (state.stores) { Object.keys(state.stores).forEach(k => { state.stores[k] = 1; }); } const rootResources = ["cured meat", "torch", "waterskin", "bone spear", "rucksack", "l armour", "bolas", "cask", "wagon", "i armour", "iron sword", "bullets", "medicine", "rifle", "steel sword", "water tank", "convoy", "s armour", "charm", "alienalloy", "laserrifle", "energycell", "grenade", "energyblade", "fluidrecycler", "cargodrone", "hypo", "kineticarmour", "glowstone", "stim", "disruptor", "plasmarifle"]; rootResources.forEach(key => { if (state[key] !== undefined && typeof state[key] === 'number') { state[key] = 1; } }); saveState(); showSuccess('已重置所有资源'); } }; // 冷却加速控制(重置资源下方) const cooldownControlContainer = document.createElement("div"); cooldownControlContainer.className = "cooldown-control-container"; cooldownControlContainer.style.cssText = ` margin: 8px 0; padding: 8px; background: rgba(50,50,50,0.8); border-radius: 6px; display: flex; flex-direction: column; gap: 6px; `; leftColumn.appendChild(cooldownControlContainer); // 加速标题 const cooldownTitle = document.createElement("span"); cooldownTitle.innerText = "⏱️ 冷却加速控制"; cooldownTitle.style.cssText = "color: #fff; font-size: 14px; font-weight: bold; text-align: center;"; cooldownControlContainer.appendChild(cooldownTitle); // 当前加速倍数显示 const speedDisplay = document.createElement("span"); speedDisplay.className = "cooldown-speed-display"; speedDisplay.innerText = `当前:${cooldownSpeedMultiplier}x`; speedDisplay.style.cssText = "color: #fff; font-size: 13px; text-align: center;"; cooldownControlContainer.appendChild(speedDisplay); // 加速滑块 const speedSlider = document.createElement("input"); speedSlider.className = "cooldown-speed-slider"; speedSlider.type = "range"; speedSlider.min = 0.5; speedSlider.max = 100; speedSlider.step = 0.5; speedSlider.value = cooldownSpeedMultiplier; speedSlider.style.width = "100%"; // 滑块拖动事件:阻止冒泡 speedSlider.addEventListener('mousedown', function(e) { e.stopPropagation(); }); // 滑块值变化时更新显示 speedSlider.addEventListener("input", function() { const speed = parseFloat(this.value); cooldownSpeedMultiplier = speed; speedDisplay.innerText = `当前:${speed}x`; }); cooldownControlContainer.appendChild(speedSlider); // 滑块范围标签 const sliderLabel = document.createElement("div"); sliderLabel.style.cssText = "display: flex; justify-content: space-between; color: #aaa; font-size: 12px;"; sliderLabel.innerHTML = "0.5x(慢)100x(快)"; cooldownControlContainer.appendChild(sliderLabel); // 加速操作按钮 const speedBtnContainer = document.createElement("div"); speedBtnContainer.style.display = "flex"; speedBtnContainer.style.gap = "6px"; cooldownControlContainer.appendChild(speedBtnContainer); // 应用加速按钮 const applySpeedBtn = document.createElement("button"); applySpeedBtn.innerText = "✅ 应用加速"; styleButton(applySpeedBtn, "#28a745"); applySpeedBtn.style.fontSize = "12px"; applySpeedBtn.style.padding = "4px 6px"; applySpeedBtn.onclick = function(e) { e.stopPropagation(); updateCooldownSpeed(cooldownSpeedMultiplier); }; speedBtnContainer.appendChild(applySpeedBtn); // 重置加速按钮 const resetSpeedBtn = document.createElement("button"); resetSpeedBtn.innerText = "🔄 重置为1x"; styleButton(resetSpeedBtn, "#dc3545"); resetSpeedBtn.style.fontSize = "12px"; resetSpeedBtn.style.padding = "4px 6px"; resetSpeedBtn.onclick = function(e) { e.stopPropagation(); cooldownSpeedMultiplier = 1; speedSlider.value = 1; speedDisplay.innerText = `当前:1x`; updateCooldownSpeed(1); }; speedBtnContainer.appendChild(resetSpeedBtn); // 中列:地图全开 + 战斗控制 + 解锁功能 const rightColumn = document.createElement("div"); rightColumn.style.display = "flex"; rightColumn.style.flexDirection = "column"; rightColumn.style.minWidth = "140px"; contentContainer.appendChild(rightColumn); // 地图全开功能 const unlockMapBtn = document.createElement("button"); unlockMapBtn.innerText = "🗺️ 地图全开"; styleButton(unlockMapBtn, "#e67e22"); rightColumn.appendChild(unlockMapBtn); unlockMapBtn.onclick = function() { updateState(); if (!state.game) state.game = {}; if (!state.game.world) state.game.world = {}; if (!state.game.world.mask) { showSuccess("当前地图数据为空,无需解锁!"); return; } let updatedCount = 0; state.game.world.mask.forEach((row, i) => { if (Array.isArray(row)) { row.forEach((cell, j) => { if (cell === null) { state.game.world.mask[i][j] = true; updatedCount++; } }); } }); saveState(); showSuccess(`地图已全开!共解锁 ${updatedCount} 个未知区域`); }; // 自动击杀 let attackBtn = document.createElement("button"); attackBtn.id = "attackButton"; attackBtn.innerText = "👊 关闭自动击杀"; styleButton(attackBtn, "#dc3545"); rightColumn.appendChild(attackBtn); let attackFlag = true; attackBtn.onclick = function() { attackFlag = !attackFlag; if (attackFlag) { attackBtn.innerText = "👊 关闭自动击杀"; attackBtn.style.background = "#dc3545"; showSuccess("已开启自动击杀"); } else { attackBtn.innerText = "👊 开启自动击杀"; attackBtn.style.background = "#28a745"; showSuccess("已关闭自动击杀"); } }; setInterval(() => { if (attackFlag) { const fistAttack = document.getElementById('attack_fists') || document.querySelector('.attack-fists'); if (fistAttack && fistAttack.classList.contains('disabled')) { fistAttack.classList.remove('disabled'); } fistAttack?.click(); const steelSwordAttack = document.getElementById('attack_steel-sword') || document.querySelector('.attack-steel-sword'); if (steelSwordAttack && steelSwordAttack.classList.contains('disabled')) { steelSwordAttack.classList.remove('disabled'); } steelSwordAttack?.click(); } }, 100); // 技能解锁 const unlockPerksBtn = document.createElement("button"); unlockPerksBtn.innerText = "🥋 解锁全部武功技能"; styleButton(unlockPerksBtn, "#ffc107"); rightColumn.appendChild(unlockPerksBtn); unlockPerksBtn.onclick = function() { updateState(); const allPerks = [ "stealthy", "boxer", "martial artist", "evasive", "unarmed master", "gastronome", "scout", "precise", "barbarian" ]; let unlockedCount = 0; allPerks.forEach(perk => { if (state.character.perks[perk] !== true) { state.character.perks[perk] = true; unlockedCount++; } }); saveState(); showSuccess(`武功技能解锁完成!共解锁 ${unlockedCount} 个新技能`); }; // 飞船蓝图解锁 const unlockBlueprintsBtn = document.createElement("button"); unlockBlueprintsBtn.innerText = "📜 解锁全部科技蓝图"; styleButton(unlockBlueprintsBtn, "#17a2b8"); rightColumn.appendChild(unlockBlueprintsBtn); unlockBlueprintsBtn.onclick = function() { updateState(); // "hologram" const allBlueprints = [ "hypo", "kineticarmour", "stim", "glowstone", "disruptor", "plasmarifle", "laserrifle", "energyblade", "grenade", "energycell", "alienalloy", "fluidrecycler", "cargodrone" ]; let unlockedCount = 0; allBlueprints.forEach(blueprint => { if (state.character.blueprints[blueprint] !== true) { state.character.blueprints[blueprint] = true; unlockedCount++; } }); saveState(); showSuccess(`科技蓝图解锁完成!共解锁 ${unlockedCount} 个新蓝图`); }; // 科技建筑解锁 const unlockBuildingsBtn = document.createElement("button"); unlockBuildingsBtn.innerText = "🏗️ 解锁全部科技建筑"; styleButton(unlockBuildingsBtn, "#dc3545"); rightColumn.appendChild(unlockBuildingsBtn); unlockBuildingsBtn.onclick = function() { updateState(); // "advanced workshop": 1, "drone factory": 1, "laboratory": 1, "nuclear reactor": 1, "spaceship dock": 1, "defense tower": 5 const allBuildings = { "cart": 1, "trap": 10, "hut": 20, "lodge": 1, "trading post": 1, "tannery": 1, "smokehouse": 1, "workshop": 1, "iron mine": 1, "steelworks": 1, "coal mine": 1, "sulphur mine": 1, "armoury": 1 }; let unlockedCount = 0; Object.keys(allBuildings).forEach(building => { if (state.game.buildings[building] === undefined) { state.game.buildings[building] = allBuildings[building]; unlockedCount++; } }); saveState(); showSuccess(`科技建筑解锁完成!共解锁 ${unlockedCount} 种新建筑`); }; // 探险冷却 const cooldownBtn = document.createElement("button"); cooldownBtn.id = "cooldownButton"; cooldownBtn.innerText = "🧌 探险冷却(有)"; styleButton(cooldownBtn, "#6f42c1"); let cooldownFlag = false, cooldownInterval = null; rightColumn.appendChild(cooldownBtn); cooldownBtn.onclick = function() { if (!cooldownFlag) { cooldownFlag = true; cooldownBtn.innerText = "🧌 探险冷却(无)"; cooldownInterval = setInterval(() => { let embarkButton = document.querySelector("#embarkButton"); if (embarkButton) { embarkButton.classList.remove("disabled"); } $('#liftoffButton').removeClass('disabled'); }, 500); showSuccess("已关闭探险冷却(强制移除禁用)"); } else { cooldownFlag = false; cooldownBtn.innerText = "🧌 探险冷却(有)"; clearInterval(cooldownInterval); cooldownInterval = null; showSuccess("已开启探险冷却(恢复默认逻辑)"); } }; // 中列:人口编辑 + 导入导出(优化焦点问题) const middleColumn = document.createElement("div"); middleColumn.style.display = "flex"; middleColumn.style.flexDirection = "column"; middleColumn.style.minWidth = "140px"; contentContainer.appendChild(middleColumn); // 人口编辑功能(核心优化区域) const populationContainer = document.createElement("div"); populationContainer.style.display = "flex"; populationContainer.style.flexDirection = "column"; populationContainer.style.gap = "5px"; populationContainer.style.margin = "5px 0"; populationContainer.style.pointerEvents = "auto"; // 添加人口容器专属类名 populationContainer.className = "population-control-container"; middleColumn.appendChild(populationContainer); const populationLabel = document.createElement("span"); populationLabel.innerText = "人口数量:"; populationLabel.style.color = "white"; populationLabel.style.fontSize = "14px"; populationContainer.appendChild(populationLabel); // 人口输入框(优化焦点) populationInput = document.createElement("input"); populationInput.type = "number"; populationInput.min = "1"; populationInput.style.padding = "6px"; populationInput.style.borderRadius = "4px"; populationInput.style.border = "2px solid #9b59b6"; populationInput.style.fontSize = "14px"; populationInput.style.width = "90%"; populationInput.style.zIndex = "10000"; populationInput.style.pointerEvents = "auto"; populationInput.removeAttribute('disabled'); populationInput.removeAttribute('readonly'); // 初始化输入框值 const currentPopulation = state.game?.population || state.population || 80; populationInput.value = currentPopulation; populationContainer.appendChild(populationInput); // 核心优化1:鼠标悬浮时自动激活并聚焦输入框 populationInput.addEventListener('mouseenter', function() { // 确保输入框处于可编辑状态 this.removeAttribute('disabled'); this.removeAttribute('readonly'); // 添加视觉反馈 this.style.borderColor = "#4CAF50"; this.style.boxShadow = "0 0 0 2px rgba(76, 175, 80, 0.2)"; // 自动聚焦 this.focus(); }); // 核心优化2:鼠标离开时保持可编辑状态但移除焦点样式 populationInput.addEventListener('mouseleave', function() { // 保留可编辑状态,仅调整样式 this.style.borderColor = "#9b59b6"; this.style.boxShadow = "none"; }); // 优化点击事件:仅在点击输入框容器时聚焦(不影响其他元素) populationContainer.addEventListener('click', function(e) { // 点击容器空白区域时聚焦输入框 if (e.target === populationContainer) { populationInput.focus(); } }); // 输入验证 populationInput.addEventListener('input', function() { this.value = this.value.replace(/[^0-9]/g, ''); const value = parseInt(this.value, 10); if (isNaN(value) || value < 1) { this.value = 1; } }); // 应用人口设置按钮 const setPopulationBtn = document.createElement("button"); setPopulationBtn.innerText = "👥 应用人口设置"; styleButton(setPopulationBtn, "#9b59b6"); populationContainer.appendChild(setPopulationBtn); setPopulationBtn.onclick = function(e) { e.stopPropagation(); // 阻止事件冒泡 updateState(); const newPopulation = parseInt(populationInput.value, 10); if (isNaN(newPopulation) || newPopulation < 1) { showSuccess("请输入有效的人口数量(至少1)", 2000); return; } // 多路径支持 if (state.game) { state.game.population = newPopulation; } else { state.population = newPopulation; } saveState(); showSuccess(`人口已设置为:${newPopulation}`); }; // 导出游戏状态 const exportBtn = document.createElement("button"); exportBtn.innerText = "💾 导出游戏状态"; styleButton(exportBtn, "#27ae60"); middleColumn.appendChild(exportBtn); exportBtn.onclick = function() { updateState(); const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(state, null, 2)); const downloadAnchorNode = document.createElement('a'); downloadAnchorNode.setAttribute("href", dataStr); downloadAnchorNode.setAttribute("download", "a_dark_room_save_" + new Date().getTime() + ".json"); document.body.appendChild(downloadAnchorNode); downloadAnchorNode.click(); downloadAnchorNode.remove(); showSuccess("游戏状态已导出!"); }; // 复制游戏状态 const copyBtn = document.createElement("button"); copyBtn.innerText = "📋 复制游戏状态"; styleButton(copyBtn, "#f39c12"); middleColumn.appendChild(copyBtn); copyBtn.onclick = function() { updateState(); navigator.clipboard.writeText(JSON.stringify(state, null, 2)) .then(() => showSuccess("游戏状态已复制到剪贴板!")) .catch(err => { console.error('无法复制内容: ', err); showSuccess("复制失败,请手动复制!", 2000); }); }; // 导入游戏状态 const importBtn = document.createElement("button"); importBtn.innerText = "📂 导入游戏状态"; styleButton(importBtn, "#3498db"); middleColumn.appendChild(importBtn); const fileInput = document.createElement("input"); fileInput.type = "file"; fileInput.accept = ".json"; fileInput.style.display = "none"; document.body.appendChild(fileInput); importBtn.onclick = function() { if (confirm("确定要导入游戏状态吗?这将覆盖当前进度!")) { fileInput.click(); } }; fileInput.onchange = function(event) { const file = event.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = function(e) { try { const importedState = JSON.parse(e.target.result); if (typeof importedState === 'object' && importedState !== null) { state = importedState; const currentPopulation = state.game?.population || state.population || 80; populationInput.value = currentPopulation; saveState(); showSuccess("导入成功!"); } else { showSuccess("导入的数据无效!", 2000); } } catch (error) { console.error("导入失败:", error); showSuccess("导入失败,文件格式不正确!", 2000); } }; reader.readAsText(file); fileInput.value = ""; }; // 初始化状态函数 function initState() { try { state = JSON.parse(localStorage.getItem("gameState")) || {}; if (!state.character) state.character = {}; if (!state.character.perks) state.character.perks = {}; if (!state.character.blueprints) state.character.blueprints = {}; if (!state.game) state.game = {}; if (!state.game.buildings) state.game.buildings = {}; if (!state.game.world) state.game.world = {}; if (!state.game.world.mask) state.game.world.mask = []; if (!state.stores) state.stores = {}; } catch (e) { console.error("初始化游戏状态失败:", e); state = { character: { perks: {}, blueprints: {} }, game: { buildings: {}, world: { mask: [] } }, stores: {} }; } } // 更新状态函数 function updateState() { try { const latestState = JSON.parse(localStorage.getItem("gameState")) || {}; Object.assign(state, latestState); if (!state.character) state.character = {}; if (!state.character.perks) state.character.perks = {}; if (!state.character.blueprints) state.character.blueprints = {}; if (!state.game) state.game = {}; if (!state.game.buildings) state.game.buildings = {}; if (!state.game.world) state.game.world = {}; if (!state.game.world.mask) state.game.world.mask = []; if (!state.stores) state.stores = {}; } catch (e) { console.error("更新游戏状态失败:", e); } } // 保存状态函数 function saveState() { try { localStorage.setItem("gameState", JSON.stringify(state)); setTimeout(() => { location.reload(); }, 200); } catch (e) { console.error("保存游戏状态失败:", e); showSuccess("操作失败,请重试!", 2000); } } // 展开关闭功能 toggleBtn.onclick = function() { isExpanded = !isExpanded; contentContainer.style.display = isExpanded ? "flex" : "none"; toggleBtn.innerText = isExpanded ? "−" : "+"; }; // 拖动功能 titleBar.addEventListener("mousedown", function(e) { isDragging = true; const rect = mainContainer.getBoundingClientRect(); dragOffsetX = e.clientX - rect.left; dragOffsetY = e.clientY - rect.top; titleBar.style.cursor = "grabbing"; e.stopPropagation(); }); document.addEventListener("mousemove", function(e) { if (isDragging) { const x = e.clientX - dragOffsetX; const y = e.clientY - dragOffsetY; const viewportWidth = window.innerWidth; const viewportHeight = window.innerHeight; const containerWidth = mainContainer.offsetWidth; const containerHeight = mainContainer.offsetHeight; const constrainedX = Math.max(0, Math.min(x, viewportWidth - containerWidth)); const constrainedY = Math.max(0, Math.min(y, viewportHeight - containerHeight)); mainContainer.style.left = constrainedX + "px"; mainContainer.style.top = constrainedY + "px"; mainContainer.style.right = "auto"; mainContainer.style.bottom = "auto"; } }); document.addEventListener("mouseup", function() { if (isDragging) { isDragging = false; titleBar.style.cursor = "move"; } }); } // 定时检查游戏加载状态 const interval = setInterval(() => { if (localStorage.getItem("gameState") || window.game || window.room || window.resources) { injectButton(); clearInterval(interval); } }, 500); // 10秒超时保护 setTimeout(() => { clearInterval(interval); const hasGameState = localStorage.getItem("gameState") !== null; const hasWindowResources = window.game || window.room || window.resources; if (!hasGameState && !hasWindowResources) { // showSuccess("10秒内未检测到游戏,辅助工具未加载", 3000); } }, 10000); })();