Extract_Gift_link_Indiegala

Indiegala礼物链接提取

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name:zh-CN         IG礼物链接提取
// @name               Extract_Gift_link_Indiegala
// @namespace          https://blog.chrxw.com/
// @version            0.8
// @description:zh-CN  Indiegala礼物链接提取
// @description        Indiegala礼物链接提取
// @author             Chr_
// @license            AGPL-3.0
// @icon               https://blog.chrxw.com/favicon.ico
// @match              https://www.indiegala.com/library
// @grant              GM_setClipboard
// ==/UserScript==

(() => {
    "use strict";

    let GObjs = {};

    addbtn();
    function addbtn() {
        let area = document.querySelector("div.profile-private-page-user");
        let dv1 = document.createElement("div");
        let dv2 = document.createElement("div");
        let dv3 = document.createElement("div");
        let btnExtractGift = document.createElement("button");
        let btnExtractKey = document.createElement("button");
        let btnCopy = document.createElement("button");
        let btnClear = document.createElement("button");
        let txtResult = document.createElement("textarea");
        dv1.style.cssText = "margin: 12px 0;display: flex;";
        dv2.style.cssText = "margin: 0 12px;display: block;";
        dv3.style.cssText = "margin: 0 12px;display: block;";
        btnExtractGift.addEventListener("click", extractGift);
        btnExtractKey.addEventListener("click", extractKey);
        btnCopy.addEventListener("click", copy);
        btnClear.addEventListener("click", clear);
        btnExtractGift.style.cssText = "display: inherit;";
        btnClear.style.cssText = "float: right;";
        btnExtractGift.textContent = "提取礼物链接";
        btnExtractKey.textContent = "提取Key";
        btnCopy.textContent = "复制";
        btnClear.textContent = "×";
        btnCopy.id = "btnCopy";
        txtResult.style.cssText = "width: 70%;white-space: nowrap;";
        txtResult.id = "extractLinks";
        dv2.appendChild(btnExtractGift);
        dv2.appendChild(btnExtractKey);
        dv3.appendChild(btnCopy);
        dv3.appendChild(btnClear);
        dv1.appendChild(dv2);
        dv1.appendChild(txtResult);
        dv1.appendChild(dv3);
        area.appendChild(dv1);
        Object.assign(GObjs, { txtResult, btnCopy });
    }
    function extractGift() {
        const { txtResult } = GObjs;
        let gifts = document.querySelectorAll("div[ref=bundle] ul.profile-private-page-library-sublist-active div.profile-private-page-library-gifts div.profile-private-page-library-gift-title > div.overflow-auto");
        if (gifts.length > 0) {
            let list = [];
            let old = txtResult.value;
            for (let gift of gifts) {
                let giftLink = gift.querySelector("a").href;
                let giftPass = gift.querySelector("div:last-child>span").textContent;
                if (old.indexOf(giftLink.substring(38,)) >= 0) {
                    console.log(`重复的礼物链接 ${giftLink.substring(38,)}`);
                    continue;
                }
                list.push(`IG慈善包链接:( ${giftLink} )IG慈善包密码:( ${giftPass} )`);
            }
            if (list.length > 0) {
                if (txtResult.value !== "") {
                    txtResult.value += "\n";
                }
                txtResult.value += list.join("\n");
            }
        } else {
            alert("未找到可识别的礼物链接");
        }
        copy();
    }
    function extractKey() {
        const { txtResult } = GObjs;
        let cols = document.querySelectorAll("ul.profile-private-page-library-sublist-active div.profile-private-page-library-key-cont.overflow-auto");
        if (cols.length > 0) {
            let list = [];
            let old = txtResult.value;
            for (let col of cols) {
                const gameName = col.querySelector("div.profile-private-page-library-title-row-full")?.title ?? "";
                const gameKey = col.querySelector("input")?.value ?? "";

                if (old.indexOf(gameKey) >= 0) {
                    console.log(`重复的key ${giftLink.substring(38,)}`);
                    continue;
                }
                list.push(`${gameName}  ${gameKey}`);
            }
            if (list.length > 0) {
                if (txtResult.value !== "") {
                    txtResult.value += "\n";
                }
                txtResult.value += list.join("\n");
            }
        } else {
            alert("未找到可识别的Key信息");
        }
        copy();
    }
    function copy() {
        const { btnCopy, txtResult } = GObjs;
        GM_setClipboard(txtResult.value, "text");
        btnCopy.textContent = "已复制";
        setTimeout(() => { btnCopy.textContent = "复制"; }, 1000);
    }
    function clear() {
        const { txtResult } = GObjs;
        if (confirm("确定要清空吗?")) {
            txtResult.value = "";
        }
    }
})();