Javascript 如何通过javascript或jquery隐藏源代码和检查元素?

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

How to hide source code and inspect element via javascript or jquery?

javascriptjqueryhtmlcss

提问by Jishnu V S

I want to disable my webpage source code and inspect element bar. And I did it with this script. But if I wrote " view-source: "before my url the source code will be shown. How can I fix this? Is there any possibility to hide source code? Good answers must be appreciated

我想禁用我的网页源代码并检查元素栏。我用这个脚本做到了。但是如果我" view-source: "在我的 url 之前写,源代码将被显示。我怎样才能解决这个问题?有没有可能隐藏源代码?好的答案必须赞赏

<script>

  document.onkeydown = function(e) {
    if(e.keyCode == 123) {
     return false;
    }
    if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)){
     return false;
    }
    if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)){
     return false;
    }
    if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)){
     return false;
    }

    if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)){
     return false;
    }      
 }

</script>

回答by Marcus

There is no way to hide the JS code. JS-Scripts are running inside your Browser. So the Browser must know the JS-Scripts. And if the Browser know it, the user can have a look at them. You could only try to compress them, to avoid to easy reading by user.

没有办法隐藏JS代码。JS 脚本在您的浏览器中运行。所以浏览器必须知道 JS-Scripts。如果浏览器知道,用户可以查看它们。您只能尝试压缩它们,以避免用户轻松阅读。

Use https://jscompress.com/if you have a static JS-Script

如果您有静态 JS 脚本,请使用https://jscompress.com/

回答by Haresh Vidja

You cant restrict view-source, but any way you can put javscript and css compressed file.

您不能限制查看源代码,但是您可以通过任何方式放置 javscript 和 css 压缩文件。

Dont bind Event from Inline attribute of HTMLDont bind event from inline attribute in html, For example

不要从 HTML 的内联属性绑定事件 不要从 html 的内联属性绑定事件,例如

<button id="btn_callme" onclick="callme(this)"> Call Me</button>

instead of this you can bind event in javascript code.

而不是这个,你可以在javascript代码中绑定事件。

<button id="btn_callme"> Call Me</button>

$(document).on('click', '#callme', function(){
      // write code here    
})

Combine Common Javascript files into one

将常见的 Javascript 文件合二为一

If you use multiple javascript as common javascript you can combined it in one file and minify it, it will make some faster execution.

如果您使用多个 javascript 作为通用 javascript,您可以将其合并到一个文件中并缩小它,这样可以加快执行速度。

Compress/minify Javascript

压缩/缩小 Javascript

For security of Javascript code you can compress it from online tools like http://javascriptcompressor.com/

为了 Javascript 代码的安全性,您可以从http://javascriptcompressor.com/等在线工具压缩它

Compress/Minify CSS code

压缩/缩小 CSS 代码

For security of CSS code you can compress it from online tools like https://csscompressor.net/

为了 CSS 代码的安全性,您可以从https://csscompressor.net/等在线工具压缩它

Don't allow execute script from Console

不允许从控制台执行脚本

I want to suggest one more security, regarding console, if you want to prevent javascript code execution from console.log you can use below code. Here I have used debug_modeclass in body tag for allowed execute from console.

关于控制台,我想建议另一种安全性,如果您想阻止从 console.log 执行 javascript 代码,您可以使用以下代码。在这里,我debug_mode在 body 标签中使用了class 以允许从控制台执行。

(function () {
    if (!$('body').hasClass('debug_mode')) {
        var _z = console;
        Object.defineProperty(window, "console", {
            get: function () {
                if ((window && window._z && window._z._commandLineAPI) || {}) {
                    throw "Nice trick! but not permitted!";
                }
                return _z;
            },
            set: function (val) {
                _z = val;
            }
        });
    }
})();

回答by cheralathan

You can't hide your source code.

你不能隐藏你的源代码。

Here are the different options to view your source code.

以下是查看源代码的不同选项。

  • Right Click and Inspect Element or Press CTRL + SHIFT + I

  • Right Click and View Page Source or Press CTRL + U

  • Finally Go to chrome menu navigate to More Tools--> Developer tools

  • Another option is accessing your code by using firebug Extension.

  • 右键单击并检查元素或按 CTRL + SHIFT + I

  • 右键单击并查看页面源代码或按 CTRL + U

  • 最后转到 chrome 菜单导航到更多工具-->开发人员工具

  • 另一种选择是使用 firebug Extension 访问您的代码。

回答by kurniadi455

Try this code put the code on the body

试试这个代码把代码放在 body

oncontextmenu="return false;" onkeydown="return false;" onmousedown="return false;"

And will be like this

并且会是这样

<body oncontextmenu="return false;" onkeydown="return false;" onmousedown="return false;"></body>

回答by AcidSnake

I've read multiple sources that you shouldn't for plenty of reasons like making people not want to revisit the site when they discover their browser options are removed. I suggest reading this article. Information on what you're trying to do.

我已经阅读了多个来源,出于很多原因,您不应该这样做,例如让人们在发现浏览器选项被删除时不想重新访问该网站。我建议阅读这篇文章。有关您尝试执行的操作的信息。

or resort to php code so it's server side. javascript is client side so regardless you're going to be able to read the code.

或求助于 php 代码,所以它是服务器端。javascript 是客户端,因此无论您是否能够阅读代码。

回答by brk

There is no way to hide source code & inspect element, but all you can do is disablethe the right clickof mouse button using

有没有办法隐藏源代码和检查元素,但所有你能做的就是disable在该right click用鼠标按钮

    function disableclick(event){
           if(event.button==2){ // this value is 3 for some othe browser
            // Rest of code
         return false;    
       }
    }
document.body.onclick = disableclick()

Also not that you need to disable F12key since pressing F12open the developers console, which is actually bypass right click event

也不是F12因为按下F12打开开发人员控制台而需要禁用键,这实际上是绕过右键单击事件

回答by Mownesh

Try this in your project:

在你的项目中试试这个:

document.addEventListener('keydown', function() {
  if (event.keyCode == 123) {
    alert("This function has been disabled to prevent you from stealing my code!");
    return false;
  } else if (event.ctrlKey && event.shiftKey && event.keyCode == 73) {
    alert("This function has been disabled to prevent you from stealing my code!");
    return false;
  } else if (event.ctrlKey && event.keyCode == 85) {
    alert("This function has been disabled to prevent you from stealing my code!");
    return false;
  }
}, false);

if (document.addEventListener) {
  document.addEventListener('contextmenu', function(e) {
    alert("This function has been disabled to prevent you from stealing my code!");
    e.preventDefault();
  }, false);
} else {
  document.attachEvent('oncontextmenu', function() {
    alert("This function has been disabled to prevent you from stealing my code!");
    window.event.returnValue = false;
  });
}

回答by Darko Davidovic

We can do a lot of tricks to prevent someone to use your code and reuse them. -We can obfuscate JavaScript source code to be human impossible to read and reverse from obfuscation. In the example into the snippets, we have protected and unprotected functions. The both functions has console.log, but second one is prevented by source limitation. -Next that we can to do, even to hide the source code is to use module export/import inside ES6. Everything you need is main.js file attached to your HTML and inside of it use module import from otherFile.js. After you finish your code, obfuscate your main.js file and user no chance to see from where some functions is coming. Se first scenario in example below.

我们可以做很多技巧来防止有人使用您的代码并重用它们。- 我们可以将 JavaScript 源代码混淆为人类无法阅读并从混淆中逆转。在代码片段的示例中,我们有受保护和不受保护的函数。这两个函数都有console.log,但是第二个函数被源限制阻止了。-接下来我们可以做的,甚至隐藏源代码是在 ES6 中使用模块导出/导入。您需要的一切都是附加到您的 HTML 的 main.js 文件,并且在其中使用来自 otherFile.js 的模块导入。在你完成你的代码之后,混淆你的 main.js 文件并且用户没有机会看到一些函数是从哪里来的。下面示例中的第一个场景。

runMeUnprotected()
function runMeUnprotected(){
  console.log("Started to building span elements...")
    for (let i =0; i < 5; i++) {
      document.write(`<span>I am unprotected text number ${i+1}</span><br/>`)
    }
}

// protected source code
const _0x335d=['aW5pdA==','Z2dlcg==','Y2FsbA==','XCtcKyAqKD86W2EtekEtWl8kXVswLTlhLXpBLVpfJF0qKQ==','YWN0aW9u','d2Fybg==','ZGVidQ==','Y29uc3RydWN0b3I=','PHNwYW4+SSBhbSBwcm90ZWN0ZWQgc291cmNlIHRleHQgbnVtYmVyIA==','bG9n','cmV0dXJuIChmdW5jdGlvbigpIA==','Y291bnRlcg==','aW5mbw==','c3RhdGVPYmplY3Q=','aW5wdXQ=','d2hpbGUgKHRydWUpIHt9','dGFibGU=','PC9zcGFuPjxici8+','U3RhcnRlZCB0byBidWlsZGluZyBzcGFuIGVsZW1lbnRzLi4u','dGVzdA==','Y29uc29sZQ==','dHJhY2U=','ZXhjZXB0aW9u','ZXJyb3I=','bGVuZ3Ro','c3RyaW5n','YXBwbHk=','e30uY29uc3RydWN0b3IoInJldHVybiB0aGlzIikoICk='];(function(_0xd0c961,_0x335d1f){const _0x1f6828=function(_0x127db4){while(--_0x127db4){_0xd0c961['push'](_0xd0c961['shift']());}};_0x1f6828(++_0x335d1f);}(_0x335d,0x9a));const _0x1f68=function(_0xd0c961,_0x335d1f){_0xd0c961=_0xd0c961-0x0;let _0x1f6828=_0x335d[_0xd0c961];if(_0x1f68['QtBSWb']===undefined){(function(){let _0x2475f7;try{const _0x2fdd22=Function('return\x20(function()\x20'+'{}.constructor(\x22return\x20this\x22)(\x20)'+');');_0x2475f7=_0x2fdd22();}catch(_0x455560){_0x2475f7=window;}const _0x4e2279='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';_0x2475f7['atob']||(_0x2475f7['atob']=function(_0x46d82b){const _0x2f1454=String(_0x46d82b)['replace'](/=+$/,'');let _0x13e571='';for(let _0x586170=0x0,_0x181ba3,_0x10e93b,_0xd38bec=0x0;_0x10e93b=_0x2f1454['charAt'](_0xd38bec++);~_0x10e93b&&(_0x181ba3=_0x586170%0x4?_0x181ba3*0x40+_0x10e93b:_0x10e93b,_0x586170++%0x4)?_0x13e571+=String['fromCharCode'](0xff&_0x181ba3>>(-0x2*_0x586170&0x6)):0x0){_0x10e93b=_0x4e2279['indexOf'](_0x10e93b);}return _0x13e571;});}());_0x1f68['QWdIgQ']=function(_0xb91783){const _0x3680fc=atob(_0xb91783);let _0x315641=[];for(let _0x3bd552=0x0,_0x3c07d7=_0x3680fc['length'];_0x3bd552<_0x3c07d7;_0x3bd552++){_0x315641+='%'+('00'+_0x3680fc['charCodeAt'](_0x3bd552)['toString'](0x10))['slice'](-0x2);}return decodeURIComponent(_0x315641);};_0x1f68['RgtGCS']={};_0x1f68['QtBSWb']=!![];}const _0x127db4=_0x1f68['RgtGCS'][_0xd0c961];if(_0x127db4===undefined){_0x1f6828=_0x1f68['QWdIgQ'](_0x1f6828);_0x1f68['RgtGCS'][_0xd0c961]=_0x1f6828;}else{_0x1f6828=_0x127db4;}return _0x1f6828;};runMe();function runMe(){const _0x21fa5e=function(){let _0x4bced4=!![];return function(_0x1dd36e,_0xf8c54f){const _0x310c36=_0x4bced4?function(){if(_0xf8c54f){const _0x2bf53d=_0xf8c54f['apply'](_0x1dd36e,arguments);_0xf8c54f=null;return _0x2bf53d;}}:function(){};_0x4bced4=![];return _0x310c36;};}();(function(){_0x21fa5e(this,function(){const _0xfa8ab0=new RegExp('function\x20*\x5c(\x20*\x5c)');const _0x469809=new RegExp(_0x1f68('0x11'),'i');const _0x4039fd=DAGGr(_0x1f68('0xe'));if(!_0xfa8ab0[_0x1f68('0x5')](_0x4039fd+'chain')||!_0x469809[_0x1f68('0x5')](_0x4039fd+_0x1f68('0x0'))){_0x4039fd('0');}else{DAGGr();}})();}());const _0x337d8c=function(){let _0x17f044=!![];return function(_0x40e373,_0x6952d4){const _0x2fa85a=_0x17f044?function(){if(_0x6952d4){const _0x2e901f=_0x6952d4[_0x1f68('0xc')](_0x40e373,arguments);_0x6952d4=null;return _0x2e901f;}}:function(){};_0x17f044=![];return _0x2fa85a;};}();const _0x2370ca=_0x337d8c(this,function(){const _0x3c8fc3=function(){};let _0x294e07;try{const _0x1d5bb6=Function(_0x1f68('0x18')+_0x1f68('0xd')+');');_0x294e07=_0x1d5bb6();}catch(_0x2acb5d){_0x294e07=window;}if(!_0x294e07[_0x1f68('0x6')]){_0x294e07['console']=function(_0x2e207d){const _0x4fd782={};_0x4fd782['log']=_0x2e207d;_0x4fd782[_0x1f68('0x13')]=_0x2e207d;_0x4fd782['debug']=_0x2e207d;_0x4fd782[_0x1f68('0x1a')]=_0x2e207d;_0x4fd782[_0x1f68('0x9')]=_0x2e207d;_0x4fd782[_0x1f68('0x8')]=_0x2e207d;_0x4fd782[_0x1f68('0x2')]=_0x2e207d;_0x4fd782[_0x1f68('0x7')]=_0x2e207d;return _0x4fd782;}(_0x3c8fc3);}else{_0x294e07['console'][_0x1f68('0x17')]=_0x3c8fc3;_0x294e07[_0x1f68('0x6')][_0x1f68('0x13')]=_0x3c8fc3;_0x294e07[_0x1f68('0x6')]['debug']=_0x3c8fc3;_0x294e07[_0x1f68('0x6')][_0x1f68('0x1a')]=_0x3c8fc3;_0x294e07[_0x1f68('0x6')]['error']=_0x3c8fc3;_0x294e07[_0x1f68('0x6')][_0x1f68('0x8')]=_0x3c8fc3;_0x294e07[_0x1f68('0x6')]['table']=_0x3c8fc3;_0x294e07[_0x1f68('0x6')]['trace']=_0x3c8fc3;}});_0x2370ca();console[_0x1f68('0x17')](_0x1f68('0x4'));for(let _0x37144d=0x0;_0x37144d<0x5;_0x37144d++){document['write'](_0x1f68('0x16')+(_0x37144d+0x1)+_0x1f68('0x3'));}}function DAGGr(_0x4a2f16){function _0x5e901c(_0xd2c678){if(typeof _0xd2c678===_0x1f68('0xb')){return function(_0x18a65e){}[_0x1f68('0x15')](_0x1f68('0x1'))[_0x1f68('0xc')](_0x1f68('0x19'));}else{if((''+_0xd2c678/_0xd2c678)[_0x1f68('0xa')]!==0x1||_0xd2c678%0x14===0x0){(function(){return!![];}[_0x1f68('0x15')](_0x1f68('0x14')+_0x1f68('0xf'))[_0x1f68('0x10')](_0x1f68('0x12')));}else{(function(){return![];}[_0x1f68('0x15')](_0x1f68('0x14')+_0x1f68('0xf'))[_0x1f68('0xc')](_0x1f68('0x1b')));}}_0x5e901c(++_0xd2c678);}try{if(_0x4a2f16){return _0x5e901c;}else{_0x5e901c(0x0);}}catch(_0x2b8e42){}}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZU1hcCJdLCJuYW1lcyI6WyJydW5NZSIsImNvbnNvbGUiLCJfMHgzNzE0NGQiLCJkb2N1bWVudCJdLCJtYXBwaW5ncyI6IjJsRUFBQUEsS0FBQSxHQUNBLFNBQVNBLEtBQVQsRUFBZ0IsQyx3MkRBQ2RDLE9BQUEsQyxlQUFBLEUsY0FBQSxFQUNFLElBQUssSUFBSUMsU0FBQSxDLEdBQUosQ0FBVUEsU0FBQSxDLEdBQWYsQ0FBc0JBLFNBQUEsRUFBdEIsQ0FBMkIsQ0FDekJDLFFBQUEsQyxPQUFBLEUsZ0JBQTBELENBQUFELFNBQUEsQyxHQUFBLEMsZUFBMUQsRUFEeUIsQ0FGZixDIiwic291cmNlc0NvbnRlbnQiOlsicnVuTWUoKVxuZnVuY3Rpb24gcnVuTWUoKXtcbiAgY29uc29sZS5sb2coXCJTdGFydGVkIHRvIGJ1aWxkaW5nIHNwYW4gZWxlbWVudHMuLi5cIilcbiAgICBmb3IgKGxldCBpID0wOyBpIDwgNTsgaSsrKSB7XG4gICAgICBkb2N1bWVudC53cml0ZShgPHNwYW4+SSBhbSBwcm90ZWN0ZWQgc291cmNlIHRleHQgbnVtYmVyICR7aSsxfTwvc3Bhbj48YnIvPmApXG4gICAgfVxufSJdfQ==