// ==UserScript==
// @name MWI Item Favorites Manager
// @namespace http://tampermonkey.net/
// @version test0.15
// @description 仓库物品收藏管理||Added a favorite button to the item menu.
// @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 https://update.greasyfork.cloud/scripts/531682/MWI%20Item%20Favorites%20Manager.user.js
// @updateURL https://update.greasyfork.cloud/scripts/531682/MWI%20Item%20Favorites%20Manager.meta.js
// ==/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, categoryName) {
const characterName = getCharacterName();
if (!characterName) return;
const storageKey = `mw_favorites_${characterName}`;
const favorites = loadFavoritesFromLocalStorage();
// 检查是否已存在相同物品
const existingIndex = favorites.findIndex(item => item.name === itemName);
if (existingIndex === -1) {
favorites.push({name: itemName, category: categoryName});
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.findIndex(item => item.name === itemName);
const isFavorite = itemIndex !== -1;
if (isFavorite) {
// 获取物品原分类
const itemCategory = favorites[itemIndex].category;
// 从收藏中移除
favorites.splice(itemIndex, 1);
localStorage.setItem(`mw_favorites_${characterName}`, JSON.stringify(favorites));
const favoritesGrid = document.querySelector('#favorites-category .Inventory_itemGrid__20YAH');
const existingItem = favoritesGrid.querySelector(`svg[aria-label="${itemName}"]`);
if (existingItem) {
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 categorySpan = [...document.querySelectorAll('.Inventory_categoryButton__35s1x')]
.find(span => span.textContent.trim() === itemCategory);
if (categorySpan) {
// 找到分类所在的grid容器
const categoryGrid = categorySpan.closest('.Inventory_itemGrid__20YAH');
if (categoryGrid) {
// 将物品返回原分类
categoryGrid.appendChild(itemContainer);
}
}
refresh();
//existingItem.closest('.Item_itemContainer__x7kH1').remove();
}
} else {
// 使用已定义的函数添加到收藏
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 categoryGrid = itemContainer.closest('.Inventory_itemGrid__20YAH');
const categoryName = categoryGrid ?
categoryGrid.querySelector('.Inventory_categoryButton__35s1x')?.textContent.trim() :
'未知分类';
saveFavoritesToLocalStorage(itemName, categoryName);
// 添加到收藏分类
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(item => { // 改为遍历对象
const inventoryItem = document.querySelector(`.Inventory_items__6SXv0 .Item_itemContainer__x7kH1 svg[aria-label="${item.name}"]`);
if (inventoryItem) {
const itemContainer = inventoryItem.closest('.Item_itemContainer__x7kH1');
const existingItem = favoritesGrid.querySelector(`svg[aria-label="${item.name}"]`);
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);
})();