Robert Vinsant
Robert Vinsant

Reputation: 1

Pages do not load using chrome console

Pages do not load using chrome console, when code is done executing last page will load

I would like to see the pages load as the code executes.

function pause(milliseconds) {
    dt = new Date();
    while ((new Date()) - dt <= milliseconds) { }
}

console.error ('Page 1');

window.location.href = "example.com/?page=2;
pause (1000);
console.error ('Page 2');
pause (1000);

window.location.href = "example.com/?page=3;
pause (1000);
console.error ('Page 3');
pause (1000);

I would like to start on page 1 open dev tools paste code, have the code take me to page 2 see it for a second and then page 3 so on for a couple hundred pages.

Upvotes: 0

Views: 1162

Answers (1)

blex
blex

Reputation: 25634

As said in the comments above, it's not possible to run a script in your developer console and let it run while you visit multiple pages. However, there are other ways to do it.

The one I'm showing you here is using the TamperMonkey Chrome extension. You can add it to your browser, and add the following script :

// ==UserScript==
// @name         URL looper
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Loop through an Array of URLs
// @match        *://*/*
// @grant unsafeWindow
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==

(function() {
    'use strict';

    const urls = [
        "https://stackoverflow.com/questions/tagged/javascript?page=1",
        "https://stackoverflow.com/questions/tagged/javascript?page=2",
        "https://stackoverflow.com/questions/tagged/javascript?page=3",
        "https://stackoverflow.com/questions/tagged/javascript?page=4",
        "https://stackoverflow.com/questions/tagged/javascript?page=5",
        "https://stackoverflow.com/questions/tagged/javascript?page=6"
    ];

    const delay = 1000;
    let timer;

    // Declare a global function which you can use in your console.
    // `unsafeWindow` is a way of accessing the page's `window` object from TamperMonkey
    unsafeWindow.MyLoop = {
        start: function() {
           // Set a global variable that will persist between page loads
           // and between multiple site domains
           GM_setValue('loopIsRunning', true);
           location.href = urls[0];
        },
        stop: function() {
           GM_setValue('loopIsRunning', false);
           clearTimeout(timer);
        }
    };

    if (GM_getValue('loopIsRunning')) {
        const currentIndex = urls.indexOf(location.href);
        if (currentIndex > -1 && currentIndex < urls.length - 1) {
            timer = setTimeout(function() {
                location.href = urls[currentIndex + 1];
            }, delay);
        } else if (currentIndex >= urls.length - 1) {
            unsafeWindow.MyLoop.stop();
        }
    }
})();

Save it, load any page, open your console, and you can now use these methods:

MyLoop.start();
MyLoop.stop();

Upvotes: 1

Related Questions