识货APP图文高清下载素材

// ==UserScript==
// @name         识货DOM高清图自动存ID文件夹【美化永久授权版】
// @namespace    http://tampermonkey.net/
// @version      19.0
// @description  一次授权永久记住路径,美化可折叠UI,彩色日志,监控指定DOM自动保存高清原图到ID文件夹
// @match        *://m.shihuo.cn/app/assets/find_community/*/detail.html*
// @match        *://m.shihuo.cn/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-end
// @noframes
// ==/UserScript==

(function() {
    'use strict';

    // ========== 配置 ==========
    const TARGET_SELECTOR = 'body > div:nth-child(1) > div > div:nth-child(2)';
    const POST_ID = new URLSearchParams(window.location.search).get('id') || 'shihuo_img';
    const AUTH_KEY = 'shihuo_save_path_auth';

    let rootFolderHandle = null;
    let taskFinished = false;
    const saveUrlSet = new Set();

    // ========== 美化UI样式注入 ==========
    const style = document.createElement('style');
    style.textContent = `
        #shihuoMainPanel{
            position:fixed;
            bottom:15px;
            right:15px;
            width:380px;
            background:rgba(15,15,18,0.95);
            backdrop-filter:blur(10px);
            border:1px solid #2a2a2a;
            border-radius:12px;
            box-shadow:0 8px 30px rgba(0,0,0,0.4);
            z-index:999998;
            overflow:hidden;
            transition:all 0.3s ease;
        }
        #shihuoMainPanel.mini{
            width:160px;
            height:40px;
        }
        #shihuoPanelHead{
            display:flex;
            align-items:center;
            justify-content:space-between;
            padding:10px 14px;
            background:rgba(30,30,35,0.6);
            border-bottom:1px solid #2a2a2a;
            font-size:13px;
            color:#ccc;
        }
        #shihuoPanelHead .title{
            font-weight:600;
            color:#fff;
        }
        #shihuoToggleBtn{
            background:transparent;
            border:none;
            color:#888;
            cursor:pointer;
            font-size:16px;
        }
        #shihuoAuthBtn{
            margin:10px 14px;
            width:calc(100% - 28px);
            padding:8px 0;
            border:none;
            border-radius:6px;
            background:#1677ff;
            color:#fff;
            font-size:12px;
            cursor:pointer;
            transition:background 0.2s;
        }
        #shihuoAuthBtn:hover{
            background:#4096ff;
        }
        #shihuoAuthBtn.hide{
            display:none;
        }
        #shihuoLogWrap{
            height:280px;
            padding:10px 14px;
            overflow-y:auto;
            font-size:12px;
            line-height:1.7;
        }
        #shihuoLogWrap::-webkit-scrollbar{
            width:4px;
        }
        #shihuoLogWrap::-webkit-scrollbar-thumb{
            background:#333;
            border-radius:2px;
        }
        .log-info{color:#a0a0a0;}
        .log-succ{color:#52c41a;}
        .log-warn{color:#faad14;}
        .log-err{color:#ff4d4f;}
        .mini #shihuoLogWrap,.mini #shihuoAuthBtn{display:none;}
    `;
    document.head.appendChild(style);

    // ========== 创建面板UI ==========
    const panel = document.createElement('div');
    panel.id = 'shihuoMainPanel';
    panel.innerHTML = `
        <div id="shihuoPanelHead">
            <span class="title">识货高清图自动保存</span>
            <button id="shihuoToggleBtn">—</button>
        </div>
        <button id="shihuoAuthBtn">📁 首次设置保存根路径</button>
        <div id="shihuoLogWrap"></div>
    `;
    document.body.appendChild(panel);

    const logWrap = document.getElementById('shihuoLogWrap');
    const authBtn = document.getElementById('shihuoAuthBtn');
    const toggleBtn = document.getElementById('shihuoToggleBtn');

    // 折叠/展开面板
    toggleBtn.onclick = () => panel.classList.toggle('mini');

    // ========== 彩色日志 ==========
    function log(text, type = 'info') {
        const time = new Date().toLocaleTimeString();
        const line = document.createElement('div');
        line.className = `log-${type}`;
        line.innerText = `[${time}] ${text}`;
        logWrap.appendChild(line);
        logWrap.scrollTop = logWrap.scrollHeight;
    }

    // ========== 截取高清原图链接 ==========
    function getHighDefUrl(url) {
        if (!url) return '';
        const idx = url.indexOf('?imageView2/');
        return idx > 0 ? url.substring(0, idx) : url;
    }

    // ========== 绕过跨域获取图片Blob ==========
    function getImageBlob(url) {
        return new Promise(resolve => {
            GM_xmlhttpRequest({
                url,
                method: 'GET',
                responseType: 'blob',
                timeout: 15000,
                onload: res => resolve(res.response),
                onerror: () => resolve(null)
            });
        });
    }

    // ========== 保存单张图片到ID文件夹 ==========
    async function saveImgToFolder(url, idx) {
        if (!rootFolderHandle || saveUrlSet.has(url)) return;
        saveUrlSet.add(url);

        try {
            // 自动创建帖子ID文件夹
            const folder = await rootFolderHandle.getDirectoryHandle(POST_ID, { create: true });
            const blob = await getImageBlob(url);
            if (!blob) {
                log(`第${idx}张图片加载失败`, 'err');
                return;
            }
            // 写入文件
            const file = await folder.getFileHandle(`${idx}.jpg`, { create: true });
            const writer = await file.createWritable();
            await writer.write(blob);
            await writer.close();

            log(`已保存 → ${POST_ID}/${idx}.jpg`, 'succ');
        } catch (e) {
            log(`第${idx}张保存异常`, 'err');
        }
    }

    // ========== 扫描指定DOM提取图片 ==========
    async function scanDOM() {
        if (taskFinished) return;

        const dom = document.querySelector(TARGET_SELECTOR);
        if (!dom) {
            setTimeout(scanDOM, 1200);
            return;
        }

        const imgs = dom.querySelectorAll('img');
        if (imgs.length === 0) {
            setTimeout(scanDOM, 1200);
            return;
        }

        taskFinished = true;
        log(`已监控到 ${imgs.length} 张图片,开始提取高清原图`, 'warn');

        let index = 1;
        for (let img of imgs) {
            const realUrl = getHighDefUrl(img.src);
            if (realUrl) await saveImgToFolder(realUrl, index++);
        }

        log('✅ 全部图片保存完成', 'succ');
    }

    // ========== 授权路径 + 永久记住 ==========
    async function initAuth() {
        // 检测是否已经授权过
        const hasAuth = GM_getValue(AUTH_KEY, false);
        if (hasAuth && 'showDirectoryPicker' in window) {
            log('已读取历史保存路径,无需重新设置', 'succ');
            authBtn.classList.add('hide');
            // 等待页面就绪后扫描
            setTimeout(scanDOM, 1500);
            return;
        }

        // 首次授权
        authBtn.onclick = async () => {
            if (!window.showDirectoryPicker) {
                log('当前浏览器不支持本地文件夹授权', 'err');
                return;
            }
            try {
                rootFolderHandle = await window.showDirectoryPicker({ mode: 'readwrite' });
                // 标记永久授权
                GM_setValue(AUTH_KEY, true);
                authBtn.classList.add('hide');
                log('🎉 保存路径设置成功,已永久记住', 'succ');
                setTimeout(scanDOM, 1500);
            } catch (e) {
                log('已取消路径选择', 'warn');
            }
        };
    }

    // 初始化启动
    log('脚本已启动,等待页面加载...', 'info');
    log('当前帖子ID:' + POST_ID, 'info');
    initAuth();

})();


评论

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。