Greasy Fork is available in English.

JLCJLCJLCJLCJLC

上立创整点LDO

// ==UserScript==
// @name         JLCJLCJLCJLCJLC
// @namespace    http://tampermonkey.net/
// @version      2024-04-10-4
// @description  上立创整点LDO
// @author       You
// @match        https://www.szlcsc.com/huodong.html*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @grant        GM_getResourceText
// @license      GPLv3
// ==/UserScript==

(function () {
    const style = `
#ext-root {
    position: fixed;
    top: 111px;
    right: 20px;
    width: 370px;
    max-height: 80vh;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 9999;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: white;
    font-size: larger;
}`
    'use strict';
    const sleep = v => new Promise(r => setTimeout(r, v))
    const rootElem = document.createElement("div")
    rootElem.id = "ext-root"
    GM_addStyle(style)
    let coupons = [];
    document.querySelectorAll(".coupon-item").forEach(v => {
        if (v.querySelector(".condition").innerText === "满16可用" &&
            !v.querySelector(".coupon-item-name").innerText.startsWith("<新人专享>")) {
            let elem = v.querySelector(".coupon-item-btn-text")
            coupons.push({
                root: v,
                button: elem,
                url: v.firstElementChild.getAttribute("data-url"),
            })
        }
    })
    const span = document.createElement("span");
    const baseText = `共找到${coupons.length}张优惠券`
    span.innerText = baseText
    rootElem.appendChild(span)
    const button = document.createElement("button")
    button.innerText = "开始领取&预览页面"

    async function getCoupon() {
        for (let i = 0; i < coupons.length; i++) {
            const v = coupons[i];
            if (v.button.innerText === "立即抢券") {
                v.button.click();
                await sleep(1500)
            }
            span.innerText = baseText + `已抢${i + 1}张优惠券`
        }
    }

    button.onclick = async () => {
        button.onclick = undefined
        console.log("clicked")
        getCoupon() // no await
        const parser = new DOMParser();
        const newWindow = window.open("", "_blank").document.body
        newWindow.classList.add("page-brand")
        const brandCss = document.createElement("link")
        brandCss.rel = "stylesheet"
        brandCss.href = "https://static.szlcsc.com/ecp/public/css/page/list/brand/brand.02b0b6e2.css"
        const custonCSS = document.createElement("style")
        custonCSS.innerHTML = `
        body {
            padding-top: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .block-brand {
            width: 1200px;
        }
        `
        newWindow.appendChild(custonCSS)
        newWindow.appendChild(brandCss)
        for (let i = 0; i < coupons.length; i++) {
            const coupon = coupons[i]
            await new Promise(r => {
                console.log(coupon.url)
                GM_xmlhttpRequest({
                    method: "GET",
                    url: coupon.url,
                    onload: function (response) {
                        const doc = parser.parseFromString(response.responseText, "text/html");
                        const blk = document.createElement("div")
                        blk.classList.add("block-brand")
                        const description = doc.querySelector("meta[name=description]").getAttribute("content")
                        const a = document.createElement("a")
                        a.href = coupon.url
                        a.target = "_blank"
                        a.innerText = description
                        blk.appendChild(a)
                        const brand_info = doc.querySelector(".brand-info")
                        blk.appendChild(brand_info)
                        const det_screen = doc.querySelector(".det-screen-wrapper")
                        try {
                            let i = response.responseText.indexOf("window.filterData = ")
                            let j = response.responseText.indexOf("};", i)
                            const text = response.responseText.slice(i + 19, j + 1)
                            let data = eval("(" + text + ")")
                            let cateList = det_screen.querySelector("#CategoryList")
                            cateList.parentElement.classList.add("det-screen-height")
                            cateList.style.height = 'auto'
                            cateList.innerHTML = ""
                            for (const cate of data.category_list) {
                                let elem = document.createElement("div")
                                elem.style.height = '18px';
                                let span = document.createElement("span")
                                span.innerText = cate.label
                                elem.appendChild(span)
                                cateList.appendChild(elem)
                            }

                            let standardList = det_screen.querySelector("#standardList")
                            standardList.parentElement.classList.add("det-screen-height")
                            standardList.style.height = 'auto'
                            standardList.innerHTML = ""
                            for (const cate of data.standard_list) {
                                let elem = document.createElement("div")
                                elem.style.height = '18px';
                                let span = document.createElement("span")
                                span.innerText = cate.label
                                elem.appendChild(span)
                                standardList.appendChild(elem)
                            }
                        } catch (e) {
                            console.error(e)
                        }
                        blk.appendChild(det_screen)
                        newWindow.appendChild(blk)
                        r()
                    }
                });
            });
        }
    }
    rootElem.appendChild(button)
    document.body.appendChild(rootElem)
})();