I need to write a page which does not allow the user to see the code. To catch the context menu open I want to use the JS event. To catch web-console open I want to use this: https://sindresorhus.com/devtools-detect/ and close the page. How to catch saving? Is it enough to catch Ctrl+S?
2 Answers
No, trapping ctrl + s
will only solve the case where the user uses a keyboard shortcut (be sure to remember it's cmd+s for Macs). They will still get the save webpage dialog if they use the File Menu
or the context menu's "Save As" action. I have never heard of a way to trap those.
You will never be able to prevent the user from seeing your code, if the browser can download and execute it, the user can see it, whether through curl
, a packet sniffer or DevTools.
The best you can do is obfuscate your code, which anyone with enough skills will be able to turn in to non-obfuscated code.
We can listen for keydown. If the ctrlKey and S key are being pressed, do the work. The preventDefault is preventing the save page dialog from opening up.
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 's') {
e.preventDefault();
console.log('pressing ctrl+s')
}
});
But preventing a user from seeing your code? That's a different story. We can detect Ctrl+S, but not prevent someone from seeing your code.
curl
orwget
from the command line. Or they can use a packet capture tool while the browser is accessing the page. – Barmarctrl+s
– Juan Mendes