无法使用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页,如此反复几百页。
答案 0 :(得分:1)
正如上面的评论中所述,无法在开发者控制台中运行脚本并在您访问多个页面时使其运行。但是,还有其他方法可以实现。
我在这里向您展示的是使用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();