DevTools Tips

Manipulate global objects on page load, before other scripts run

Categories: Supported by:

When a page loads, and all of its global objects are ready, but before the JavaScript code of the page actually kicks-in, is a great time to inject debugging code. For example, it might be useful to replace global functions with ones that contain altered code.

Safari lets you do this by creating an Inspector Bootstrap Script. This script is guaranteed to run after JavaScript global objects have been created, as long as DevTools is opened.

To create a bootstrap script:

  1. Open the Sources tool
  2. At the bottom of the sidebar, click Add resource (+).
  3. Choose the Inspector Bootstrap Script type.
  4. Write the code you need.

The Sources tab in Safari's WebInspector, showing the Add resource button and the Bootstrap script type

To learn more, see Inspector Bootstrap Script on the webkit blog.