Overview Open Chrome DevTools What's New in DevTools DevTools tips Simulate mobile devices with Device Mode Performance insights: Get actionable insights on your website's performance Lighthouse: Optimize website speed Animations: Inspect and modify CSS animation effects Changes: Track your HTML, CSS, and JavaScript changes Coverage: Find unused JavaScript and CSS Developer Resources: View and manually load source maps CSS Overview: Identify potential CSS improvements Issues: Find and fix problems Media: View and debug media players information Memory Inspector: Inspect ArrayBuffer, TypedArray, DataView, and Wasm Memory. Network conditions: Override the user agent string Security: Understand security issues Search: Find text across all loaded resources Sensors: Emulate device sensors WebAuthn: Emulate authenticators Customize DevTools Engineering blog
Overview Open Chrome DevTools What's New in DevTools DevTools tips Simulate mobile devices with Device Mode Performance insights: Get actionable insights on your website's performance Lighthouse: Optimize website speed Animations: Inspect and modify CSS animation effects Changes: Track your HTML, CSS, and JavaScript changes Coverage: Find unused JavaScript and CSS Developer Resources: View and manually load source maps CSS Overview: Identify potential CSS improvements Issues: Find and fix problems Media: View and debug media players information Memory Inspector: Inspect ArrayBuffer, TypedArray, DataView, and Wasm Memory. Network conditions: Override the user agent string Security: Understand security issues Search: Find text across all loaded resources Sensors: Emulate device sensors WebAuthn: Emulate authenticators Customize DevTools Engineering blog

Run snippets of JavaScript

Published on Updated on

If you find yourself running the same code in the Console repeatedly, consider saving the code as a snippet instead. Snippets have access to the page's JavaScript context. They are an alternative to bookmarklets.

You can author snippets in the Sources panel and run them on any page and in incognito mode.

DevTools stores snippets as your local preferences. DevTools doesn't sync snippets with settings and they can't be accessed via the file system.

For example, the screenshot below shows the DevTools documentation homepage on the left and some snippet source code in the Sources > Snippets pane on the right.

The DevTools documentation homepage before running the snippet. The Run button is highlighted.

Here's the snippet source code that logs some message and replaces the homepage's HTML body with a <p> element that contains the message:

console.log('Hello, Snippets!');
document.body.innerHTML = '';
const p = document.createElement('p');
p.textContent = 'Hello, Snippets!';
document.body.appendChild(p);

When you click the Run. Run button, the Console drawer pops up to display the Hello, Snippets! message that the snippet logs, and the page's content changes.

The homepage after running the snippet.

Open the Snippets pane

The Snippets pane lists your snippets. To edit a snippet, open it in one of two ways:

  • Navigate to Sources > More tabs. More tabs > Snippets.

    The More tabs menu on the Sources pane.
  • From the Command Menu:

    1. Press Control+Shift+P (Windows/Linux) or Command+Shift+P (Mac) to open the Command Menu.
    2. Start typing Snippets, select Show Snippets, and press Enter.
    Selecting Show Snippets from the Command Menu.

The Sources > Snippets pane shows you a list of snippets you saved, empty in this example.

An empty Snippets pane.

Create snippets

You can create snippets in the Snippets pane or by running the corresponding command from the Command Menu anywhere in DevTools.

The Snippets pane sorts your snippets in alphabetical order.

Create a snippet in the Sources panel

  1. Open the Snippets pane.

  2. Click New snippet. New snippet.

  3. Enter a name for your snippet and press Enter to save.

    Naming a snippet.

Create a snippet from the Command Menu

  1. Focus your cursor anywhere inside of DevTools.

  2. Press Control+Shift+P (Windows/Linux) or Command+Shift+P (Mac) to open the Command Menu.

  3. Start typing Snippet, select Create new snippet, then press Enter to run the command.

    Selecting Create new snippet from the Command Menu.

See Rename snippets if you'd like to give your new snippet a custom name.

Edit snippets

  1. Open the Snippets pane.

  2. In the Snippets pane, click the name of the snippet that you want to edit. The Sources panel opens it in the Code Editor.

    A snippet opened in the Code Editor.
  3. Use the Code Editor to edit code in your snippet. An asterisk next to the snippet name means that you haven't saved your changes yet.

    An asterisk next to the snippet name that indicates unsaved code.
  4. Press Control+S (Windows/Linux) or Command+S (Mac) to save.

Run snippets

Similar to creating a snippet, you can run it both in the Snippets pane and from the Command Menu.

Run a snippet in the Sources panel

  1. Open the Snippets pane.

  2. Click the name of the snippet you want to run. The Sources panel opens it in the Code Editor.

  3. Click Run. Run in the action bar at the bottom of the editor, or press Control+Enter (Windows/Linux) or Command+Enter (Mac).

    The Run button.

Run a snippet from the Command Menu

  1. Focus your cursor anywhere inside of DevTools.

  2. Press Control+O (Windows/Linux) or Command+O (Mac) to open the Command Menu.

  3. Type the ! character followed by the name of the snippet that you want to run.

    Running a snippet from the Open Menu.
  4. Press Enter to run the snippet.

Rename snippets

  1. Open the Snippets pane.
  2. Right-click the snippet name and select Rename.

Delete snippets

  1. Open the Snippets pane.
  2. Right-click the snippet name and select Remove.

Updated on Improve article

This site uses cookies to deliver and enhance the quality of its services and to analyze traffic. If you agree, cookies are also used to serve advertising and to personalize the content and advertisements that you see. Learn more about our use of cookies.