如何从网页中提取 javaScript 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4873349/
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
How do you extract the javaScript code from a webpage?
提问by LB40
I've to refactor a bunch of html files containing a mix of html and javascript code. I'd like to isolate the javascript code to be able to use various tools (for instance gjslint).
我必须重构一堆包含 html 和 javascript 代码的 html 文件。我想隔离 javascript 代码以便能够使用各种工具(例如 gjslint)。
Is there a tool to do that automatically ? (I did search but wasn't able to find anything, maybe using the wrong terms.)
是否有工具可以自动执行此操作?(我确实进行了搜索,但找不到任何东西,可能是使用了错误的术语。)
thanks...
谢谢...
回答by Petah
If you want to get the javascript with javascript you can use jQuery like so
如果你想用 javascript 获取 javascript,你可以像这样使用 jQuery
$('script').each(function(){
alert($(this).html())
});
回答by David M?rtensson
WebDeveloperToolbarplugin to Firefox has an option Information->View Javascript that will list all inline scirpts and all included scripts and here you can cut and paste what you need.
Firefox 的WebDeveloperToolbar插件有一个选项信息->查看 Javascript,它将列出所有内联脚本和所有包含的脚本,在这里您可以剪切和粘贴您需要的内容。
It will all be presented as a single webpage with collapsable section for each scripttag and external file.
它将全部呈现为单个网页,其中包含每个脚本标签和外部文件的可折叠部分。
But eventhandlers like <a href"" onclick="">will not be listed unfortunatly.
但<a href"" onclick="">不幸的是,事件处理程序不会被列出。
This also works on any webpage without having to edit them first.
这也适用于任何网页,而无需先对其进行编辑。
回答by Jakob
If you install firebugthere is a tab called "script" which displays the content of all files with JavaScripts.
如果您安装firebug,则会有一个名为“脚本”的选项卡,它显示所有带有 JavaScript 的文件的内容。
EDIT: Just realized that it won't help you much if the scripts are inline. If it's included from various other files, it's still useful though.
编辑:刚刚意识到如果脚本是内联的,它不会对你有多大帮助。如果它包含在各种其他文件中,它仍然很有用。
回答by indra
Depends on the editor you're using, but you could do a RegularExpression search and extract blocks of text in the HTML/JS mix that is enclosed in <script></script>tags.
取决于您使用的编辑器,但您可以执行正则表达式搜索并提取包含在<script></script>标签中的 HTML/JS 组合中的文本块。
回答by Heriba
If you want to extract only the tags then you can try it with this site: https://js-download.com/
如果您只想提取标签,则可以在此站点上尝试:https: //js-download.com/
Here are the tags extracted and the result will be merged to one file.
这是提取的标签,结果将合并到一个文件中。

