如何保护/隐藏 Javascript 方法或 JS 文件以供用户查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3838908/
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-25 02:24:51  来源:igfitidea点击:

How to protect/Hide Javascript method or JS file from user to view

javascriptajaxsecurityjson

提问by Brij

How can I protect my implemented JS methods from user? So that no one could copy these methods. Normally, User can see JS methods in firebug easily. I want to hide methods in firebug also. Is there any trick to hide methods from users?

如何保护我实现的 JS 方法不被用户使用?所以没有人可以复制这些方法。通常,用户可以很容易地在 firebug 中看到 JS 方法。我也想隐藏萤火虫中的方法。有什么技巧可以隐藏用户的方法吗?

Note:I don't want to use Eval method. so please don't suggest me to eval based tricks

注意:我不想使用 Eval 方法。所以请不要建议我使用基于评估的技巧

回答by epascarello

There is no way to hide your code from the user. If the browser can see it, than Firebug can see it. Using a minifier/packer used to be a way to make it harder to get to the data, but sites like http://jsbeautifier.org/have the ability to take packed code and make it pretty.

没有办法对用户隐藏你的代码。如果浏览器可以看到它,那么 Firebug 可以看到它。使用 minifier/packer 曾经是一种使获取数据变得更加困难的方法,但是像http://jsbeautifier.org/这样的站点可以采用打包代码并使其变得漂亮。

Short answer is there is no way of hiding code. If it was possible, Microsoft, Google, Yahoo, Etc would be hiding their code.

简短的回答是没有办法隐藏代码。如果可能的话,微软、谷歌、雅虎等都会隐藏他们的代码。

Think of JavaScript, HTML, and CSS as open source since the source code is just one click away.

将 JavaScript、HTML 和 CSS 视为开源,因为只需单击一下即可获得源代码。

回答by Edgar Bonet

The only way to efficiently protect your code is to never send it to the client:
Put your “secret” methods on the server and make them ajax-callable from the client.

有效保护您的代码的唯一方法是永远不要将其发送给客户端:
将您的“秘密”方法放在服务器上并使它们可从客户端进行 ajax 调用。

回答by mkoryak

  1. try packing the code/minifiying it
  1. 尝试打包代码/缩小它

google closure compileris a good start

谷歌闭包编译器是一个好的开始

there is also a YUI lib that you can use and many others.

还有一个您可以使用的 YUI 库和许多其他库。

This will make your code pretty much impossible to debug in firebug.

这将使您的代码几乎不可能在 firebug 中调试。

There is no full proof way though to stop users for mucking with your code, since it does run on the client side by definition.

虽然没有完全证明方法可以阻止用户使用您的代码,因为它确实在客户端运行。

  1. Another thing you can do is put the methods you want to hide inside of a closure, so that the user cant simply run someMethod() from firebug and have it execute code.
  1. 您可以做的另一件事是将您想要隐藏的方法放在闭包中,这样用户就不能简单地从 firebug 运行 someMethod() 并让它执行代码。