1
votes

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
"I need to write a page which does not allow the user to see the code." That's not possible. I can download and analyze your code with wget, curl or Postman.jabaa
Even if y ou can catch these, someone who wants to get the code can simply use curl or wget from the command line. Or they can use a packet capture tool while the browser is accessing the page.Barmar
It is somewhat possible if you write css within the headers, but it's wacky and very very limited.mstephen19
sindresorhus.com/devtools-detect doesn't work for floating Dev Tools. Open Dev Tools, open the menu in the top right corner and select floating window.jabaa
@lith.al Please fix your question title, it doesn't seem like you're asking about how to detect ctrl+sJuan Mendes

2 Answers

0
votes

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.

-2
votes

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.