diff --git a/README.md b/README.md index 9e42e3e..bc05353 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ This is a script that quickly redirects to corresponding Steam games on the HumbleBundle website. +--- +## 安装 +[点击安装 addSteamLink.user.js](https://github.com/mookechee/HB-RapidSteamSearchJS/raw/main/addSteamLink.user.js) + +需要先安装 Tampermonkey、Violentmonkey 或其他兼容的 userscript 管理器。 + --- ## 使用后效果 点击标题处的超链接即可跳转steam搜索界面 diff --git a/addSteamLink.user.js b/addSteamLink.user.js index 1896914..27c59dc 100644 --- a/addSteamLink.user.js +++ b/addSteamLink.user.js @@ -12,31 +12,21 @@ // @license MIT // @homepage https://github.com/mookechee/HB-RapidSteamSearchJS // @supportURL https://github.com/mookechee/HB-RapidSteamSearchJS/issues -// @updateURL https://github.com/mookechee/HB-RapidSteamSearchJS/raw/main/addSteamLink.js -// @downloadURL https://github.com/mookechee/HB-RapidSteamSearchJS/raw/main/addSteamLink.js +// @updateURL https://github.com/mookechee/HB-RapidSteamSearchJS/raw/main/addSteamLink.user.js +// @downloadURL https://github.com/mookechee/HB-RapidSteamSearchJS/raw/main/addSteamLink.user.js // @run-at document-end // ==/UserScript== (function() { const CONFIG = { - initialDelay: 500, // 初始等待500ms - retryInterval: 300, // 每300ms检查一次 - maxRetries: 5 // 最多重试5次 + initialDelay: 500 // 初始等待500ms }; - let retryCount = 0; + const selector = 'h1[data-entity-kind="product"][data-anchor-name="#human_name"].human_name-view'; function initialize() { - const selector = 'h1[data-entity-kind="product"][data-anchor-name="#human_name"].human_name-view'; - const elements = document.querySelectorAll(selector); - - if (elements.length > 0) { - processElements(elements); - startMutationObserver(); - } else if (retryCount < CONFIG.maxRetries) { - retryCount++; - setTimeout(initialize, CONFIG.retryInterval); - } + processElements(document.querySelectorAll(selector)); + startMutationObserver(); } function processElements(elements) { @@ -58,9 +48,13 @@ const observer = new MutationObserver(mutations => { mutations.forEach(mutation => { mutation.addedNodes.forEach(node => { - if (node.nodeType === 1 && node.matches('h1[data-entity-kind="product"]')) { + if (node.nodeType !== Node.ELEMENT_NODE) return; + + if (node.matches(selector)) { processElements([node]); } + + processElements(node.querySelectorAll(selector)); }); }); });