Chrome控制台脚本跳转页面后失效,如何自动返回页面A续跑?
Hey there! No worries at all—we all start somewhere, and this is a super common gotcha with page redirects and client-side scripts. Let’s break down what’s going wrong and fix it step by step.
The Core Problem
Your original approach relies on a setTimeout running in Page A’s context—but when the server redirects you to Page B, Page A’s entire execution environment gets destroyed. That means the timeout you set never gets to run, because the page it was attached to no longer exists.
Solution 1: Auto-Redirect from Page B (Most Reliable)
Since we can’t control Page A’s script after it’s been replaced by Page B, we need to add logic directly to Page B to send us back to A. The easiest way to do this (without modifying Page B’s source code) is using a browser user script (like Tampermonkey or Greasemonkey).
Here’s a user script that will trigger a redirect back to Page A 10 seconds after Page B loads:
// ==UserScript== // @name Auto-Back to Page A // @version 0.1 // @description Redirect from Page B to Page A automatically // @match https://your-domain.com/pageB // Replace with Page B's actual URL // @grant none // ==/UserScript== (function() { 'use strict'; // Wait 10 seconds, then jump back to Page A setTimeout(() => { window.location.href = "https://your-domain.com/pageA"; // Replace with Page A's URL }, 10000); })();
Solution 2: Fix Page A’s Script to Restart on Load
Once we’re back to Page A, we need to make sure your infinite loop script starts automatically again. We’ll also tweak it to either:
- Avoid the server redirect entirely by refreshing Page A proactively, or
- Clean up before a potential redirect so the restart is smooth.
Here’s the revised Page A script:
// Your original repeating function function functionA() { // Add your functionA logic here console.log("Running functionA..."); } function startInfiniteLoop() { // Run functionA immediately functionA(); // Set up the regular loop (adjust the interval to your needs) const loopInterval = setInterval(functionA, 1000); // Example: runs every 1 second // Option 1: Proactively refresh Page A before the server redirects (smoother) setTimeout(() => { clearInterval(loopInterval); // Clean up the loop first window.location.reload(); // Refresh Page A to avoid being sent to B }, 59 * 60 * 1000); // Trigger 59 minutes after page load } // Start the loop as soon as Page A finishes loading window.addEventListener('load', startInfiniteLoop);
Why This Works
- The user script runs directly in Page B’s context, so it’s not affected by Page A being unloaded.
- By using
window.addEventListener('load'), we ensure your loop restarts every time Page A loads—whether it’s the first visit, a refresh, or a redirect back from B. - Proactively refreshing Page A avoids the server redirect to B entirely, which makes the whole process seamless (no detour to B at all).
Quick Notes
- If you can’t use a user script (e.g., restricted browser), and Page B is on the same domain as A, you could use
localStorageto flag that you need to redirect back—but user scripts are far more reliable for cross-page control. - Adjust the timing values (like the 59-minute timeout or 10-second wait on B) to match your server’s exact redirect behavior.
内容的提问来源于stack exchange,提问作者ChanKim




