使用 Chrome 开发者工具编辑 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12593168/
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
Editing Javascript using Chrome Developer Tools
提问by cooperia
I am trying to edit javascript on a site using Chrome's Developer Tools. I have read about 30 accounts of how to do this as well as watched a few videos. The fact is, when I go to the sources tab and open the file I want to edit, I can't do anything to it. Is there some step I am missing?
我正在尝试使用 Chrome 的开发人员工具在网站上编辑 javascript。我已经阅读了大约 30 个有关如何执行此操作的帐户并观看了一些视频。事实是,当我转到源选项卡并打开要编辑的文件时,我无能为力。有什么我遗漏的步骤吗?
I can create break points, step through, etc... I just can't edit. Was this functionality removed recently?
我可以创建断点、单步执行等...我就是无法编辑。最近是否删除了此功能?
回答by raddevon
I know this question is stale, but I just had a similar problem and found the solution.
我知道这个问题已经过时了,但我刚刚遇到了类似的问题并找到了解决方案。
If you have the file prettified, Chrome will not allow edits. I turned it off and was able to edit. Willing to bet this is/was your problem.
如果您对文件进行了美化,Chrome 将不允许编辑。我将其关闭并能够进行编辑。愿意打赌这是/是你的问题。
回答by welah
You can edit javascript in the developer tools on the "Sources" tab, BUT it will only allow you to edit javascript in its own file. Script embedded in an HTML (or PHP) file will remain read-only.
您可以在“源”选项卡上的开发人员工具中编辑 javascript,但它只允许您在其自己的文件中编辑 javascript。嵌入在 HTML(或 PHP)文件中的脚本将保持只读状态。
回答by gcb
It has some limitations:
它有一些限制:
has to be a JS file. can't be embeded tags in a html page.
it cannot be prettified.
必须是一个JS文件。不能在 html 页面中嵌入标签。
它不能美化。
回答by byronaltice
I don't know if you need this to save permanently, but if you need to just temporarily modify the js:
我不知道你是否需要这个来永久保存,但如果你只需要临时修改js:
I can copy that javascript I want to modify into a text editor, edit it, then paste it in the console and it will redefine any functions or whatever that I need to be redefined.
我可以将我想要修改的 javascript 复制到文本编辑器中,对其进行编辑,然后将其粘贴到控制台中,它将重新定义任何函数或我需要重新定义的任何内容。
for instance, if the page has:
例如,如果页面有:
<script>
var foo = function() { console.log("Hi"); }
</script>
I can take the content between the script, edit it, then enter it into the debugger like:
我可以在脚本之间获取内容,对其进行编辑,然后将其输入到调试器中,例如:
foo = function() { console.log("DO SOMETHING DIFFERENT"); }
and it will work for me.
它对我有用。
Or if you have like,
或者如果你有喜欢的,
function foo() {
doAThing();
}
You can just enter
你可以输入
function foo() {
doSomethingElse();
}
and foo will be redefined.
并且 foo 将被重新定义。
Probably not the best workaround, but it works. Will last until you reload the page.
可能不是最好的解决方法,但它有效。将持续到您重新加载页面。
回答by wafe
I did search "chrome dev tool edit javascript". This page is the first search result. But it is too outdated, it does not help me.
我确实搜索了“chrome dev tool edit javascript”。此页面是第一个搜索结果。但它太过时了,对我没有帮助。
I am using Chrome 73, this version of Chrome has "Enable Local Overrides" option. Using the function, I could edit a javascript and could run and debug.
我使用的是 Chrome 73,此版本的 Chrome 具有“启用本地覆盖”选项。使用该函数,我可以编辑 javascript 并可以运行和调试。