Save Gelbooru Images to Eagle

Save images from Gelbooru to Eagle.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name                Save Gelbooru Images to Eagle
// @name:zh             导入Gelbooru图片到Eagle
// @name:zh-CN          导入Gelbooru图片到Eagle
// @description         Save images from Gelbooru to Eagle.
// @description:zh      在Gelbooru网页上添加下载按钮直接导入Eagle,仅处理post
// @description:zh-CN   在Gelbooru网页上添加下载按钮直接导入Eagle,仅处理post

// @namespace           https://github.com/miracleXL
// @homepageURL         https://github.com/miracleXL/scripts-for-Eagle
// @icon                https://gelbooru.com/favicon.ico
// @version             0.1.2

// @author              miracleXL
// @match               gelbooru.com/index.php?page=post&s=view&id=*
// @connect             gelbooru.com
// @connect             localhost
// @grant               GM_xmlhttpRequest
// @require             https://code.jquery.com/jquery-3.5.1.min.js
// ==/UserScript==


(function() {
    'use strict';
    // 是否保存标签
    const saveTags = true;
    const saveMetadataTags = false;

    // Eagle API 服务器位置
    const EAGLE_SERVER_URL = "http://localhost:41595";
    const EAGLE_IMPORT_API_URL = `${EAGLE_SERVER_URL}/api/item/addFromURL`;
    const EAGLE_IMPORT_API_URLS = `${EAGLE_SERVER_URL}/api/item/addFromURLs`;
    const EAGLE_CREATE_FOLDER_API_URL = `${EAGLE_SERVER_URL}/api/folder/create`;
    const EAGLE_GET_FOLDERS_API_URL = `${EAGLE_SERVER_URL}/api/folder/list`;

    const BUTTON_POS = "scrollebox";

    function download(data){
        GM_xmlhttpRequest({
            url: EAGLE_IMPORT_API_URL,
            method: "POST",
            data: JSON.stringify(data),
            onload: function(response) {
                if(response.statusText !== "OK"){
                    console.log(response);
                    alert("下载失败!")
                }
            }
        });
    }

    function addButton(buttons_div){
        buttons_div.append(" | ");
        let button = document.createElement("a");
        button.innerText = "Download to Eagle";
        button.href = "javascript:;";
        buttons_div.appendChild(button);
        //绑定下载事件
        button.addEventListener("click",async ()=>{
            let data = getImageData();
            console.log(data);
            download(data);
        })
    }

    addButton(document.getElementById(BUTTON_POS));

    function getImageData(){
        let tag_list = document.getElementById("tag-list");
        let url = $("li a:contains(Original image)", tag_list)[0];
        let source = $("li:contains(Source: )", tag_list)[0];
        let data = {
            "url": url.href,
            "name": document.title,
            "website": document.URL,
            "annotation": source?.textContent,
            "tags": [],
            "headers": {
                "referer" : document.URL
            }
        };
        if(saveTags){
            for(let tag of document.getElementsByClassName("tag-type-artist")){
                data.tags.push(tag.children[1].textContent);
            }
            for(let tag of document.getElementsByClassName("tag-type-character")){
                data.tags.push(tag.children[1].textContent);
            }
            if(saveMetadataTags){
                for(let tag of document.getElementsByClassName("tag-type-metadata")){
                    data.tags.push(tag.children[1].textContent);
                }
            }
            for(let tag of document.getElementsByClassName("tag-type-copyright")){
                data.tags.push(tag.children[1].textContent);
            }
            for(let tag of document.getElementsByClassName("tag-type-general")){
                data.tags.push(tag.children[1].textContent);
            };
        }
        return data;
    };
})();