0

I hope somebody will help me to fix the issue.

I have a follow script for chrome console , it worked fine the last couple of days , however running it today I started to receive this Error ( below ) . I would highly appreciate if somebody can help me fix the code issue , the code and the error is attached below . Thanks in advance for help.

var jq = document.createElement('script');
jq.src = "//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"; //Loads JavaScript
document.getElementsByTagName('head')[0].appendChild(jq);
jQuery.noConflict()

window.setInterval(function(){
var x = 0;
$("button:contains('Follow')").each(
function(){ if($(this).text()=="Follow"){
if(x==2) return false; 
$(this).trigger("click");
$('._4gt3b').scrollTop($('._4gt3b')[0].scrollHeight);
x++;}
}
)
$('._4gt3b').scrollTop($('._4gt3b')[0].scrollHeight); console.log('loop');
}, 70000); 

Here is the error that I'm receiving after running the script

>VM137:7 Uncaught TypeError: Cannot read property 'scrollHeight' of undefined  
        at HTMLButtonElement.<anonymous> (<anonymous>:7:40)  
        at Function.each (jquery.min.js:2)  
        at n.fn.init.each (jquery.min.js:2)  
        at <anonymous>:3:32   
user1944153
  • 19
  • 1
  • 7

2 Answers2

1

It's looks like the $('._4gt3b')[0] and $('._4gt3b')[1] does not exists.

You can prevent this bug by checking the value first, like this:

var element = $('._4gt3b')[1];
if ( element !== undefined ) {
    $('._4gt3b').scrollTop(element.scrollHeight);
}
Yuval Pruss
  • 8,716
  • 15
  • 42
  • 67
  • Thanks Yoval for your answer , I tried to fix is that way but the problem persists . Do you have other ideeas ? – user1944153 Aug 08 '17 at 17:55
  • What do you see in the console right now? @user1944153 – Yuval Pruss Aug 08 '17 at 18:00
  • I'm not sure if I made everything right , but i see The same error message . VM137:7 Uncaught TypeError: Cannot read property 'scrollHeight' of undefined at HTMLButtonElement. (:7:40) at Function.each (jquery.min.js:2) at n.fn.init.each (jquery.min.js:2) at :3:32 – user1944153 Aug 08 '17 at 18:29
0

Firstly store/cache the dom $('._4gt3b') in some variable e.g. $abc = $('._4gt3b') , and make a javascript check on whether the dom exist or not either by length or index function then do you logic. It will safeguard your code from any console error