// ==UserScript== // @name A Dark Room(小黑屋/暗室/黑暗房间) 游戏资源辅助提示信息优化版 // @namespace http://tampermonkey.net/ // @version 1.8.6.1 // @description 调整人口输入框长度,使其更短 // @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(), 1000); }, duration); } // 全局变量 let isExpanded = true; let dragOffsetX = 0; let dragOffsetY = 0; let isDragging = false; let state; let populationInput; // 全局保存人口输入框引用 function injectButton() { if (document.getElementById("buttonContainer")) return; // 初始化游戏状态 initState(); // 资源容器列表 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", "alien alloy", "laser rifle", // "energy cell", "grenade", "energy blade", "fluid recycler", // "cargo drone", "hypo", "kinetic armour", "glowstone", "stim", "disruptor", "plasma rifle"]; // rootResources.forEach(key => { // if(state[key] === undefined || (typeof state[key] === 'number' && state[key]<999999)){ // state[key] = 999999; // } // }); saveState(); showSuccess('资源已加满!') // alert("资源已加满!"); }; // 快速收集木头 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); } else { gatherBtn.innerText = "🪵 开始收集木头"; gatherBtn.style.background = "#007bff"; isGathering = false; clearInterval(gatherTimer); } }; // 快速查看陷阱 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); } else { trapBtn.innerText = "🪤 开始查看陷阱"; trapBtn.style.background = "#17a2b8"; isTrapping = false; clearInterval(trapTimer); } }; // 重置资源 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", "alien alloy", "laser rifle", // "energy cell", "grenade", "energy blade", "fluid recycler", // "cargo drone", "hypo", "kinetic armour", "glowstone", "stim", "disruptor", "plasma rifle"]; // rootResources.forEach(key => { // state[key] = 1; // }); saveState(); showSuccess('已重置所有资源') } }; // 中列:地图全开 + 战斗控制 + 解锁功能 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) { alert("当前地图数据为空,无需解锁!"); 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} 个未知区域`) // alert(`地图已全开!共解锁 ${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"; } else { attackBtn.innerText = "👊 开启自动击杀"; attackBtn.style.background = "#28a745"; } }; 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} 个新技能`) // alert(`武功技能解锁完成!共解锁 ${unlockedCount} 个新技能`); }; // 飞船蓝图解锁 const unlockBlueprintsBtn = document.createElement("button"); unlockBlueprintsBtn.innerText = "📜 解锁全部科技蓝图"; styleButton(unlockBlueprintsBtn, "#17a2b8"); rightColumn.appendChild(unlockBlueprintsBtn); unlockBlueprintsBtn.onclick = function() { updateState(); const allBlueprints = [ "hypo", "kinetic armour", "glowstone", "stim", "disruptor", "plasma rifle", "laser rifle", "energy blade", "grenade", "energy cell", "alien alloy", "fluid recycler", "cargo drone", "advanced armour", "rocket launcher", "hologram" ]; let unlockedCount = 0; allBlueprints.forEach(blueprint => { if(state.character.blueprints[blueprint] !== true){ state.character.blueprints[blueprint] = true; unlockedCount++; } }); saveState(); showSuccess(`科技蓝图解锁完成!共解锁 ${unlockedCount} 个新蓝图`) // alert(`科技蓝图解锁完成!共解锁 ${unlockedCount} 个新蓝图`); }; // 科技建筑解锁 const unlockBuildingsBtn = document.createElement("button"); unlockBuildingsBtn.innerText = "🏗️ 解锁全部科技建筑"; styleButton(unlockBuildingsBtn, "#dc3545"); rightColumn.appendChild(unlockBuildingsBtn); unlockBuildingsBtn.onclick = function() { updateState(); 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, "laboratory": 1, "spaceship dock": 1, "drone factory": 1, "advanced workshop": 1, "nuclear reactor": 1, "defense tower": 5 }; let unlockedCount = 0; Object.keys(allBuildings).forEach(building => { if(state.game.buildings[building] === undefined){ state.game.buildings[building] = allBuildings[building]; unlockedCount++; } }); saveState(); showSuccess(`科技建筑解锁完成!共解锁 ${unlockedCount} 种新建筑`) // alert(`科技建筑解锁完成!共解锁 ${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); } else { cooldownFlag = false; cooldownBtn.innerText = "🧌 探险冷却(有)"; clearInterval(cooldownInterval); cooldownInterval = null; } }; // 中列:人口编辑 + 导入导出 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"; 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"; // 关键修改:将宽度从100%改为100px,可根据需要调整这个数值 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); // 点击容器聚焦输入框 populationContainer.addEventListener('click', function(e) { if (e.target !== populationInput) { 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() { updateState(); const newPopulation = parseInt(populationInput.value, 10); if (isNaN(newPopulation) || newPopulation < 1) { alert("请输入有效的人口数量(至少1)"); return; } // 多路径支持 if (state.game) { state.game.population = newPopulation; } else { state.population = newPopulation; } saveState(); alert(`人口已设置为:${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(); alert("游戏状态已导出!"); }; // 复制游戏状态 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(() => alert("游戏状态已复制到剪贴板!")) .catch(err => { console.error('无法复制内容: ', err); alert("复制失败,请手动复制!"); }); }; // 导入游戏状态 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(); alert("导入成功!"); } else { alert("导入的数据无效!"); } } catch (error) { console.error("导入失败:", error); alert("导入失败,文件格式不正确!"); } }; 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 = []; } catch(e) { console.error("初始化游戏状态失败:", e); state = { character: { perks: {}, blueprints: {} }, game: { buildings: {}, world: { mask: [] } } }; } } // 更新状态函数 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 = []; } catch(e) { console.error("更新游戏状态失败:", e); } } // 保存状态函数 function saveState() { try { localStorage.setItem("gameState", JSON.stringify(state)); setTimeout(() => { location.reload(); }, 500); } catch(e) { console.error("保存游戏状态失败:", e); alert("操作失败,请重试!"); } } // 展开关闭功能 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"; } }); // 初始聚焦输入框 setTimeout(() => { populationInput.focus(); }, 1000); } // 定时检查游戏加载状态 const interval = setInterval(() => { if (localStorage.getItem("gameState") || window.game || window.room || window.resources) { injectButton(); clearInterval(interval); } }, 500); })();