在 chrome 开发者工具中从工作区运行 javascript 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27959590/
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
Run javascript file from the workspace in chrome developer tools
提问by arnoutaertgeerts
Is it possible to run a newly created JavaScript file in a local workspace in the chrome developer tools console?
是否可以在 chrome 开发人员工具控制台的本地工作区中运行新创建的 JavaScript 文件?
The workflow I'm trying to achieve is the one shown in this image:
我试图实现的工作流程如下图所示:
I want to be able to create a new file in my workspace, run (or require or whatever) the file and be able to use it's functions and variables in the chrome developer console.
我希望能够在我的工作区中创建一个新文件,运行(或要求或其他)该文件并能够在 chrome 开发人员控制台中使用它的函数和变量。
If I'm correct, this means running the script within the context of the webpage and adding the methods and variables to the window object?
如果我是对的,这意味着在网页的上下文中运行脚本并将方法和变量添加到 window 对象?
Is their a way this can be done?
他们有办法做到这一点吗?
回答by Trendfischer
I could not find an automatic way to add a local file into the DOM context. The best solution I found so far:
我找不到将本地文件添加到 DOM 上下文的自动方法。迄今为止我找到的最佳解决方案:
- Open your local workspace and the right file
- Press CTRL + a (Select all)
- Press CTRL + SHIFT + e (alternative: Right click with the mouse on the selected text and click on "Evaluate in Console")
- 打开本地工作区和正确的文件
- 按 CTRL + a(全选)
- 按 CTRL + SHIFT + e(或者:用鼠标右键单击所选文本,然后单击“在控制台中评估”)
Well, this is not much better than copy&paste but spares a few key presses/mouse clicks.
嗯,这并不比复制和粘贴好多少,但省去了几次按键/鼠标点击。
回答by Michael Dowd
You can define a method in your page to dynamically add javascript to the page and then call it from the console.
您可以在页面中定义一个方法来动态地将 javascript 添加到页面,然后从控制台调用它。
For example if you had a method like this:
例如,如果您有这样的方法:
function loadJs(filename) {
var tag=document.createElement('script');
tag.setAttribute("type","text/javascript");
tag.setAttribute("src", filename);
document.getElementsByTagName("head")[0].appendChild(tag);
}
then you can use that method from the console to load your javascript files.
然后您可以从控制台使用该方法来加载您的 javascript 文件。
回答by Raj Kumar
You can create a plain html file like this with your javascript file in the script tag.
您可以使用脚本标记中的 javascript 文件创建这样的纯 html 文件。
Then you should be able to get all your methods in the developer console.
然后您应该能够在开发人员控制台中获取所有方法。