// ==UserScript== // @name:zh-CN HB截图助手 // @name HB_Screenshots // @namespace https://blog.chrxw.com // @supportURL https://blog.chrxw.com/scripts.html // @contributionURL https://afdian.com/@chr233 // @version 3.1 // @description 一键生成密钥截图 // @description:zh-CN 一键生成密钥截图 // @author Chr_ // @license AGPL-3.0 // @icon https://blog.chrxw.com/favicon.ico // @require https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.all.min.js // @require https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.js // @include https://www.humblebundle.com/home/library/* // @grant GM_setClipboard // @grant GM_addStyle // ==/UserScript== (() => { "use strict"; // 初始化 let GTimer = 0; GM_addStyle(` div.hb_panel_out { position: fixed; left: 10px; top: 10px; z-index: 200; display: flex; font-size: small; } div.hb_panel_out>*:not(:first-child){ margin-left: 10px; } div.hb_panel { background-color: #1d283a; display: flex; border-radius: 5px; } div.hb_cnv { max-height: 200px; overflow: scroll; } button.hb_sc { position: relative; left: -50px; top: 0; } button.hb_control { background-color: #7339db; border-radius: 5px; padding: 10px; margin: 5px; } `); window.addEventListener("load", () => { setTimeout(() => { addGUI(); }, 1000); GTimer = setInterval(() => { waitLoading(); }, 5000); }); function addGUI() { function genDiv(cls) { const d = document.createElement("div"); d.className = cls; return d; } function genBtn(txt, foo) { const b = document.createElement("button"); b.className = "hb_control"; b.textContent = txt; b.addEventListener("click", foo); return b; } const divBtns = genDiv("hb_panel_out"); document.body.appendChild(divBtns); const div1 = genDiv("hb_panel"); divBtns.appendChild(div1); const btnScraper = genBtn("刮Key", scraperKeys); div1.appendChild(btnScraper); const btnFix = genBtn("修复Key",()=> fixKey(false)); div1.appendChild(btnFix); const btnGenImg = genBtn("截图", genImage); div1.appendChild(btnGenImg); const div2 = genDiv("hb_panel"); divBtns.appendChild(div2); const btnCopyTxt = genBtn("纯文本", copyTxt); div2.appendChild(btnCopyTxt); const btnCopyCSV = genBtn("CSV", copyCSV); div2.appendChild(btnCopyCSV); const btnCopyMD = genBtn("Markdown", copyMD); div2.appendChild(btnCopyMD); const btnCopyHTML = genBtn("HTML表格", copyHTML); div2.appendChild(btnCopyHTML); const div3 = genDiv("hb_panel"); divBtns.appendChild(div3); const btnAbout = genBtn("关于", showAbout); div3.appendChild(btnAbout); } function waitLoading() { if (document.querySelector("div.key-list h4") != null) { clearInterval(GTimer); addRemover(); } } function addRemover() { function genBtn(ele) { const b = document.createElement("button"); b.className = "hb_sc"; b.addEventListener("click", () => { ele.innerHTML = ""; }); b.textContent = "×"; b.title = "隐藏Key(刷新恢复)"; return b; } const keys = document.querySelectorAll("div.key-list>div"); if (keys) { for (let ele of keys) { const btn = genBtn(ele); ele.appendChild(btn); } } } function scraperKeys() { const btns = document.querySelectorAll("div.flex.flex-col.gap-4 button.preserve-brand-colors"); if (btns) { let i = 0; let t = setInterval(() => { if (i < btns.length) { const btn = btns[i++]; if (btn.querySelector("svg") || btn.querySelector("span")) { console.info(btn.textContent) } else { btn.click(); console.warn(btn.textContent) } } else { clearInterval(t); showAlert("", "刮开完成, 即将自动刷新", "success", 1000); setTimeout(() => { window.location.reload(); }, 800); } }, 1000); } else { showAlert("", "没有检测到可刮的Key", "warning", 1000); } } function fixKey(skipAlert = false) { const warpers = document.querySelectorAll("div[class*='sm:max-w-[248px]']"); for (let w of warpers) { w.classList.remove("sm:max-w-[248px]"); } if (!skipAlert) { showAlert("", "修复成功", "success", 1000); } } async function genImage() { window.stop(); showAlert("", "图片生成中...", "info", 1000); fixKey(true); const Start = new Date().getTime(); const steam = document.querySelector(".sr-user"); const btns = document.querySelectorAll("div.bg-card.flex.flex-col"); if (btns || btns.length > 0) { if (steam) { steam.style.display = "none"; } const keyArea = document.querySelector("div.flex.flex-col.gap-4"); const canvas = await snapdom.toCanvas(keyArea); if (steam) { steam.style.display = ""; } const End = new Date().getTime(); Swal.fire({ text: `图片生成完成, 右键图片复制, 耗时 ${End - Start} ms`, imageUrl: canvas.toDataURL("image/png"), width: 1000, imageAlt: "Key 截图" }); } else { showAlert("", "Key列表为空?\n或许是卡DOM了, 刷新一下即可", "warning", "Ok", null, 1000); } } function parseKeys() { const data = []; const keys = document.querySelectorAll("div.bg-card.flex.flex-col"); if (keys) { for (let key of keys) { const title = key.querySelector("div.text-accent"); const keyStr = key.querySelector("button>span.min-w-0.shrink.truncate"); if (title && keyStr) { data.push([title.textContent.trim(), keyStr.textContent]); } else { console.log(title, keyStr); } } } return data; } function copyTxt() { const data = parseKeys(); const list = []; for (let [title, key] of data) { list.push(`${title} ${key}`); } setClipboard(list.join("\n"), "text"); showAlert("", "复制成功", "success", 1000); } function copyCSV() { const data = parseKeys(); const list = ["游戏名, Key"]; for (const [title, key] of data) { list.push(`${title}, ${key}`); } setClipboard(list.join("\n"), "text"); showAlert("", "复制成功", "success", 1000); } function copyMD() { const data = parseKeys(); const list = ["| 游戏名 | Key |", "| --- | --- |"]; for (const [title, key] of data) { list.push(`| ${title} | ${key} |`); } setClipboard(list.join("\n"), "text"); showAlert("", "复制成功", "success", 1000); } function copyHTML() { const data = parseKeys(); const tdCss = "style=\"padding:5px 10px;border-top:1px solid;\""; const list = [ "
| 游戏名 | Key |
|---|---|
| ${title} | ${key} |