Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch Chrome deprecation warnings with Javascript

To centralize our Javascript warnings and errors we use the window.onerror listener.

Unfortunately this does not catch Chrome deprecation warnings which are only shown on the console.

Is there a way to catch those warnings with Javascript?

Example code to trigger a Chrome deprecation warning (Needs a recent version of Chrome):

<!DOCTYPE HTML>                                                                  
<html>                                                                           
  <head><title>Chrome test</title></head>                                        
  <body>                                                                         
    <div class="text">Hallo</div>                                                
    <script>                                                                     
      const range = document.createRange()                                       
      range.selectNode(document.querySelector('.text'))                          
      window.getSelection().addRange(range)                                      
      window.getSelection().addRange(range)                                      
    </script>                                                                    
  </body>                                                                        
</html>            
like image 974
jpoppe Avatar asked Mar 28 '26 20:03

jpoppe


1 Answers

Deprecation warnings, and the like, are added by the browser after your code ran in it, when it detects patterns or behaviors that are about to be changed/deprecated. The idea is that you follow the link provided, and amend your code to fit the future version, as your code would break once that version is out.

As such, you cannot catch those in your code - nor should you want to. You want to see those in testing and fix/future proof your code.

If you don't want to see those warnings in your Developer Tools, go to Settings, under Console, check "User messages only", and you won't see those warnings again

Caveat: these instructions fit Chrome 61. Developer Tools settings keep shifting between versions, so it may look differently disabling Chrome messages in your browser - but it should be similar.

like image 57
Traveling Tech Guy Avatar answered Mar 31 '26 13:03

Traveling Tech Guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!