舍弃 alert 换成 toast 提示, 新增账号权限检查

This commit is contained in:
ali
2026-07-29 11:18:21 +08:00
parent 8dfd89a2ce
commit e314881dba
+75 -11
View File
@@ -1,9 +1,10 @@
// ==UserScript==
// @name Mysteel 钢铁价格提取器 (双擎透视+缓存+登录校验版)
// @name Mysteel 钢铁价格提取器
// @namespace http://tampermonkey.net/
// @version 6.1
// @description 合并列表页与详情页的抓取逻辑,统一透视表导出,支持多维筛选及本地缓存,加入登录状态拦截
// @version 6.2
// @description 合并列表页与详情页的抓取逻辑,统一透视表导出csv,支持多维筛选及本地缓存,加入登录状态拦截
// @author ali
// @mail solitude@hraulein.com
// @match *://*.mysteel.com/market/*
// @match *://*.mysteel.com/m/*
// @grant none
@@ -207,7 +208,7 @@
function startSingleProcess() {
// 🔒 登录状态守卫拦截
if (!isUserLoggedIn()) {
alert('系统检测到您未登录!\n请先登录 Mysteel 账号后再进行数据提取。');
showToast('请先登录 Mysteel 账号后再进行数据提取操作。', 'error');
return;
}
@@ -220,7 +221,7 @@
// 提取日期:从 <span class="content-info-text publish-time"> 提取
const dateEl = document.querySelector('.content-info-text.publish-time');
if (!dateEl) {
alert('未找到页面的发布时间,请确认当前网页结构!');
showToast('未找到页面的发布时间,请确认当前网页结构!', 'error');
return;
}
// 提取 "2026-07-24 17:41" 中的 "2026-07-24"
@@ -234,11 +235,15 @@
if (flatData.length > 0) {
generatePivotCSV(flatData, '单日');
} else {
alert('根据当前筛选条件,未提取到有效数据!');
showToast('根据当前筛选条件,未提取到有效数据!', 'warning');
}
} catch (error) {
if (error.message === 'NO_PERMISSION') {
showToast('当前账号暂无权限查看价格数据,请确认是否已购买或开通相关权限!', 'warning');
} else {
console.error('详情页提取出错:', error);
alert('发生错误,请查看控制台日志。');
showToast('发生错误,请查看控制台日志。', 'error');
}
} finally {
btn.innerText = originalText;
btn.disabled = false;
@@ -251,7 +256,7 @@
async function startBatchProcess() {
// 🔒 登录状态守卫拦截
if (!isUserLoggedIn()) {
alert('系统检测到您未登录!\n请先登录 Mysteel 账号后再进行批量提取。');
showToast('请先登录 Mysteel 账号后再进行批量提取操作。', 'error');
return;
}
@@ -264,7 +269,7 @@
btn.innerText = '正在分析列表...';
const targetLinks = getTargetLinks();
if (targetLinks.length === 0) {
alert('未发现符合条件的链接!');
showToast('未发现符合条件的链接!', 'warning');
return;
}
@@ -285,6 +290,7 @@
}
}
} catch (err) {
if (err.message === 'NO_PERMISSION') throw err;
console.error(`渲染 ${linkObj.date} 失败:`, err);
}
}
@@ -294,12 +300,16 @@
if (flatData.length > 0) {
generatePivotCSV(flatData, '批量');
} else {
alert('根据当前筛选条件,未能提取到任何数据!');
showToast('根据当前筛选条件,未能提取到任何数据!', 'warning');
}
} catch (error) {
if (error.message === 'NO_PERMISSION') {
showToast('当前账号暂无权限查看价格数据,请确认是否已购买或开通相关权限!', 'warning');
} else {
console.error('批量处理出错:', error);
alert('发生错误,请查看控制台。');
showToast('发生错误,请查看控制台。', 'error');
}
} finally {
btn.innerText = originalText;
btn.disabled = false;
@@ -311,6 +321,56 @@
// 公共组件区
// ==========================================
// Toast 提示组件
function showToast(message, type = 'info') {
const toast = document.createElement('div');
toast.innerText = message;
let bgColor = '#1890ff';
if (type === 'error') bgColor = '#ff4d4f';
else if (type === 'success') bgColor = '#52c41a';
else if (type === 'warning') bgColor = '#faad14';
Object.assign(toast.style, {
position: 'fixed',
top: '20px',
left: '50%',
transform: 'translateX(-50%)',
backgroundColor: bgColor,
color: '#fff',
padding: '12px 24px',
borderRadius: '4px',
boxShadow: '0 4px 12px rgba(0,0,0,0.15)',
zIndex: '999999',
fontSize: '14px',
whiteSpace: 'pre-wrap',
opacity: '0',
transition: 'opacity 0.3s, top 0.3s',
pointerEvents: 'none',
textAlign: 'center',
lineHeight: '1.5'
});
document.body.appendChild(toast);
// 动画显示
requestAnimationFrame(() => {
toast.style.opacity = '1';
toast.style.top = '40px';
});
// 定时移除
setTimeout(() => {
toast.style.opacity = '0';
toast.style.top = '20px';
setTimeout(() => {
if (toast.parentNode) {
toast.parentNode.removeChild(toast);
}
}, 300);
}, 5000);
}
// 获取列表页的日期链接
function getTargetLinks() {
const listItems = document.querySelectorAll('#articleList .nlist li');
@@ -392,6 +452,10 @@
if (!tdBreed || !tdSpec || !tdMaterial || !tdPrice) continue;
if (tdPrice.querySelector('.icon-key')) {
throw new Error('NO_PERMISSION');
}
const cleanText = (element) => {
let clone = element.cloneNode(true);
let childIcons = clone.querySelectorAll('a, img, div');