admin管理员组文章数量:1022452
I have a code that i want to put in chrome console
var a = document.getElementsByClassName("dispo");
if (a.length > 0) {
for (let i = 0; i < a.length ; i++) {
if (a[i].textContent.length > 0) {
console.log(a[i].parentElement.textContent.substr(0,10) + " - " + a[i].textContent);
}
}
} else {
setInterval(function(){ document.location.reload() },60000);
}
The function above gets some data from the website, but if it didn't find the data i want it to reload every minute until the data is available.
I want to insert the code only once and leave the browser working.
So how to run the function every time i reload the page ?
I have a code that i want to put in chrome console
var a = document.getElementsByClassName("dispo");
if (a.length > 0) {
for (let i = 0; i < a.length ; i++) {
if (a[i].textContent.length > 0) {
console.log(a[i].parentElement.textContent.substr(0,10) + " - " + a[i].textContent);
}
}
} else {
setInterval(function(){ document.location.reload() },60000);
}
The function above gets some data from the website, but if it didn't find the data i want it to reload every minute until the data is available.
I want to insert the code only once and leave the browser working.
So how to run the function every time i reload the page ?
- 1 One way is put it in a Tampermonkey userscript – charlietfl Commented Jul 2, 2018 at 22:53
-
1
Or use
XMLHttpRequest
andDOMParser
and don't reload the page at all. – ibrahim mahrir Commented Jul 2, 2018 at 23:09 - 1 If you reload the page, setInterval will be no longer working as it looks like. – Liang Commented Jul 2, 2018 at 23:09
- How to do that exactly ? – nadhirxz Commented Jul 2, 2018 at 23:10
2 Answers
Reset to default 3You can change your code to not reload the page every time but instead request it via XMLHttpRequest
. You can then parse the response into a document using DOMParser
:
function request(callback) { // request will request the page content as text (without reloading)
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://your-url-here");
xhr.onload = function() {
callback(xhr.response);
};
xhr.send();
}
function next() { // next will be called each time instead of reloading
request(function(response) { // first we request the page
var doc = new DOMParser().parseFromString(response, "text/html"); // then we parse it as a document
var a = doc.getElementsByClassName("dispo"); // use doc instead of document (doc will be the newly requested document/page)
if (a.length > 0) {
for (let i = 0; i < a.length ; i++) {
if (a[i].textContent.length > 0) {
console.log(a[i].parentElement.textContent.substr(0,10) + " - " + a[i].textContent);
}
}
} else {
setTimeout(next, 60000); // if we didn't find anything, then call next after a minute
}
});
}
next();
Notes:
- First make sure you're currently on that page so you won't get CORS error.
- If the url has parameters, you should send those as a form.
As per your code, you are waiting for an async function that will create an element with a "dispo" classname. Then when it is loaded, you're gonna do something. If it isn't, you will check it in 1 minute.
Try below code
const checkDispo=()=>{
var a = document.getElementsByClassName("dispo");
if (a.length > 0) {
clearInterval(intv);
for (let i = 0; i < a.length ; i++) {
if (a[i].textContent.length > 0) {
console.log(a[i].parentElement.textContent.substr(0,10) + " - " + a[i].textContent);
}
}
}
}
const intv=setInterval(checkDispo,60000);
checkDispo();
I have a code that i want to put in chrome console
var a = document.getElementsByClassName("dispo");
if (a.length > 0) {
for (let i = 0; i < a.length ; i++) {
if (a[i].textContent.length > 0) {
console.log(a[i].parentElement.textContent.substr(0,10) + " - " + a[i].textContent);
}
}
} else {
setInterval(function(){ document.location.reload() },60000);
}
The function above gets some data from the website, but if it didn't find the data i want it to reload every minute until the data is available.
I want to insert the code only once and leave the browser working.
So how to run the function every time i reload the page ?
I have a code that i want to put in chrome console
var a = document.getElementsByClassName("dispo");
if (a.length > 0) {
for (let i = 0; i < a.length ; i++) {
if (a[i].textContent.length > 0) {
console.log(a[i].parentElement.textContent.substr(0,10) + " - " + a[i].textContent);
}
}
} else {
setInterval(function(){ document.location.reload() },60000);
}
The function above gets some data from the website, but if it didn't find the data i want it to reload every minute until the data is available.
I want to insert the code only once and leave the browser working.
So how to run the function every time i reload the page ?
- 1 One way is put it in a Tampermonkey userscript – charlietfl Commented Jul 2, 2018 at 22:53
-
1
Or use
XMLHttpRequest
andDOMParser
and don't reload the page at all. – ibrahim mahrir Commented Jul 2, 2018 at 23:09 - 1 If you reload the page, setInterval will be no longer working as it looks like. – Liang Commented Jul 2, 2018 at 23:09
- How to do that exactly ? – nadhirxz Commented Jul 2, 2018 at 23:10
2 Answers
Reset to default 3You can change your code to not reload the page every time but instead request it via XMLHttpRequest
. You can then parse the response into a document using DOMParser
:
function request(callback) { // request will request the page content as text (without reloading)
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://your-url-here");
xhr.onload = function() {
callback(xhr.response);
};
xhr.send();
}
function next() { // next will be called each time instead of reloading
request(function(response) { // first we request the page
var doc = new DOMParser().parseFromString(response, "text/html"); // then we parse it as a document
var a = doc.getElementsByClassName("dispo"); // use doc instead of document (doc will be the newly requested document/page)
if (a.length > 0) {
for (let i = 0; i < a.length ; i++) {
if (a[i].textContent.length > 0) {
console.log(a[i].parentElement.textContent.substr(0,10) + " - " + a[i].textContent);
}
}
} else {
setTimeout(next, 60000); // if we didn't find anything, then call next after a minute
}
});
}
next();
Notes:
- First make sure you're currently on that page so you won't get CORS error.
- If the url has parameters, you should send those as a form.
As per your code, you are waiting for an async function that will create an element with a "dispo" classname. Then when it is loaded, you're gonna do something. If it isn't, you will check it in 1 minute.
Try below code
const checkDispo=()=>{
var a = document.getElementsByClassName("dispo");
if (a.length > 0) {
clearInterval(intv);
for (let i = 0; i < a.length ; i++) {
if (a[i].textContent.length > 0) {
console.log(a[i].parentElement.textContent.substr(0,10) + " - " + a[i].textContent);
}
}
}
}
const intv=setInterval(checkDispo,60000);
checkDispo();
版权声明:本文标题:javascript - How to keep the function in chrome console and run it after reloading the page? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745531041a2154750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论