12

How to open the browser's JavaScript console with JavaScript code?

if (ShowConsole() == false)
   alert('I cannot open your JavaScript console, please "
   + "use your browser\'s menu to open manually.')
else console.log('now you can see some message')

I suggest ShowConsole() function does not exist and is a product of my imagination, you need to go to menus and find the right item and click it. And do it different way in different browser. I am looking for some browser specific API to help lazy developers.

By "open" I mean make it visible, as if the user selected the corresponding menu command.

exebook
  • 27,243
  • 27
  • 105
  • 196
  • 4
    **product of my imagination** nice thought – Deepak Ingole Feb 25 '14 at 04:50
  • Might be interested in http://stackoverflow.com/questions/7798748/find-out-whether-chrome-console-is-open – Deepak Ingole Feb 25 '14 at 04:53
  • 1
    Sadly, I don't think it's possible to open devtools programmatically in a normal browser. If you're making an Electron app, you can use [toggleDevtools](https://electronjs.org/docs/api/web-contents#contentstoggledevtools). – Brady Dowling Apr 29 '19 at 14:06
  • 3
    You can't and you shouldn't either, if the user needs to see something use alert() otherwise log it to a file. Dev tools are for debugging not end user experience – Sam Stephenson Jun 12 '19 at 15:00

1 Answers1

1

I cannot find any way to do this, so my impression is that this is explicitly designed as a security measure. After all, just try to go to Facebook.com and open the console. What do you see?

STOP!

This is a browser feature intended for developers. If someone told you to copy-paste something here to enable a Facebook feature or "hack" someone's account, it is a scam and will give them access to your Facebook account.

If it's a security risk, why give any developer the ability to open any security window for any user?

Theoretically, if this wasn't a problem, all you'd need to do is: <a href="chrome://inspect">Inspect!</a>. And Chrome even has initialization mode with a --allow-file-access-from-files parameter to allow greater developer freedom. This working would make sense to me -- If someone started chrome.exe with a flag indicating developer tools at the command line, it's quite clearly not some user who was tricked into opening their console, so, it should give us greater permissions.

Unfortunately, absolutely everything I have tried fails, and this gives me the impression that it's a hardcore security protocol for Google, and they won't be working anytime soon. The results to be exact...

  • <a href="chrome://inspect">Inspect!</a> : Console results with error...

Not allowed to load local resource: filename...

  • <a href="file://chrome://inspect">File!</a>: Redirects to about:blank#blocked.

  • C:\Program Files (x86)\Google\Chrome\Application>chrome.exe --allow-file-access-from-files : This appeared to have no effect at all.

The only real remaining possibility is: a Chrome plugin that allows href="chrome://..." links. I have searched and tried several, none have produced any desirable results. But the possibility is there! You said this was to help lazy developers -- this is what it will take to do a proper accommodation to laziness.

HoldOffHunger
  • 10,963
  • 6
  • 53
  • 100