// ==UserScript==
// @name MWI Item Favorites Manager
// @namespace http://tampermonkey.net/
// @version test0.13
// @description 物品收藏管理
// @icon https://www.milkywayidle.com/favicon.svg
// @author Meoling
// @license MIT
// @match https://www.milkywayidle.com/*
// @match https://test.milkywayidle.com/*
// @grant GM_getValue
// @grant GM_setValue
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
// 获取当前角色名
function getCharacterName() {
const headerInfo = document.querySelector('.Header_info__26fkk');
if (!headerInfo) return null;
const nameElement = headerInfo.querySelector('.CharacterName_name__1amXp');
return nameElement ? nameElement.textContent.trim() : null;
}
// 保存收藏物品到本地存储
function saveFavoritesToLocalStorage(itemName) {
const characterName = getCharacterName();
if (!characterName) return;
const storageKey = `mw_favorites_${characterName}`;
let favorites = JSON.parse(localStorage.getItem(storageKey)) || [];
if (!favorites.includes(itemName)) {
favorites.push(itemName);
localStorage.setItem(storageKey, JSON.stringify(favorites));
}
}
// 从本地存储加载收藏物品
function loadFavoritesFromLocalStorage() {
const characterName = getCharacterName();
if (!characterName) return [];
const storageKey = `mw_favorites_${characterName}`;
return JSON.parse(localStorage.getItem(storageKey)) || [];
}
// 创建收藏分类
function addFavoritesCategory() {
// 查找仓库的所有分类容器
const firstContainer = document.querySelector('.Inventory_items__6SXv0');
const inventoryContainers = firstContainer.querySelectorAll(':scope > div');
if (inventoryContainers && inventoryContainers.length > 0) {
// 检查是否已经添加了收藏分类
const existingFavorites = firstContainer.querySelector('#favorites-category');
if (existingFavorites) {
return; // 已经存在收藏分类,不需要再添加
}
// 创建新的收藏分类
const favoritesContainer = document.createElement('div');
// 复制现有分类的结构
const itemGridHTML = `
`;
favoritesContainer.innerHTML = itemGridHTML;
favoritesContainer.id = 'favorites-category';
// 将收藏分类添加到仓库的最前面
if (firstContainer) {
firstContainer.insertBefore(favoritesContainer, firstContainer.firstChild);
//console.log('收藏分类已添加');
}
}
}
// 添加收藏按钮
function addFavoriteButton(menuContainer) {
// 检查是否已存在收藏按钮
const existingButton = menuContainer.querySelector('.favorite-button');
if (existingButton) {
return;
}
const favoriteButton = document.createElement('button');
favoriteButton.className = 'Button_button__1Fe9z Button_fullWidth__17pVU favorite-button';
favoriteButton.textContent = '收藏/取消收藏';
// 添加点击事件
favoriteButton.addEventListener('click', function() {
// 获取当前物品名称
const itemName = menuContainer.querySelector('.Item_name__2C42x').textContent.trim();
const characterName = getCharacterName();
if (!characterName) return;
const favorites = loadFavoritesFromLocalStorage();
const itemIndex = favorites.indexOf(itemName);
const isFavorite = itemIndex !== -1;
if (isFavorite) {
// 从收藏中移除
favorites.splice(itemIndex, 1);
localStorage.setItem(`mw_favorites_${characterName}`, JSON.stringify(favorites));
const favoritesGrid = document.querySelector('#favorites-category .Inventory_itemGrid__20YAH');
if (favoritesGrid) {
const existingItem = favoritesGrid.querySelector(`svg[aria-label="${itemName}"]`);
if (existingItem) {
existingItem.closest('.Item_itemContainer__x7kH1').remove();
}
}
} else {
// 使用已定义的函数添加到收藏
saveFavoritesToLocalStorage(itemName);
const inventoryItem = document.querySelector(`.Inventory_items__6SXv0 .Item_itemContainer__x7kH1 svg[aria-label="${itemName}"]`);
if (!inventoryItem) {
console.log('未在仓库中找到该物品');
return;
}
const itemContainer = inventoryItem.closest('.Item_itemContainer__x7kH1');
if (!itemContainer) {
console.log('无法获取物品容器');
return;
}
// 添加到收藏分类
const favoritesGrid = document.querySelector('#favorites-category .Inventory_itemGrid__20YAH');
if (!favoritesGrid) {
console.log('未找到收藏分类');
return;
}
// 检查是否已存在相同物品
const existingItem = favoritesGrid.querySelector(`svg[aria-label="${itemName}"]`);
if (!existingItem) {
favoritesGrid.appendChild(itemContainer);
}
}
});
// 将按钮添加到菜单底部
menuContainer.appendChild(favoriteButton);
}
// 刷新函数,当DOM变化时调用
function refresh() {
// 检查是否在仓库页面
const inventoryContainer = document.querySelector('.Inventory_items__6SXv0');
if (inventoryContainer) {
addFavoritesCategory();
// 加载收藏物品
const favorites = loadFavoritesFromLocalStorage();
const favoritesGrid = document.querySelector('#favorites-category .Inventory_itemGrid__20YAH');
if (favoritesGrid) {
favorites.forEach(itemName => {
const inventoryItem = document.querySelector(`.Inventory_items__6SXv0 .Item_itemContainer__x7kH1 svg[aria-label="${itemName}"]`);
if (inventoryItem) {
const itemContainer = inventoryItem.closest('.Item_itemContainer__x7kH1');
const existingItem = favoritesGrid.querySelector(`svg[aria-label="${itemName}"]`);
if (!existingItem && itemContainer) {
favoritesGrid.appendChild(itemContainer);
}
}
});
}
}
// 检查是否出现物品菜单
const itemMenu = document.querySelector('.Item_actionMenu__2yUcG');
if (itemMenu) {
addFavoriteButton(itemMenu);
}
}
// 设置MutationObserver监听DOM变化
const config = { attributes: true, childList: true, subtree: true };
const observer = new MutationObserver(function (mutationsList, observer) {
refresh();
});
observer.observe(document, config);
})();