我最近开始学习JS,并编写了一个脚本来计算Facebook组中的帖子数量。 我正在使用Chrome控制台执行此脚本。
问题在于,随着我加载越来越多的帖子,Chrome会随着时间的推移占用大量内存,直到崩溃为止。 (我曾尝试删除包含这些帖子的HTML,但这无济于事)
这是脚本:
async function counter(){
var postArr = document.getElementsByClassName('_4-u2 mbm _4mrt _5jmm _5pat _5v3q _7cqq _4-u8');
//creates an array with all the posts that are currently in the page
var counter = 0;
while(!(postArr.length === 0)){
var postArr = document.getElementsByClassName('_4-u2 mbm _4mrt _5jmm _5pat _5v3q _7cqq _4-u8');
counter = counter +postArr.length;
localStorage.setItem('Counter', counter);
document.querySelector('#u_0_x > span > span > input').value = counter;
await new Promise(r => setTimeout(r,2000));
var myNode = document.getElementById("group_mall_140579966013837");
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
//Deletes the used HTML
while (!(myNode.firstChild)){
window.scrollBy(0, 50000);
await new Promise(r => setTimeout(r,200));
window.scrollBy(0, -50000);
await new Promise(r => setTimeout(r,200));
window.scrollBy(0, 50000);
// waiting until the page is refreshed, and it starts over
}
}
}
您能帮我解决问题吗?如果您对代码有任何建议,请与我分享:D