Line of Action Auto Downloader

automatically downloads reference pictures from Line of Action

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Line of Action Auto Downloader 
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  automatically downloads reference pictures from Line of Action
// @author       copilot&marco
// @match        https://line-of-action.com/practice-tools/app*
// @license MIT
// @grant        GM_download
// ==/UserScript==

(function() {
    'use strict';

    console.log("[LoA Downloader] 脚本已启动");

    const selector = ".overlay--image";
    const downloadedSet = new Set();

    function extractUrl(styleStr) {
        console.log("[LoA Downloader] 当前 style:", styleStr);
        const match = styleStr.match(/url\(["']?(.*?)["']?\)/);
        return match ? match[1] : null;
    }

    function downloadImage(url) {
        if (!url) return;
        if (downloadedSet.has(url)) {
            console.log("[LoA Downloader] 已下载过,跳过:", url);
            return;
        }
        downloadedSet.add(url);

        const filename = "LoA_" + Date.now() + "_" + url.split("/").pop();
        console.log("[LoA Downloader] 下载:", url, "保存为:", filename);

        GM_download({
            url: url,
            name: filename,
            onerror: err => console.error("[LoA Downloader] 下载失败:", err),
            onload: () => console.log("[LoA Downloader] 下载完成:", filename)
        });
    }

    function bindObserver() {
        const target = document.querySelector(selector);
        if (!target) {
            console.warn("[LoA Downloader] 未找到目标元素,1秒后重试...");
            setTimeout(bindObserver, 1000);
            return;
        }

        console.log("[LoA Downloader] 找到目标元素,开始监听");

        // 初始提取
        const initialUrl = extractUrl(target.getAttribute("style") || "");
        if (initialUrl) downloadImage(initialUrl);

        const observer = new MutationObserver(mutations => {
            for (const m of mutations) {
                if (m.type === "attributes" && m.attributeName === "style") {
                    const newUrl = extractUrl(target.getAttribute("style") || "");
                    if (newUrl) downloadImage(newUrl);
                }
            }
        });

        observer.observe(target, { attributes: true, attributeFilter: ["style"] });
    }

    window.addEventListener("load", () => {
        bindObserver();
    });

    window.addEventListener("hashchange", () => {
        console.log("[LoA Downloader] hash 变化,重新绑定观察器");
        bindObserver();
    });

})();