Reputation: 8409
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
<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>
Upvotes: 9
Views: 96493
Reputation: 1447
key that need to be stop script
document.onkeydown = function(e) {
if(event.keyCode == 123) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.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;
}
}
now in the body element paste/write
<body oncontextmenu="return false;">
Upvotes: -1
Reputation: 1
<script type="text/javascript">
var element = new Image;
var devtoolsOpen = false;
element.__defineGetter__("id", function()
{
devtoolsOpen = true;
window.location.replace("http://www.w3schools.com");
// This only executes when devtools is open.
});
setInterval(function() {
devtoolsOpen = false;
console.log(element);
}, 1000);
</script>
Upvotes: -1
Reputation: 1
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.
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==
Upvotes: -1
Reputation: 9
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;
});
}
Upvotes: -1
Reputation: 8496
You cant restrict view-source, but any way you can put javscript and css compressed file.
Dont bind Event from Inline attribute of HTML Dont bind event from inline attribute in html, For example
<button id="btn_callme" onclick="callme(this)"> Call Me</button>
instead of this you can bind event in javascript code.
<button id="btn_callme"> Call Me</button>
$(document).on('click', '#callme', function(){
// write code here
})
Combine Common Javascript files into one
If you use multiple javascript as common javascript you can combined it in one file and minify it, it will make some faster execution.
Compress/minify Javascript
For security of Javascript code you can compress it from online tools like http://javascriptcompressor.com/
Compress/Minify CSS code
For security of CSS code you can compress it from online tools like 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_mode
class in body tag for allowed execute from console.
(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;
}
});
}
})();
Upvotes: 4
Reputation: 19
Try this code put the code on the 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>
Upvotes: 0
Reputation: 41
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.
Upvotes: 0
Reputation: 572
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.
Upvotes: 1
Reputation: 50291
There is no way to hide source code & inspect element, but all you can do is disable
the the right click
of mouse button using
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 F12
key since pressing F12
open the developers console, which is actually bypass right click event
Upvotes: 1
Reputation: 1930
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.
Use https://jscompress.com/ if you have a static JS-Script
Upvotes: 6