brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: DevTools Snippets keep your console code and run with Ctrl+Enter

Devtools
~/tips/tip-devtools-snippets

console.table([...document.querySelectorAll('img')].map(i => i.src))

Saves JavaScript in DevTools so you can run it on any page with one shortcut.

If I paste the same ten lines into the Console every time, I should save them. DevTools has a place for that, and it is more comfortable than the Console input.

Open Sources > More tabs > Snippets, click New snippet, type a name and press Enter. Write your code in the editor and save with Ctrl+S (Command+S on a Mac). An asterisk next to the name means unsaved changes. To run it, press Ctrl+Enter (Command+Enter), or click Run in the editor’s action bar.

A snippet runs in the context of the page, like code typed in the Console. So a snippet like this works on any page you open:

console.table([...document.querySelectorAll('img')].map(i => i.src))

You do not even need the Snippets pane open to start one. Press Ctrl+O (Command+O), type ! followed by the snippet name, and press Enter. The docs describe this as an alternative to bookmarklets.

Right-click a name in the pane to rename or remove a snippet. They are sorted alphabetically, so a shared prefix in the names keeps related ones together.

Where they live is the catch. The docs say snippets are stored as local preferences, are not synced with your settings and cannot be reached through the file system. So there is no file to put in git or to copy to another computer. If a snippet matters, keep a copy of the code somewhere else. A snippet can also change the page you run it on, which is the point, so try it on a page you do not mind altering.

Further reading: Chrome DevTools: run snippets of code

More Devtools tips Clear site data for one site from Chrome DevTools user.id === 42 $x(path [, startNode]) @media (prefers-color-scheme: dark) { queryObjects(Promise) All Devtools tips (22) All topics

Link