How to check if you are on widget.php page?

You may use the global variable $pagenow to figure out if you are on a particular admin page, in your case this would be checking if you are on the widgets.php admin page:

<?php 

    global $pagenow;

    if( $pagenow === 'widgets.php' ) {
        ?>
        <script>

            // JavaScript goes here

        </script>
        <?php 
    }
 

Furthermore, it will be helpful for you to use the plugin Query Monitor so that you may easily find out what conditionals may be used on a particular instance.