33

Reference:

Using the new code snippets feature in google chrome

I am using the code snippets in google chrome, so say I have a snippet file. check_consistency.js

Is there an api or a global object through which we can run the snippet directly from the command line, something like:

  window.runSnippet('check_consistency.js')

or maybe call methods defined in the snippet directly.

3
  • 1
    Damn, I was really hoping for an answer to this. Would be pretty handy to have a way to set console-scoped functions through snippets. Commented Jul 18, 2013 at 16:24
  • 4
    Raised a feature request on Devtools to add this. code.google.com/p/chromium/issues/detail?id=358949 Commented Apr 2, 2014 at 4:15
  • 2
    @VivekKodira sadly that ticket was closed as not being a priority. Commented Jan 7, 2016 at 23:07

2 Answers 2

27

Workflow Tip 1

I also want to see this functionality added. Meanwhile, perhaps try opening the Sources where (as you know) you can select a snippet and right click it to run it. You may or may not know that you can tap Esc on this page in order to show the console at the same time as your snippets:

Workflow Tip 2

The snippets documentation also mentions

The ability to quickly locate a specific file can be essential to a developer's workflow. The DevTools allow you to search across all script, stylesheet and snippet files using the following shortcuts:

Ctrl + O (Windows, Linux)
Cmd + O (Mac OSX)

which will work regardless of the panel you are currently in.

...and...

A keyboard shortcut is also available for easily executing a snippet - just select your snippet then use Ctrl/Cmd + Enter to run it. This replicates the behavior of the Run (>) button - currently in the Sources console, but which will be moving into the debugger control in the near future.

What this means is that while in the console you can press Ctrl/Cmd+O to quickly select your snippet, and then press Cmd/Control+Enter to run it.

Sign up to request clarification or add additional context in comments.

2 Comments

I was looking for a chrome API so that the mouse is not involved. This new update solves the purpose.
You can also type "!" after cmd-O - this will pull up your list of snippets.
13

I have a work around for when I'm running snippets a bunch of times on a site. I wrap my snippet code in a function and assign it to a global variable.

e.g.,

window.mySnippet = function (value) {
  console.log(value.toUpperCase());
};

When I run this snippet I can now run

mySnippet('hello world');  
-> "HELLO WORLD"

You still have to run the snippet once to load it into memory, but it's better than nothing.

1 Comment

I'm using Ctrl/Cmd+O, then typing "!" to show a list of snippets. Select with cursor and Enter. Would still like to get to that point quicker, though (like Ctrl/Cmd+Shift+P for commands)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.