javascript 从 Google Chrome 控制台命令行运行代码片段,使用(实验性)代码片段功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15579164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:15:37  来源:igfitidea点击:

Running code snippets from Google Chrome console command line, using (experimental) code snippets feature?

javascriptgoogle-chromegoogle-chrome-devtoolsjavascript-debugger

提问by sbr

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

我在谷歌浏览器中使用代码片段,所以说我有一个片段文件。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:

是否有一个 api 或一个全局对象,我们可以通过它直接从命令行运行代码片段,例如:

  window.runSnippet('check_consistency.js')

or maybe call methods defined in the snippet directly.

或者直接调用代码片段中定义的方法。

采纳答案by cwd

Workflow Tip 1

工作流程提示 1

I also want to see this functionality added. Meanwhile, perhaps try opening the Sourceswhere (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 Escon this page in order to show the console at the same timeas your snippets:

我也希望看到添加此功能。同时,也许尝试打开Sources(如您所知)您可以选择一个片段并右键单击它以运行它的位置。您可能知道也可能不知道您可以点击Esc此页面以在显示代码的同时显示控制台

Workflow Tip 2

工作流程提示 2

The snippets documentationalso 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.

快速定位特定文件的能力对于开发人员的工作流程至关重要。DevTools 允许您使用以下快捷方式搜索所有脚本、样式表和代码段文件:

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

无论您当前所在的面板如何,这都将起作用。

...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.

键盘快捷键也可用于轻松执行代码段 - 只需选择您的代码段,然后使用 Ctrl/Cmd + Enter 运行它。这复制了 Run (>) 按钮的行为 - 当前在 Sources 控制台中,但在不久的将来将进入调试器控件。

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

这意味着,在控制台中,您可以按Ctrl/Cmd+O快速选择代码片段,然后按Cmd/Control+Enter运行它。

回答by JaredMcAteer

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.

您仍然需要运行一次代码片段才能将其加载到内存中,但总比没有好。