Compare commits
7
Commits
740c42f55c
...
454a50edf2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
454a50edf2 | ||
|
|
3d9eb8e015 | ||
|
|
4f9e962e6b | ||
|
|
9ebcabc3c1 | ||
|
|
a741980f40 | ||
|
|
4b15c4d16e | ||
|
|
0d4b855718 |
+266
-35
@@ -1,8 +1,8 @@
|
|||||||
// ==UserScript==
|
// ==UserScript==
|
||||||
// @name Mysteel 钢铁价格提取器
|
// @name Mysteel 钢铁价格提取器
|
||||||
// @namespace http://tampermonkey.net/
|
// @namespace http://tampermonkey.net/
|
||||||
// @version 6.5
|
// @version 0.8.2
|
||||||
// @description 合并列表页与详情页的抓取逻辑,统一透视表导出csv,支持多维筛选及本地缓存,加入登录状态拦截
|
// @description 实现列表页与详情页的抓取逻辑,统一透视表导出csv/xlsx,支持多维筛选及本地缓存
|
||||||
// @author ali
|
// @author ali
|
||||||
// @mail solitude@hraulein.com
|
// @mail solitude@hraulein.com
|
||||||
// @match *://*.mysteel.com/market/*
|
// @match *://*.mysteel.com/market/*
|
||||||
@@ -19,12 +19,20 @@
|
|||||||
const isDetailPage = window.location.href.includes('/m/');
|
const isDetailPage = window.location.href.includes('/m/');
|
||||||
|
|
||||||
// 预设筛选常量
|
// 预设筛选常量
|
||||||
const FILTER_OPTIONS = {
|
let FILTER_OPTIONS = {
|
||||||
names: ['高线', '螺纹钢', '盘螺'],
|
names: ["盘螺", "螺纹钢", "高线", "TEST"],
|
||||||
specs: ['Φ6', 'Φ8', 'Φ8-10', 'Φ10', 'Φ12', 'Φ14', 'Φ16', 'Φ18', 'Φ20', 'Φ22', 'Φ25', 'Φ28', 'Φ32', 'Φ36', 'Φ36-40', 'Φ40'],
|
specs: ["Φ6", "Φ8-10", "Φ8", "Φ10", "Φ12", "Φ14", "Φ16", "Φ18", "Φ20", "Φ22", "Φ25", "Φ28", "Φ32", "Φ36-40", "Φ36", "Φ40", "TEST"],
|
||||||
materials: ['HPB300', 'HRB400', 'HRB400E', 'HRB500E']
|
materials: ["HPB300", "HRB400", "HRB400E", "HRB500E", "TEST"]
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 尝试加载本地存储的动态筛选项目
|
||||||
|
try {
|
||||||
|
const dynamicOpts = localStorage.getItem('mysteel_dynamic_options');
|
||||||
|
if (dynamicOpts) {
|
||||||
|
Object.assign(FILTER_OPTIONS, JSON.parse(dynamicOpts));
|
||||||
|
}
|
||||||
|
} catch (e) { }
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 状态守卫区
|
// 状态守卫区
|
||||||
// ==========================================
|
// ==========================================
|
||||||
@@ -41,7 +49,7 @@
|
|||||||
|
|
||||||
// 读取本地缓存的筛选条件
|
// 读取本地缓存的筛选条件
|
||||||
function loadSavedFilters() {
|
function loadSavedFilters() {
|
||||||
const defaultFilters = { names: [], specs: [], materials: [], exportFormat: 'csv', exportFilename: '钢材透视价格表' };
|
const defaultFilters = { names: [], specs: [], materials: [], exportFormat: 'csv', exportFilename: '钢材透视价格表', debugMode: false, devMode: false };
|
||||||
const saved = localStorage.getItem('mysteel_filters');
|
const saved = localStorage.getItem('mysteel_filters');
|
||||||
if (saved) {
|
if (saved) {
|
||||||
try {
|
try {
|
||||||
@@ -58,16 +66,19 @@
|
|||||||
localStorage.setItem('mysteel_filters', JSON.stringify(filters));
|
localStorage.setItem('mysteel_filters', JSON.stringify(filters));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取当前活动筛选条件 (直接从 DOM 获取)
|
|
||||||
function getCurrentFilters() {
|
function getCurrentFilters() {
|
||||||
const formatEl = document.getElementById('ms-filter-format');
|
const formatEl = document.getElementById('ms-filter-format');
|
||||||
const filenameEl = document.getElementById('ms-filter-filename');
|
const filenameEl = document.getElementById('ms-filter-filename');
|
||||||
|
const debugModeEl = document.getElementById('ms-debug-mode');
|
||||||
|
const devModeEl = document.getElementById('ms-dev-mode-switch');
|
||||||
return {
|
return {
|
||||||
names: Array.from(document.querySelectorAll('input[name="ms-filter-names"]:checked')).map(el => el.value),
|
names: Array.from(document.querySelectorAll('input[name="ms-filter-names"]:checked')).map(el => el.value),
|
||||||
specs: Array.from(document.querySelectorAll('input[name="ms-filter-specs"]:checked')).map(el => el.value),
|
specs: Array.from(document.querySelectorAll('input[name="ms-filter-specs"]:checked')).map(el => el.value),
|
||||||
materials: Array.from(document.querySelectorAll('input[name="ms-filter-materials"]:checked')).map(el => el.value),
|
materials: Array.from(document.querySelectorAll('input[name="ms-filter-materials"]:checked')).map(el => el.value),
|
||||||
exportFormat: formatEl ? formatEl.value : 'csv',
|
exportFormat: formatEl ? formatEl.value : 'csv',
|
||||||
exportFilename: filenameEl ? filenameEl.value : '钢材透视价格表'
|
exportFilename: filenameEl ? filenameEl.value : '钢材透视价格表',
|
||||||
|
devMode: devModeEl ? devModeEl.checked : false,
|
||||||
|
debugMode: (devModeEl && devModeEl.checked && debugModeEl) ? debugModeEl.checked : false
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +102,7 @@
|
|||||||
const btn = document.createElement('button');
|
const btn = document.createElement('button');
|
||||||
btn.id = 'mysteel-action-btn';
|
btn.id = 'mysteel-action-btn';
|
||||||
// 根据页面类型设定按钮文字
|
// 根据页面类型设定按钮文字
|
||||||
btn.innerText = isListPage ? '🚀 批量提取并透视导出' : '📊 导出当日数据(透视版)';
|
btn.innerText = isListPage ? '🚀 批量提取数据' : '📊 提取当日数据';
|
||||||
|
|
||||||
Object.assign(btn.style, {
|
Object.assign(btn.style, {
|
||||||
padding: '12px 24px',
|
padding: '12px 24px',
|
||||||
@@ -146,7 +157,7 @@
|
|||||||
return options.map(opt => {
|
return options.map(opt => {
|
||||||
const isChecked = savedFilters[groupName].includes(opt) ? 'checked' : '';
|
const isChecked = savedFilters[groupName].includes(opt) ? 'checked' : '';
|
||||||
return `
|
return `
|
||||||
<label style="display:inline-flex; align-items:center; margin-right:12px; margin-bottom:8px; font-size:13px; cursor:pointer;">
|
<label style="display:flex; align-items:center; font-size:13px; cursor:pointer;">
|
||||||
<input type="checkbox" name="ms-filter-${groupName}" value="${opt}" style="margin-right:4px;" ${isChecked}> ${opt}
|
<input type="checkbox" name="ms-filter-${groupName}" value="${opt}" style="margin-right:4px;" ${isChecked}> ${opt}
|
||||||
</label>
|
</label>
|
||||||
`;
|
`;
|
||||||
@@ -154,18 +165,47 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
panel.innerHTML = `
|
panel.innerHTML = `
|
||||||
<div style="font-size:16px; font-weight:bold; margin-bottom:15px; border-bottom:1px solid #eee; padding-bottom:8px;">高级筛选配置</div>
|
<style>
|
||||||
<div style="margin-bottom:12px;">
|
.ms-switch { position: relative; display: inline-block; width: 34px; height: 20px; }
|
||||||
<div style="font-size:13px; color:#333; font-weight:bold; margin-bottom:6px;">类别:</div>
|
.ms-switch input { opacity: 0; width: 0; height: 0; margin: 0; }
|
||||||
<div style="display:flex; flex-wrap:wrap;">${buildCheckboxes('names', FILTER_OPTIONS.names)}</div>
|
.ms-slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; border-radius: 34px; }
|
||||||
|
.ms-slider:before { position: absolute; content: ""; height: 14px; width: 14px; left: 3px; bottom: 3px; background-color: white; transition: .4s; border-radius: 50%; }
|
||||||
|
.ms-switch input:checked + .ms-slider { background-color: #1890ff; }
|
||||||
|
.ms-switch input:checked + .ms-slider:before { transform: translateX(14px); }
|
||||||
|
.ms-tooltip-btn { position: relative; }
|
||||||
|
.ms-tooltip-btn:hover::after { content: attr(data-tooltip); position: absolute; bottom: 120%; left: 50%; transform: translateX(-50%); background-color: rgba(0,0,0,0.8); color: #fff; padding: 4px 8px; border-radius: 4px; font-size: 12px; white-space: nowrap; z-index: 1000; pointer-events: none; }
|
||||||
|
.ms-tooltip-btn:hover::before { content: ''; position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%); border-width: 4px; border-style: solid; border-color: rgba(0,0,0,0.8) transparent transparent transparent; z-index: 1000; pointer-events: none; }
|
||||||
|
</style>
|
||||||
|
<div style="font-size:16px; font-weight:bold; margin-bottom:15px; border-bottom:1px solid #eee; padding-bottom:8px; display:flex; justify-content:space-between; align-items:center;">
|
||||||
|
<div style="display:flex; align-items:center; gap:8px;">
|
||||||
|
<span>筛选配置</span>
|
||||||
|
<button id="ms-extract-options-btn" class="ms-tooltip-btn" data-tooltip="从当前页面获取筛选项目并更新到脚本配置中" style="background:none; border:none; cursor:pointer; color:#1890ff; padding:2px; display:flex; align-items:center; justify-content:center; border-radius:4px; outline:none;">
|
||||||
|
<svg viewBox="0 0 24 24" width="16" height="16" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<polyline points="23 4 23 10 17 10"></polyline>
|
||||||
|
<polyline points="1 20 1 14 7 14"></polyline>
|
||||||
|
<path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"></path>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="ms-extract-confirm-box" style="display:none; margin-bottom:15px; padding:10px; background:#e6f7ff; border:1px solid #91d5ff; border-radius:4px;">
|
||||||
|
<div style="font-size:12px; color:#1890ff; margin-bottom:8px; line-height:1.4;">确定要从当前页面提取新的筛选参数吗?<br>这将会更新并覆盖现有的配置项目。</div>
|
||||||
|
<div style="display:flex; justify-content:flex-end; gap:8px;">
|
||||||
|
<button id="ms-extract-cancel-btn" style="background:#f5f5f5; color:#333; border:1px solid #d9d9d9; padding:2px 8px; border-radius:4px; font-size:12px; cursor:pointer; outline:none;">取消</button>
|
||||||
|
<button id="ms-extract-confirm-action-btn" style="background:#1890ff; color:#fff; border:none; padding:2px 8px; border-radius:4px; font-size:12px; cursor:pointer; outline:none;">确定</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div style="margin-bottom:12px;">
|
<div style="margin-bottom:12px;">
|
||||||
<div style="font-size:13px; color:#333; font-weight:bold; margin-bottom:6px;">线径:</div>
|
<div style="font-size:13px; color:#333; font-weight:bold; margin-bottom:8px;">类别:</div>
|
||||||
<div style="display:flex; flex-wrap:wrap;">${buildCheckboxes('specs', FILTER_OPTIONS.specs)}</div>
|
<div id="ms-names-container" style="display:grid; grid-template-columns:repeat(5, 1fr); gap:8px 4px;">${buildCheckboxes('names', FILTER_OPTIONS.names)}</div>
|
||||||
|
</div>
|
||||||
|
<div style="margin-bottom:12px;">
|
||||||
|
<div style="font-size:13px; color:#333; font-weight:bold; margin-bottom:8px;">线径:</div>
|
||||||
|
<div id="ms-specs-container" style="display:grid; grid-template-columns:repeat(5, 1fr); gap:8px 4px;">${buildCheckboxes('specs', FILTER_OPTIONS.specs)}</div>
|
||||||
</div>
|
</div>
|
||||||
<div style="margin-bottom:15px;">
|
<div style="margin-bottom:15px;">
|
||||||
<div style="font-size:13px; color:#333; font-weight:bold; margin-bottom:6px;">材质:</div>
|
<div style="font-size:13px; color:#333; font-weight:bold; margin-bottom:8px;">材质:</div>
|
||||||
<div style="display:flex; flex-wrap:wrap;">${buildCheckboxes('materials', FILTER_OPTIONS.materials)}</div>
|
<div id="ms-materials-container" style="display:grid; grid-template-columns:repeat(4, 1fr); gap:8px 4px;">${buildCheckboxes('materials', FILTER_OPTIONS.materials)}</div>
|
||||||
</div>
|
</div>
|
||||||
<div style="font-size:16px; font-weight:bold; margin-bottom:15px; border-bottom:1px solid #eee; padding-bottom:8px;">导出配置</div>
|
<div style="font-size:16px; font-weight:bold; margin-bottom:15px; border-bottom:1px solid #eee; padding-bottom:8px;">导出配置</div>
|
||||||
<div style="margin-bottom:12px; display:flex; gap:10px; align-items:flex-end;">
|
<div style="margin-bottom:12px; display:flex; gap:10px; align-items:flex-end;">
|
||||||
@@ -181,7 +221,35 @@
|
|||||||
<input type="text" id="ms-filter-filename" value="${savedFilters.exportFilename || '钢材透视价格表'}" style="width:100%; padding:4px 8px; font-size:13px; height:28px; box-sizing:border-box; border:1px solid #ccc; border-radius:4px; outline:none;">
|
<input type="text" id="ms-filter-filename" value="${savedFilters.exportFilename || '钢材透视价格表'}" style="width:100%; padding:4px 8px; font-size:13px; height:28px; box-sizing:border-box; border:1px solid #ccc; border-radius:4px; outline:none;">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button id="ms-close-panel-btn" style="width:100%; padding:8px; background:#1890ff; color:#fff; border:none; border-radius:4px; cursor:pointer; font-weight:bold;">保存并关闭</button>
|
<div style="font-size:16px; font-weight:bold; margin-bottom:15px; border-bottom:1px solid #eee; padding-bottom:8px; margin-top:15px; display:flex; justify-content:space-between; align-items:center;">
|
||||||
|
<span>开发者选项</span>
|
||||||
|
<label class="ms-switch">
|
||||||
|
<input type="checkbox" id="ms-dev-mode-switch" ${savedFilters.devMode ? 'checked' : ''}>
|
||||||
|
<span class="ms-slider"></span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div id="ms-dev-options-container" style="display:${savedFilters.devMode ? 'block' : 'none'}; margin-bottom:12px;">
|
||||||
|
<div style="display:flex; justify-content:flex-start; align-items:center; gap:10px;">
|
||||||
|
<input type="checkbox" id="ms-debug-mode" style="display:none;" ${savedFilters.debugMode ? 'checked' : ''}>
|
||||||
|
<button id="ms-debug-mode-btn" class="ms-tooltip-btn" data-tooltip="跳过登录限制、账号查询权限及价格拦截直接导出" style="background:${savedFilters.debugMode ? '#ff4d4f' : '#f5f5f5'}; color:${savedFilters.debugMode ? '#fff' : '#999'}; border:1px solid ${savedFilters.debugMode ? '#ff4d4f' : '#d9d9d9'}; padding:4px 8px; border-radius:4px; font-size:12px; cursor:pointer; font-weight:bold; outline:none; transition:.3s;">
|
||||||
|
DEBUG_MODE ${savedFilters.debugMode ? 'ON' : 'OFF'}
|
||||||
|
</button>
|
||||||
|
<button id="ms-reset-options-btn" class="ms-tooltip-btn" data-tooltip="恢复默认配置并清除所有缓存数据" style="background:#ff4d4f; color:#fff; border:none; padding:4px 8px; border-radius:4px; font-size:12px; cursor:pointer; font-weight:bold; outline:none;">RESET</button>
|
||||||
|
</div>
|
||||||
|
<div id="ms-reset-confirm-box" style="display:none; margin-top:10px; padding:10px; background:#fff2f0; border:1px solid #ffccc7; border-radius:4px;">
|
||||||
|
<div style="font-size:12px; color:#ff4d4f; margin-bottom:8px; line-height:1.4;">确定要恢复默认配置吗?<br>这将清除所有保存的配置和提取的筛选参数。</div>
|
||||||
|
<div style="display:flex; justify-content:flex-end; gap:8px;">
|
||||||
|
<button id="ms-reset-cancel-btn" style="background:#f5f5f5; color:#333; border:1px solid #d9d9d9; padding:2px 8px; border-radius:4px; font-size:12px; cursor:pointer; outline:none;">取消</button>
|
||||||
|
<button id="ms-reset-confirm-action-btn" style="background:#ff4d4f; color:#fff; border:none; padding:2px 8px; border-radius:4px; font-size:12px; cursor:pointer; outline:none;">确定</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style="display:flex; gap:10px; margin-top:15px;">
|
||||||
|
<button id="ms-apply-btn" style="flex:1; padding:8px; background:#fff; color:#1890ff; border:1px solid #1890ff; border-radius:4px; cursor:pointer; font-weight:bold; outline:none;">应用</button>
|
||||||
|
<button id="ms-save-btn" style="flex:1; padding:8px; background:#1890ff; color:#fff; border:none; border-radius:4px; cursor:pointer; font-weight:bold; outline:none;">保存</button>
|
||||||
|
<button id="ms-close-btn" style="flex:1; padding:8px; background:#f5f5f5; color:#333; border:1px solid #d9d9d9; border-radius:4px; cursor:pointer; font-weight:bold; outline:none;">关闭</button>
|
||||||
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// --- UI 交互逻辑 ---
|
// --- UI 交互逻辑 ---
|
||||||
@@ -217,21 +285,143 @@
|
|||||||
container.appendChild(gearBtn);
|
container.appendChild(gearBtn);
|
||||||
document.body.appendChild(container);
|
document.body.appendChild(container);
|
||||||
|
|
||||||
// 绑定保存关闭按钮事件
|
// 绑定开发者模式开关事件
|
||||||
document.getElementById('ms-close-panel-btn').addEventListener('click', () => {
|
document.getElementById('ms-dev-mode-switch').addEventListener('change', (e) => {
|
||||||
|
const devContainer = document.getElementById('ms-dev-options-container');
|
||||||
|
devContainer.style.display = e.target.checked ? 'block' : 'none';
|
||||||
|
});
|
||||||
|
|
||||||
|
// 绑定 DEBUG_MODE 按钮切换事件
|
||||||
|
document.getElementById('ms-debug-mode-btn').addEventListener('click', (e) => {
|
||||||
|
const checkbox = document.getElementById('ms-debug-mode');
|
||||||
|
checkbox.checked = !checkbox.checked;
|
||||||
|
const btn = e.target;
|
||||||
|
if (checkbox.checked) {
|
||||||
|
btn.style.background = '#ff4d4f';
|
||||||
|
btn.style.color = '#fff';
|
||||||
|
btn.style.borderColor = '#ff4d4f';
|
||||||
|
btn.innerText = 'DEBUG_MODE ON';
|
||||||
|
} else {
|
||||||
|
btn.style.background = '#f5f5f5';
|
||||||
|
btn.style.color = '#999';
|
||||||
|
btn.style.borderColor = '#d9d9d9';
|
||||||
|
btn.innerText = 'DEBUG_MODE OFF';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 绑定更新筛选项目相关事件
|
||||||
|
document.getElementById('ms-extract-options-btn').addEventListener('click', () => {
|
||||||
|
document.getElementById('ms-extract-confirm-box').style.display = 'block';
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('ms-extract-cancel-btn').addEventListener('click', () => {
|
||||||
|
document.getElementById('ms-extract-confirm-box').style.display = 'none';
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('ms-extract-confirm-action-btn').addEventListener('click', () => {
|
||||||
|
document.getElementById('ms-extract-confirm-box').style.display = 'none';
|
||||||
|
const getList = (num) => {
|
||||||
|
const ul = document.querySelector(`.table-filter-select[sort-line-num="${num}"] .table-filter-select-dropdown-list`);
|
||||||
|
if (!ul) return [];
|
||||||
|
return Array.from(ul.querySelectorAll('li')).map(li => li.innerText.trim());
|
||||||
|
};
|
||||||
|
const extractedNames = getList('1');
|
||||||
|
const extractedSpecs = getList('2');
|
||||||
|
const extractedMaterials = getList('3');
|
||||||
|
|
||||||
|
if (extractedNames.length === 0 && extractedSpecs.length === 0 && extractedMaterials.length === 0) {
|
||||||
|
showToast('筛选项目提取失败:当前页面未发现相关的筛选列表节点!\n请确认当前详情页或页面结构未改变。', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 对提取的数据进行格式化处理 (类别倒序,线径从小到大)
|
||||||
|
const sortedNames = extractedNames.reverse();
|
||||||
|
const sortedSpecs = extractedSpecs.sort((a, b) => {
|
||||||
|
const nA = parseFloat(a.replace('Φ', '').split('-')[0]) || 0;
|
||||||
|
const nB = parseFloat(b.replace('Φ', '').split('-')[0]) || 0;
|
||||||
|
return nA - nB;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 更新内存与本地缓存
|
||||||
|
FILTER_OPTIONS.names = sortedNames;
|
||||||
|
FILTER_OPTIONS.specs = sortedSpecs;
|
||||||
|
FILTER_OPTIONS.materials = extractedMaterials;
|
||||||
|
localStorage.setItem('mysteel_dynamic_options', JSON.stringify({
|
||||||
|
names: sortedNames,
|
||||||
|
specs: sortedSpecs,
|
||||||
|
materials: extractedMaterials
|
||||||
|
}));
|
||||||
|
|
||||||
|
// 更新 DOM 呈现
|
||||||
|
document.getElementById('ms-names-container').innerHTML = buildCheckboxes('names', FILTER_OPTIONS.names);
|
||||||
|
document.getElementById('ms-specs-container').innerHTML = buildCheckboxes('specs', FILTER_OPTIONS.specs);
|
||||||
|
document.getElementById('ms-materials-container').innerHTML = buildCheckboxes('materials', FILTER_OPTIONS.materials);
|
||||||
|
|
||||||
|
const code = ` let FILTER_OPTIONS = {\n names: ${JSON.stringify(sortedNames)},\n specs: ${JSON.stringify(sortedSpecs)},\n materials: ${JSON.stringify(extractedMaterials)}\n };`;
|
||||||
|
console.log('【提取并排序后的筛选项目】\\n' + code);
|
||||||
|
|
||||||
|
if (navigator.clipboard && window.isSecureContext) {
|
||||||
|
navigator.clipboard.writeText(code).then(() => {
|
||||||
|
showToast('筛选项目更新成功,配置代码已复制到剪贴板,可自行更新到脚本的 FILTER_OPTIONS 变量中', 'success');
|
||||||
|
}).catch(() => {
|
||||||
|
showToast('筛选项目更新成功,配置代码已复制到剪贴板,请按 F12 前往控制台复制。', 'success');
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
showToast('筛选项目更新成功,请按 F12 前往控制台复制配置代码。', 'success');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 绑定恢复默认设置相关事件
|
||||||
|
document.getElementById('ms-reset-options-btn').addEventListener('click', () => {
|
||||||
|
document.getElementById('ms-reset-confirm-box').style.display = 'block';
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('ms-reset-cancel-btn').addEventListener('click', () => {
|
||||||
|
document.getElementById('ms-reset-confirm-box').style.display = 'none';
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('ms-reset-confirm-action-btn').addEventListener('click', () => {
|
||||||
|
localStorage.removeItem('mysteel_filters');
|
||||||
|
localStorage.removeItem('mysteel_dynamic_options');
|
||||||
|
showToast('恢复默认配置成功!即将刷新页面...', 'success');
|
||||||
|
setTimeout(() => window.location.reload(), 1000);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 绑定底部按钮事件
|
||||||
|
document.getElementById('ms-apply-btn').addEventListener('click', () => {
|
||||||
const active = getCurrentFilters();
|
const active = getCurrentFilters();
|
||||||
saveFiltersToLocal(active); // 写入 localStorage
|
saveFiltersToLocal(active);
|
||||||
|
showToast('配置已应用', 'success');
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('ms-save-btn').addEventListener('click', () => {
|
||||||
|
const active = getCurrentFilters();
|
||||||
|
saveFiltersToLocal(active);
|
||||||
panel.style.display = 'none';
|
panel.style.display = 'none';
|
||||||
showToast('配置已保存', 'success');
|
showToast('配置已保存', 'success');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
document.getElementById('ms-close-btn').addEventListener('click', () => {
|
||||||
|
panel.style.display = 'none';
|
||||||
|
});
|
||||||
|
|
||||||
|
// 点击外部关闭面板 (不保存)
|
||||||
|
document.addEventListener('click', (e) => {
|
||||||
|
if (panel.style.display === 'block') {
|
||||||
|
if (!panel.contains(e.target) && !gearBtn.contains(e.target) && !tooltip.contains(e.target)) {
|
||||||
|
panel.style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 引擎 A: 详情页单页抓取
|
// 引擎 A: 详情页单页抓取
|
||||||
// ==========================================
|
// ==========================================
|
||||||
function startSingleProcess() {
|
function startSingleProcess() {
|
||||||
|
const filters = getCurrentFilters();
|
||||||
// 🔒 登录状态守卫拦截
|
// 🔒 登录状态守卫拦截
|
||||||
if (!isUserLoggedIn()) {
|
if (!filters.debugMode && !isUserLoggedIn()) {
|
||||||
showToast('请登录 Mysteel 账号后重试!', 'error');
|
showToast('请登录 Mysteel 账号后重试!', 'error');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -278,8 +468,9 @@
|
|||||||
// 引擎 B: 列表页批量抓取
|
// 引擎 B: 列表页批量抓取
|
||||||
// ==========================================
|
// ==========================================
|
||||||
async function startBatchProcess() {
|
async function startBatchProcess() {
|
||||||
|
const filters = getCurrentFilters();
|
||||||
// 🔒 登录状态守卫拦截
|
// 🔒 登录状态守卫拦截
|
||||||
if (!isUserLoggedIn()) {
|
if (!filters.debugMode && !isUserLoggedIn()) {
|
||||||
showToast('请登录 Mysteel 账号后重试!', 'error');
|
showToast('请登录 Mysteel 账号后重试!', 'error');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -479,7 +670,7 @@
|
|||||||
if (!tdBreed || !tdSpec || !tdMaterial || !tdPrice) continue;
|
if (!tdBreed || !tdSpec || !tdMaterial || !tdPrice) continue;
|
||||||
|
|
||||||
if (tdPrice.querySelector('.icon-key')) {
|
if (tdPrice.querySelector('.icon-key')) {
|
||||||
throw new Error('NO_PERMISSION');
|
if (!filters.debugMode) throw new Error('NO_PERMISSION');
|
||||||
}
|
}
|
||||||
|
|
||||||
const cleanText = (element) => {
|
const cleanText = (element) => {
|
||||||
@@ -499,13 +690,18 @@
|
|||||||
if (filters.materials.length > 0 && !filters.materials.includes(material)) continue;
|
if (filters.materials.length > 0 && !filters.materials.includes(material)) continue;
|
||||||
|
|
||||||
const priceStr = tdPrice.innerText.replace(/,/g, '').trim();
|
const priceStr = tdPrice.innerText.replace(/,/g, '').trim();
|
||||||
const currentPrice = parseFloat(priceStr);
|
let currentPrice = parseFloat(priceStr);
|
||||||
|
|
||||||
if (!name || isNaN(currentPrice)) continue;
|
if (!name) continue;
|
||||||
|
|
||||||
|
if (isNaN(currentPrice)) {
|
||||||
|
if (!filters.debugMode) continue;
|
||||||
|
currentPrice = tdPrice.querySelector('.icon-key') ? '无权限(锁)' : (priceStr || '无数据');
|
||||||
|
}
|
||||||
|
|
||||||
const uniqueKey = `${name}_${spec}_${material}`;
|
const uniqueKey = `${name}_${spec}_${material}`;
|
||||||
|
|
||||||
if (!dataMap.has(uniqueKey) || currentPrice < dataMap.get(uniqueKey).priceNum) {
|
if (!dataMap.has(uniqueKey)) {
|
||||||
dataMap.set(uniqueKey, {
|
dataMap.set(uniqueKey, {
|
||||||
date: dateStr,
|
date: dateStr,
|
||||||
name: name,
|
name: name,
|
||||||
@@ -513,6 +709,15 @@
|
|||||||
material: material,
|
material: material,
|
||||||
priceNum: currentPrice
|
priceNum: currentPrice
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
const existing = dataMap.get(uniqueKey);
|
||||||
|
if (typeof currentPrice === 'number' && typeof existing.priceNum === 'number') {
|
||||||
|
if (currentPrice < existing.priceNum) {
|
||||||
|
existing.priceNum = currentPrice;
|
||||||
|
}
|
||||||
|
} else if (typeof currentPrice === 'number') {
|
||||||
|
existing.priceNum = currentPrice;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Array.from(dataMap.values());
|
return Array.from(dataMap.values());
|
||||||
@@ -552,18 +757,42 @@
|
|||||||
// 读取导出设置
|
// 读取导出设置
|
||||||
const filters = getCurrentFilters();
|
const filters = getCurrentFilters();
|
||||||
const exportFormat = filters.exportFormat || 'csv';
|
const exportFormat = filters.exportFormat || 'csv';
|
||||||
|
|
||||||
|
// 3. 排序逻辑
|
||||||
|
const rows = Array.from(pivotMap.values());
|
||||||
|
const nameOrder = {};
|
||||||
|
FILTER_OPTIONS.names.forEach((n, i) => nameOrder[n] = i);
|
||||||
|
const specOrder = {};
|
||||||
|
FILTER_OPTIONS.specs.forEach((s, i) => specOrder[s] = i);
|
||||||
|
|
||||||
|
rows.sort((a, b) => {
|
||||||
|
const nA = nameOrder[a.name] !== undefined ? nameOrder[a.name] : 99;
|
||||||
|
const nB = nameOrder[b.name] !== undefined ? nameOrder[b.name] : 99;
|
||||||
|
if (nA !== nB) return nA - nB;
|
||||||
|
|
||||||
|
const sA = specOrder[a.spec] !== undefined ? specOrder[a.spec] : 99;
|
||||||
|
const sB = specOrder[b.spec] !== undefined ? specOrder[b.spec] : 99;
|
||||||
|
return sA - sB;
|
||||||
|
});
|
||||||
|
|
||||||
const exportFilename = filters.exportFilename || '钢材透视价格表';
|
const exportFilename = filters.exportFilename || '钢材透视价格表';
|
||||||
const d = new Date();
|
const d = new Date();
|
||||||
const dateStr = `${d.getFullYear()}${(d.getMonth() + 1).toString().padStart(2, '0')}${d.getDate().toString().padStart(2, '0')}`;
|
const Y = d.getFullYear();
|
||||||
|
const M = (d.getMonth() + 1).toString().padStart(2, '0');
|
||||||
|
const D = d.getDate().toString().padStart(2, '0');
|
||||||
|
const H = d.getHours().toString().padStart(2, '0');
|
||||||
|
const m = d.getMinutes().toString().padStart(2, '0');
|
||||||
|
const S = d.getSeconds().toString().padStart(2, '0');
|
||||||
|
const dateStr = `${Y}${M}${D}${H}${m}${S}`;
|
||||||
const finalFilename = `${exportFilename}_${filePrefix}_${dateStr}`;
|
const finalFilename = `${exportFilename}_${filePrefix}_${dateStr}`;
|
||||||
|
|
||||||
if (exportFormat === 'xlsx' && typeof XLSX !== 'undefined') {
|
if (exportFormat === 'xlsx' && typeof XLSX !== 'undefined') {
|
||||||
const aoa = [];
|
const aoa = [];
|
||||||
aoa.push(['类别', '线径', '材质', ...dateHeaders]);
|
aoa.push(['类别', '线径', '材质', ...dateHeaders]);
|
||||||
pivotMap.forEach(row => {
|
rows.forEach(row => {
|
||||||
const rowData = [row.name, row.spec, row.material];
|
const rowData = [row.name, row.spec, row.material];
|
||||||
uniqueDates.forEach(date => {
|
uniqueDates.forEach(date => {
|
||||||
rowData.push(row.pricesByDate[date] ? row.pricesByDate[date] : '');
|
rowData.push(row.pricesByDate[date] !== undefined ? row.pricesByDate[date] : '');
|
||||||
});
|
});
|
||||||
aoa.push(rowData);
|
aoa.push(rowData);
|
||||||
});
|
});
|
||||||
@@ -572,14 +801,14 @@
|
|||||||
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
|
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
|
||||||
XLSX.writeFile(wb, `${finalFilename}.xlsx`);
|
XLSX.writeFile(wb, `${finalFilename}.xlsx`);
|
||||||
} else {
|
} else {
|
||||||
// 3. 拼接 CSV 内容
|
// 4. 拼接 CSV 内容
|
||||||
let csvContent = '\uFEFF';
|
let csvContent = '\uFEFF';
|
||||||
csvContent += `类别,线径,材质,${dateHeaders.join(',')}\n`;
|
csvContent += `类别,线径,材质,${dateHeaders.join(',')}\n`;
|
||||||
|
|
||||||
pivotMap.forEach(row => {
|
rows.forEach(row => {
|
||||||
let rowText = `"${row.name}","${row.spec}","${row.material}"`;
|
let rowText = `"${row.name}","${row.spec}","${row.material}"`;
|
||||||
uniqueDates.forEach(date => {
|
uniqueDates.forEach(date => {
|
||||||
const price = row.pricesByDate[date] ? row.pricesByDate[date] : '';
|
const price = row.pricesByDate[date] !== undefined ? row.pricesByDate[date] : '';
|
||||||
rowText += `,"${price}"`;
|
rowText += `,"${price}"`;
|
||||||
});
|
});
|
||||||
csvContent += rowText + '\n';
|
csvContent += rowText + '\n';
|
||||||
@@ -599,6 +828,8 @@
|
|||||||
document.body.removeChild(link);
|
document.body.removeChild(link);
|
||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showToast('操作完成,请查看浏览器下载记录!', 'success');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 延时加载注入
|
// 延时加载注入
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Mysteel 钢铁价格提取器
|
# Mysteel 钢铁价格提取器
|
||||||
|
|
||||||
Mysteel 钢铁价格提取器 (Tampermonkey UserScript) 是一个专为 Mysteel (我的钢铁网) 开发的自动化数据抓取与处理工具。它能够在列表页和详情页中提取钢材价格信息,并自动生成方便分析的透视表 CSV 格式文件进行直接导出。
|
Mysteel 钢铁价格提取器 (Tampermonkey UserScript) 是一个专为 Mysteel (我的钢铁网) 开发的自动化数据抓取与处理工具。它能够在列表页和详情页中提取钢材价格信息,并自动生成方便分析的透视表 CSV/XLSX 格式文件进行直接导出。
|
||||||
|
|
||||||
## 🌟 核心特性
|
## 🌟 核心特性
|
||||||
|
|
||||||
@@ -8,10 +8,12 @@ Mysteel 钢铁价格提取器 (Tampermonkey UserScript) 是一个专为 Mysteel
|
|||||||
- **列表页批量模式 (引擎 B)**:在市场列表页自动识别并遍历每日价格链接,利用隐藏 `iframe` 静默抓取多日数据,最终生成跨越时间维度的价格透视表。
|
- **列表页批量模式 (引擎 B)**:在市场列表页自动识别并遍历每日价格链接,利用隐藏 `iframe` 静默抓取多日数据,最终生成跨越时间维度的价格透视表。
|
||||||
- **详情页单日模式 (引擎 A)**:在具体日期详情页直接提取当日价格数据并生成单日透视表。
|
- **详情页单日模式 (引擎 A)**:在具体日期详情页直接提取当日价格数据并生成单日透视表。
|
||||||
- **多维高级筛选**:内置可配置的悬浮筛选面板,支持按 **类别 (高线/螺纹钢/盘螺)**、**线径 (Φ6 - Φ40)** 和 **材质 (HPB300 等)** 进行精准过滤,只导出关注的数据。
|
- **多维高级筛选**:内置可配置的悬浮筛选面板,支持按 **类别 (高线/螺纹钢/盘螺)**、**线径 (Φ6 - Φ40)** 和 **材质 (HPB300 等)** 进行精准过滤,只导出关注的数据。
|
||||||
|
- **一键提取页面筛选参数**:支持在列表页点击“刷新”小图标,自动从网页提取最新的筛选参数(保持线径由小到大、品名倒序的智能排序)并即时更新到面板中,支持跨页面持久化保存,彻底告别手动修改代码。
|
||||||
- **自定义导出设置**:支持导出格式选择(`.csv` 或 `.xlsx`)及自定义文件名称。
|
- **自定义导出设置**:支持导出格式选择(`.csv` 或 `.xlsx`)及自定义文件名称。
|
||||||
- **本地状态缓存**:筛选配置支持本地存储 (LocalStorage),自动保存您上一次选择的过滤及导出条件,一次配置,后续复用。
|
- **本地状态缓存**:筛选配置支持本地存储 (LocalStorage),自动保存您上一次选择的过滤及导出条件,一次配置,后续复用。
|
||||||
- **智能权限及登录校验**:操作前自动检测 Mysteel 账号的登录状态。若未登录或账号无历史数据查阅权限,将智能拦截并提示,避免抓取到无效的隐藏数据。
|
- **智能权限及登录校验**:操作前自动检测 Mysteel 账号的登录状态。若未登录或账号无历史数据查阅权限,将智能拦截并提示,避免抓取到无效的隐藏数据。
|
||||||
- **轻量无侵入 UI**:在页面右下角悬浮显示操作主按钮与设置齿轮。支持鼠标悬停快速查看当前过滤状态,UI 设计现代、简洁且易用。
|
- **轻量无侵入 UI**:在页面右下角悬浮显示操作主按钮与设置齿轮。支持鼠标悬停快速查看当前过滤状态,UI 设计现代、简洁且易用(支持点击空白处智能关闭面板,以及应用、保存、关闭独立按键)。
|
||||||
|
- **开发者选项**:内置开发者调试功能面板,支持开启 `DEBUG_MODE`(跳过登录与账号权限拦截,直接使用测试/无价格数据导出体验流程),以及一键恢复默认配置。
|
||||||
- **数据自动清洗与透视**:提取数据时自动清洗非文本符号(如图标、换行等),对于同一规格材料的多个价格,智能提取最低价格,最终生成以日期为列头的标准二维透视表格,完美适配 Excel 及其他数据分析工具。
|
- **数据自动清洗与透视**:提取数据时自动清洗非文本符号(如图标、换行等),对于同一规格材料的多个价格,智能提取最低价格,最终生成以日期为列头的标准二维透视表格,完美适配 Excel 及其他数据分析工具。
|
||||||
|
|
||||||
## 🚀 安装指南
|
## 🚀 安装指南
|
||||||
@@ -21,25 +23,25 @@ Mysteel 钢铁价格提取器 (Tampermonkey UserScript) 是一个专为 Mysteel
|
|||||||
3. 将本项目中 `MysteelGetMinPrice.js` 文件的所有代码复制,并粘贴覆盖到新建的脚本编辑器中。
|
3. 将本项目中 `MysteelGetMinPrice.js` 文件的所有代码复制,并粘贴覆盖到新建的脚本编辑器中。
|
||||||
4. 保存脚本 (Windows/Linux: `Ctrl + S`, macOS: `Cmd + S`)。
|
4. 保存脚本 (Windows/Linux: `Ctrl + S`, macOS: `Cmd + S`)。
|
||||||
5. 确保脚本处于开启状态。
|
5. 确保脚本处于开启状态。
|
||||||
6. **自动更新**: 请在油猴脚本的设置中添加更新URL: `https://gitea.hraulein.com/ali/UserScript/raw/branch/main/MysteelGetMinPrice.js` 并保存
|
6. **自动更新**: 请在油猴脚本的设置中添加更新 URL: `https://gitea.hraulein.com/ali/UserScript/raw/branch/main/MysteelGetMinPrice.js` 并保存。
|
||||||
|
|
||||||
## 📖 使用说明
|
## 📖 使用说明
|
||||||
|
|
||||||
### 场景一:列表页批量导出
|
### 场景一:列表页批量导出
|
||||||
1. 登录 [Mysteel](https://www.mysteel.com/) 账号。
|
1. 登录 [Mysteel](https://www.mysteel.com/) 账号。
|
||||||
2. 导航至某地市场的钢材价格列表页(网址匹配 `https://*.mysteel.com/market/*`)。
|
2. 导航至某地市场的钢材价格列表页(网址匹配 `https://*.mysteel.com/market/*`)。
|
||||||
3. 页面右下角会出现橙色的 **`🚀 批量提取并透视导出`** 按钮。
|
3. 页面右下角会出现橙色的 **`🚀 批量提取数据`** 按钮。
|
||||||
4. 点击齿轮 `⚙️` 图标展开配置面板,勾选您想要筛选的钢材类别、线径和材质,然后点击“保存并关闭”。
|
4. 点击齿轮 `⚙️` 图标展开配置面板,勾选您想要筛选的钢材类别、线径和材质,点击“应用”或“保存”。
|
||||||
5. 点击橙色的批量提取按钮,工具会自动开始遍历当前列表页的所有目标链接并在后台逐一提取。
|
5. 点击橙色的批量提取按钮,工具会自动开始遍历当前列表页的所有目标链接并在后台逐一提取。
|
||||||
6. 提取完成后,浏览器会自动下载名为 `钢材透视价格表_批量_YYYYMMDD.csv` 的文件。
|
6. 提取完成后,页面上方会弹出成功提示,浏览器会自动下载名为 `钢材透视价格表_批量_YYYYMMDDHHMMSS.csv` 的文件。
|
||||||
|
|
||||||
### 场景二:详情页单日导出
|
### 场景二:详情页单日导出
|
||||||
1. 登录 Mysteel 账号。
|
1. 登录 Mysteel 账号。
|
||||||
2. 导航至某一天的特定价格详情页(网址匹配 `https://*.mysteel.com/m/*`)。
|
2. 导航至某一天的特定价格详情页(网址匹配 `https://*.mysteel.com/m/*`)。
|
||||||
3. 页面右下角会出现蓝色的 **`📊 导出当日数据(透视版)`** 按钮。
|
3. 页面右下角会出现蓝色的 **`📊 提取当日数据`** 按钮。
|
||||||
4. 同样可以通过点击齿轮 `⚙️` 预先配置所需的筛选条件。
|
4. 同样可以通过点击齿轮 `⚙️` 预先配置所需的筛选条件。
|
||||||
5. 点击蓝色操作按钮,工具将直接读取当前页面的表格数据并进行透视。
|
5. 点击蓝色操作按钮,工具将直接读取当前页面的表格数据并进行透视。
|
||||||
6. 提取完成后,浏览器会自动下载名为 `钢材透视价格表_单日_YYYYMMDD.csv` 的文件。
|
6. 提取完成后,浏览器会自动下载名为 `钢材透视价格表_单日_YYYYMMDDHHMMSS.csv` 的文件。
|
||||||
|
|
||||||
## ⚙️ 匹配网址及权限
|
## ⚙️ 匹配网址及权限
|
||||||
|
|
||||||
@@ -50,7 +52,6 @@ Mysteel 钢铁价格提取器 (Tampermonkey UserScript) 是一个专为 Mysteel
|
|||||||
**权限声明 (Grant)**:
|
**权限声明 (Grant)**:
|
||||||
- `none` (无特殊越权权限要求,完全基于当前页面 DOM 及标准 Web API 运行,安全可靠)
|
- `none` (无特殊越权权限要求,完全基于当前页面 DOM 及标准 Web API 运行,安全可靠)
|
||||||
|
|
||||||
|
|
||||||
## 👨💻 开发者
|
## 👨💻 开发者
|
||||||
|
|
||||||
- **Author**: ali
|
- **Author**: ali
|
||||||
Reference in New Issue
Block a user