请求开发JavaScript脚本:提取input值生成URL直至值为-1并打开
I’ve put together a couple of practical solutions to automate this process—one you can run directly in your browser’s console for quick use, and a seamless userscript that handles navigation automatically.
Option 1: Console Script (Quick Manual Execution)
This script checks the current page, navigates to the next page if needed, and you can re-run it on each new page until you hit the final URL.
// Configure these values to match your forum's setup const INPUT_NAME = "nextPage"; // Name attribute of the input element to target const BASE_URL = "https://your-forum-domain.com/thread?page="; // Base URL pattern (append the input value to this) function checkAndNavigate() { // Find the input element by its name attribute const nextInput = document.querySelector(`input[name="${INPUT_NAME}"]`); if (!nextInput) { alert(`Couldn't find input with name "${INPUT_NAME}"!`); return; } const nextValue = nextInput.value.trim(); if (nextValue === "-1") { alert(`Final URL found: ${window.location.href}`); // Optional: Uncomment below to open the final URL in a new tab // window.open(window.location.href, "_blank"); return; } // Build the next page URL and navigate const nextUrl = `${BASE_URL}${nextValue}`; console.log(`Navigating to: ${nextUrl}`); window.location.href = nextUrl; } // Run the function immediately checkAndNavigate();
How to use:
- Open your browser’s developer tools (F12 or Ctrl+Shift+I)
- Switch to the "Console" tab
- Paste the script, update
INPUT_NAMEandBASE_URLto match your forum’s structure - Press Enter to run it. Repeat this step on each new page until you reach the final URL.
Option 2: Tampermonkey Userscript (Hands-Off Automatic Navigation)
For a fully automated experience, use a userscript manager like Tampermonkey to run the script every time you load a forum page. This handles all navigations without manual input.
// ==UserScript== // @name Forum Final Page Auto-Navigator // @namespace http://tampermonkey.net/ // @version 0.1 // @description Automatically navigates to the final forum page using the nextPage input value // @author Your Name // @match https://your-forum-domain.com/* // Replace with your forum's URL pattern // @grant none // ==/UserScript== (function() { 'use strict'; // Configuration const INPUT_NAME = "nextPage"; const BASE_URL = "https://your-forum-domain.com/thread?page="; function navigateToFinal() { const nextInput = document.querySelector(`input[name="${INPUT_NAME}"]`); if (!nextInput) { console.log(`Input "${INPUT_NAME}" not found—stopping.`); return; } const nextValue = nextInput.value.trim(); if (nextValue === "-1") { console.log(`Final page reached: ${window.location.href}`); return; } const nextUrl = `${BASE_URL}${nextValue}`; console.log(`Auto-navigating to next page: ${nextUrl}`); window.location.href = nextUrl; } // Run when the page is fully loaded window.addEventListener('load', navigateToFinal); })();
How to use:
- Install Tampermonkey (or another userscript manager) for your browser
- Create a new userscript
- Replace the template code with the script above
- Update the
@matchline to match your forum’s URL (e.g.,https://forum.example.com/threads/*) - Adjust
INPUT_NAMEandBASE_URLto fit your forum’s setup - Save the script—it will now run automatically whenever you load a matching forum page.
Key Notes:
- Double-check the
BASE_URLto ensure it matches your forum’s next-page pattern (e.g., if the URL uses?threadId=instead of?page=, update accordingly). - If the input element uses a different name (not "nextPage"), modify the
INPUT_NAMEvariable to match. - The script stops and notifies you when it finds the input with value
-1—that’s your final URL.
内容的提问来源于stack exchange,提问作者Double Shadow
相关产品推荐
相关产品推荐

