gpt4 book ai didi

javascript - 无法使用 Chrome 控制台加载页面

转载 作者:行者123 更新时间:2023-12-03 00:29:29 25 4
gpt4 key购买 nike

页面无法使用 Chrome 控制台加载,当代码执行完毕后,将加载最后一页

我希望看到页面在代码执行时加载。

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);

我想从第 1 页开始打开开发工具粘贴代码,让代码带我到第 2 页查看一秒钟,然后到第 3 页,依此类推几百页。

最佳答案

正如上面的评论所述,不可能在开发者控制台中运行脚本并让它在您访问多个页面时运行。但是,还有其他方法可以做到这一点。

我在这里向您展示的是使用 TamperMonkey Chrome 扩展程序。您可以将其添加到浏览器中,并添加以下脚本:

// ==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();
}
}
})();

保存它,加载任何页面,打开控制台,然后您现在可以使用以下方法:

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

关于javascript - 无法使用 Chrome 控制台加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53935738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com