Javascript 不使用chrome控制台加载页面
不使用chrome控制台加载页面,当代码完成时,将加载最后一个页面 我希望看到页面在代码执行时加载Javascript 不使用chrome控制台加载页面,javascript,google-chrome-devtools,Javascript,Google Chrome Devtools,不使用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); consol
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);
功能暂停(毫秒){
dt=新日期();
虽然((new Date())-dt如上面的评论所述,在开发人员控制台中运行脚本并让它在您访问多个页面时运行是不可能的。但是,还有其他方法可以做到这一点
我在这里向您展示的是使用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();
}
}
})();
Chrome控制台不会在您执行它的页面之外运行。因此,当它离开页面时,您的javascript和正在执行的javascript将被丢弃。@KyleShrader是对的。如果您在页面上执行脚本,一旦离开,它将被销毁。类似Chrome的扩展可以帮助您实现这一点(你可以有一个URL数组,并通过某种循环来遍历它们)。如果你喜欢NodeJSSelenium,你还可以使用它来自动控制你的浏览器。如果你更习惯使用其他语言,它是一个很好的多语言解决方案。
MyLoop.start();
MyLoop.stop();