如何用JavaScript获取表格信息,通过Tampermonkey脚本选最低等级角色
Got it, let's break this down for your Tampermonkey script. The goal is to grab all characters from that table, find the one with the lowest level, then select their radio button. Here's a step-by-step solution tailored to your DOM snippet:
Step 1: Ensure the Script Runs After Content Loads
First, we need to make sure the script waits until the table is fully loaded. For static pages, a simple window.load listener works. If the table loads dynamically (like via AJAX), you might need a MutationObserver, but let's start with the basic case.
Step 2: Scrape Data & Find the Lowest Level Character
We'll target all <td> elements with classes box-select boxPk pad-10, then extract each character's level and their corresponding radio button. Here's the full script:
// ==UserScript== // @name Select Lowest Level Character // @namespace http://tampermonkey.net/ // @version 0.1 // @description Automatically selects the character with the lowest level // @author You // @match YOUR_TARGET_PAGE_URL_HERE // @grant none // ==/UserScript== (function() { 'use strict'; // Wait for the page to fully load window.addEventListener('load', function() { // Grab all character containers from the table const characterCells = document.querySelectorAll('td.box-select.boxPk.pad-10'); if (!characterCells.length) { console.log("No character entries found on the page!"); return; } let lowestLevel = Infinity; let targetRadio = null; // Loop through each character to find the lowest level characterCells.forEach(cell => { // --- Adjust this section to match your actual DOM structure --- // Example 1: If level is inside the <i> tag (like your snippet shows <i class="ion-...") // const levelText = cell.querySelector('i').textContent.trim(); // Example 2: If level is in the cell's text (e.g., "Robert - Level 3") const levelMatch = cell.textContent.match(/Level (\d+)/); if (!levelMatch) { console.log("Could not extract level from this cell:", cell); return; } const currentLevel = parseInt(levelMatch[1], 10); const radioButton = cell.querySelector('input[type="radio"][name="replacement"]'); // Update our target if this is the lowest level found so far if (currentLevel < lowestLevel && radioButton) { lowestLevel = currentLevel; targetRadio = radioButton; } }); // Click the radio button for the lowest level character if (targetRadio) { targetRadio.click(); console.log(`Selected character with level ${lowestLevel}`); } else { console.log("No valid radio button found for the lowest level character"); } }); })();
Key Adjustments for Your Site:
- Replace
YOUR_TARGET_PAGE_URL_HEREwith the actual URL of the page you're working on. - Tweak the level extraction logic: The example uses a regex to find "Level X" text, but if your level is in a specific element (like the
<i>tag in your snippet), swap out the level extraction code to target that element instead. - For dynamic content: If the table loads after the initial page load, replace the
window.loadlistener with a MutationObserver to watch for when the character cells are added to the DOM.
内容的提问来源于stack exchange,提问作者Billy Rammal

