Chrome Console Script broken: Uncaught TypeError: Cannot read properties of undefined (reading 'push')

594 views Asked by At

I'm using the following code on Instagram follow request page to open each user in a new tab and then unfollow it.

https://www.instagram.com/accounts/access_tool/current_follow_requests

 var i=0;  
 var unfollow="global";  
 var final="global";  
 var link=["link","link2"];  
 var proWindow=[""]  
 proWindow.length=0  
 link.length=0;  
 var ids = document.querySelectorAll(".-utLf");  
 for(i=0;i<ids.length;i++){  
   link.push('https://www.instagram.com/'+ids[i].innerText+'/');  
      console.log(link[i]);  
      proWindow[i]=window.open(link[i]);  
 }  

and then the following code to unfollow each user:

for(i=0;i<ids.length;i++){
 unfollow = proWindow[i].document.querySelector("button.sqdOP");
 unfollow.click();
 await new Promise(resolve => setTimeout(resolve, 1000));
 final = proWindow[i].document.querySelector(".aOOlW");
 final.click();}
console.log("Completed");  

however, I'm seeing the following error on the console Uncaught TypeError: Cannot read properties of undefined (reading 'push')

This preivously used to work fine but now is showing this error.

What change needs to be done to fix this code?

1

There are 1 answers

0
Andreas Aanestad On

This error originates from trying to push something undefined to an array. That could happend if the nodelist returned by the queryselector contains something with an undefined innertext for some reason.

This should work better:

if (ids[i].innerText){ // To check if the value is undefined
    link.push('https://www.instagram.com/'+ids[i].innerText+'/');  
    console.log(link[i]);  
    proWindow[i]=window.open(link[i]);    
}