简体   繁体   English

取消缩小/解压 JavaScript

[英]Unminify / Decompress JavaScript

Original Question原始问题

This maybe a stupid question but is there a way in VS 2013 to unminify JavaScript?这可能是一个愚蠢的问题,但是 VS 2013 中是否有一种方法可以取消缩小 JavaScript?

Just making sure we are all on the same page here.只是确保我们都在同一页面上。

Minify:缩小:

var flashVer=-1;if(navigator.plugins!=null&&navigator.plugins.length>0){if(navigator.plugins["Shockwave Flash 2.0"]||navigator.plugins["Shockwave Flash"]){var swVer2=navigator.plugins["Shockwave Flash 2.0"]?"

That's just an example to make sure we all know what I'm on about.这只是一个例子,以确保我们都知道我在说什么。 As far as I can tell there is no way to be able to do this.据我所知,没有办法做到这一点。 I have only been using VS 2013 for around 3 weeks so there is probably still stuff that is hidden to me.我只使用了 VS 2013 大约 3 周,所以可能还有一些东西对我来说是隐藏的。

If there is no way to do this within the program what is the next best thing for this?如果在程序中无法做到这一点,那么接下来最好的事情是什么? I did see on another similar post that recommends the site http://jsbeautifier.org/ , so may have to give that ago but would make life easier if it was built into VS 2013我确实在另一篇推荐网站http://jsbeautifier.org/ 的类似帖子上看到过,所以可能必须在此之前给出,但如果将它内置到 VS 2013 中会让生活更轻松

Thanks in advance as I know someone will be able to help me out here.提前致谢,因为我知道有人可以在这里帮助我。


Update:更新:

I have looked around VS 2013 and found nothing that can help me with this problem, like I said before they maybe some things I have missed (certain settings) so I guess if it cannot be done in VS what's the next best thing for the job?我环顾了 VS 2013 并没有发现任何可以帮助我解决这个问题的东西,就像我之前说的那样,他们可能遗漏了一些我错过的东西(某些设置),所以我想如果在 VS 中无法完成,那么下一个最好的工作是什么? I seem to run into a fair amount of JS that is minifed and would like the quickest and best way to get the job done.我似乎遇到了相当多的 JS,它们被缩小了,并且希望以最快和最好的方式完成工作。 I couple sites I have tried seem to have problems with it, is there a program I could install that would just allow me to short cut it with a hot-key or something.我尝试过的几个网站似乎都有问题,是否有我可以安装的程序,它只允许我使用热键或其他东西来缩短它。 That would be pretty handy.那会很方便。


Update 2:更新 2:

So I think its safe to say this cannot be done within VS2013, or for that matter at all due to missing var names and so on.所以我认为可以肯定地说这不能在 VS2013 中完成,或者由于缺少 var 名称等等。 So I have seen a few links and programs that allow you to format the code.所以我看到了一些允许您格式化代码的链接和程序。 Is there a way to do with within VS2013?在 VS2013 中是否有办法处理? And again if not what is the most reliable website/program that I can use to do this.再说一次,如果不是,我可以用来执行此操作的最可靠的网站/程序是什么。 Like I said I can see there have been answers and I appreciate all of them.就像我说的,我可以看到已经有了答案,我很感激他们。 I will be leaving this question open for a while to get more people to look at it and possibly give a better answer.我将把这个问题搁置一段时间,以便让更多人看到它并可能给出更好的答案。 Keep it up guys!伙计们继续加油!


Update 3:更新 3:

If anyone has any more information on this please do share.如果有人对此有更多信息,请分享。 I am still looking around now and then waiting for someone to come up with something amazing for this.我仍然时不时地环顾四周,然后等待有人为此想出一些惊人的东西。 One day people.... One day!一日人……一日!

The thing is that you cannot really "unminify" your code since some data was already lost - eg variable names.问题是您无法真正“取消缩小”您的代码,因为某些数据已经丢失 - 例如变量名称。 You can reformat it to more readable form though.不过,您可以将其重新格式化为更具可读性的形式。

  1. According to this question , since VisualStudio 2012 you can just use Ctrl + E , D keyboard shortcut 根据这个问题,从 VisualStudio 2012 开始,您可以只使用Ctrl + E , D键盘快捷键
  2. If the above is not right, there is this extension for VS 2010: http://visualstudiogallery.msdn.microsoft.com/41a0cc2f-eefd-4342-9fa9-3626855ca22a but I am not sure if it works with VS 2013如果以上不正确,VS 2010 有此扩展: http : //visualstudiogallery.msdn.microsoft.com/41a0cc2f-eefd-4342-9fa9-3626855ca22a但我不确定它是否适用于 VS 2013
  3. There is an extension to VisualStudio called ReSharper which can reformat javascript in a few different manners. VisualStudio 有一个名为ReSharper的扩展,它可以以几种不同的方式重新格式化 javascript。
  4. Also there are online formatters already mentioned in other answers ( if your code is confidential, I would advise some paranoia manifested by downloading sources and using them locally ).还有其他答案中已经提到的在线格式化程序(如果您的代码是机密的,我会建议通过下载源代码并在本地使用它们来表现出一些偏执狂)。
  5. Also you may always try to find unminified version of desired library on the interwebs此外,您可能总是尝试在互联网上找到所需库的未缩小版本
  6. Also, there is the WebStorm IDE from JetBrains that is able to reformat JS - you may download a trial for the sole purpose of reformatting your minified scripts :)此外,JetBrains 的WebStorm IDE 可以重新格式化 JS - 您可以下载试用版,其唯一目的是重新格式化您的缩小脚本:)
  7. If that's just to make debugging easier, you may want to use source maps如果这只是为了使调试更容易,您可能需要使用源映射

Also, here is a bunch of related questions:另外,这里有一堆相关的问题:

How to automatically indent source code? 如何自动缩进源代码? <-- this is for VS2010, but it looks promising, maybe it will help you if it supports JavaScript ( and it does since VS2012 according to MS support ): <-- 这是针对 VS2010 的,但它看起来很有希望,如果它支持 JavaScript,也许会对您有所帮助( 根据 MS 支持,它自 VS2012 起就支持):

Ctrl + E , D - Format whole doc Ctrl + E , D - 格式化整个文档
Ctrl + K , Ctrl + F - Format selection Ctrl + K , Ctrl + F - 格式选择

reindent(reformat) minimized jquery/javascript file in visual studio 重新缩进(重新格式化)在 Visual Studio 中最小化 jquery/javascript 文件

Visual Studio 2010 can't format complex JavaScript documents Visual Studio 2010 无法格式化复杂的 JavaScript 文档

Visual Studio code formatter Visual Studio 代码格式化程序

how to make visual studio javascript formatting work? 如何使 Visual Studio javascript 格式化工作?

I am not sure if they figured out a working way to reformat JS, but I've seen a few answers which might be helpful - I am just pasting this in here just FYI.我不确定他们是否找到了重新格式化 JS 的有效方法,但我看到了一些可能有用的答案 - 我只是将其粘贴在这里仅供参考。

Added 03.06.2014: 2014 年 6 月 3 日添加:

http://www.jsnice.org/ http://www.jsnice.org/

This tool could be useful too, it even tries to infer minified names.这个工具也很有用,它甚至试图推断缩小的名称。 As stated on their website:正如他们的网站上所说:

We will rename variables and parameters to names that we learn from thousands of open source projects.

Its just a one way transformation .... sorry in normal cases you will not get something understandable back from minified JavaScript !它只是一种单向转换......抱歉,在正常情况下,您不会从缩小的 JavaScript 中得到一些可以理解的东西!

Make just a quick look at JQuery source for a second:快速浏览一下 JQuery 源代码:

(function( window, undefined ) {

// Can't do this because several apps including ASP.NET trace
// the stack via arguments.caller.callee and Firefox dies if
// you try to trace through "use strict" call chains. (#13335)
// Support: Firefox 18+
//"use strict";
var
    // The deferred used on DOM ready
    readyList,

    // A central reference to the root jQuery(document)
    rootjQuery,

    // Support: IE<10
    // For `typeof xmlNode.method` instead of `xmlNode.method !== undefined`
    core_strundefined = typeof undefined,

    // Use the correct document accordingly with window argument (sandbox)
    location = window.location,
    document = window.document,
    docElem = document.documentElement,

    // Map over jQuery in case of overwrite
    _jQuery = window.jQuery,

    // Map over the $ in case of overwrite
    _$ = window.$,

    // [[Class]] -> type pairs
    class2type = {},

    // List of deleted data cache ids, so we can reuse them
    core_deletedIds = [],

    core_version = "1.10.2",
------

And now at the minify source:现在在缩小源:

(function(e,t){var n,r,i=typeof t,o=e.location,a=e.document,s=a.documentElement,
l=e.jQuery,u=e.$,c={},p=[],f="1.10.2", ....

I think now you see it =>我想现在你看到了 =>

window       => e
undefined    => t
readyList    => n
rootjQuery   => r
core_strundefined  => i
location     => o
document     => a 

So its mapped somehow to make it more shorter look here to minify something People normally use this so there is no way back所以它以某种方式映射以使其看起来更短以缩小某些东西 人们通常使用它,因此没有退路

you can just format it look here你可以在这里格式化它

Personally I can't think of a reason to ever unminify code^:就我个人而言,我想不出任何取消缩小代码的理由^:

  • If you're using a compiled js file (a-la google closure) and want more readable code to debug, use source maps available for well-supported libraries (speaking of jQuery, if it is served from a google CDN it already maps to the correct source)如果您使用的是已编译的 js 文件(a-la google 闭包)并希望调试更易读的代码,请使用可用于受良好支持的库的源映射(说到 jQuery,如果它是从 google CDN 提供的,它已经映射到正确的来源)

  • If you're using a whitespace-only minified js file and want more readable code to debug, you could just toggle pretty print in-browser .如果您使用只有空格的缩小 js 文件并希望调试更易读的代码,您可以在浏览器中切换漂亮的打印 This seems to best fit your question.这似乎最适合您的问题。

  • If you're using either of the above and want to modify the source code for a third-party js file, don't.如果您正在使用上述任何一种并且想要修改第三方js文件的源代码,请不要。 Any future release will cancel out your change - instead consider one of the many patterns to extend a framework (or, perhaps, do some duck punching depending on the exact scenario.)任何未来的版本都将取消您的更改 - 而是考虑扩展框架的众多模式之一(或者,根据具体情况进行一些鸭子冲刺。)

The other answers seem to cover the "unminification" process (maxification?) well, but it's worth making sure it's a necessary step first.其他答案似乎很好地涵盖了“取消缩小”过程(最大化?),但值得首先确保这是必要的步骤。

^ - Except when version control falls over, there are no backups and the only version of the file left is a minified copy in browser cache. ^ - 除非版本控制失败,否则没有备份,文件的唯一版本是浏览器缓存中的缩小副本。 Don't ask.不要问。

If the code has only been minified then the best you can do automatically is reformat to make it more readable.如果代码只是被缩小了,那么你可以自动做的最好的事情就是重新格式化以使其更具可读性。 One way of doing this is using an online formatter/beautifier.一种方法是使用在线格式化程序/美化程序。 Eg Copy and paste the line of code you posted into http://jsbeautifier.org/ or http://www.jspretty.com/ and it'll produce something like this:例如,将您发布的代码行复制并粘贴到http://jsbeautifier.org/http://www.jspretty.com/ 中,它将生成如下内容:

var flashVer = -1;
if (navigator.plugins != null && navigator.plugins.length > 0) {
    if (navigator.plugins["Shockwave Flash 2.0"]
            || navigator.plugins["Shockwave Flash"]) {
        var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? ""

But of course what these don't do is put any comments that have been removed by the minifier back in. And if the code has also been obfuscated then it will be a lot less readable since the variable names will have changed (eg var a instead of var flashVer ).但当然,这些不会做的是将任何已被压缩器删除的注释放回去。如果代码也被混淆了,那么它的可读性将大大降低,因为变量名称将发生变化(例如var a而不是var flashVer )。 See here for further details.请参阅此处了解更多详情。

As you can see from the other answers, there is no way to reconstitute minified Javascript back into its original form, it is a lossy compression.正如您从其他答案中看到的那样,无法将缩小的 Javascript 重组回其原始形式,这是一种有损压缩。 The best you can do is make it readable by reformatting it.您能做的最好的事情是通过重新格式化使其可读。

If the code is open source, then it is likely that the code will exists in a raw state on some form of version control site or as a zip.如果代码是开源的,那么代码很可能会以原始状态存在于某种形式的版本控制站点上或以 zip 的形式存在。 Why not just download the raw version if available?如果可用,为什么不直接下载原始版本?

There is an online tool to unminify Javascripts有一个在线工具来取消 Javascripts

http://jsbeautifier.org/ http://jsbeautifier.org/

And also for CSS也适用于 CSS

http://mrcoles.com/blog/css-unminify/ http://mrcoles.com/blog/css-unminify/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM