27

I'm looking for a way to open the WebKit “developer tools” from a script attached to a web-page. I need solutions for both Google Chrome and Safari, that will open the developer-tools pane if it's not already open, and (hopefully, if you can figure out how) also switch to a particular tab/section of said pane upon opening.

(Use-case, if anyone's interested: I want to open the console.log output-window if there's been an error and a developer is looking at the page; this particular page will be the output of some JavaScript unit-tests.)


I'm setting a bounty on this question because it's obviously one that hasn't been answered to anyone's satisfaction before, and the answer is a hairy one. Please don't answer it unless you have a real answer that both: 1) works in both browsers, and 2) doesn't require private extension APIs that won't work from a static web-page.

See (related, but specific to Chrome, and extensions): Can I programmatically open the devtools from a Google Chrome extension?

4
  • 12
    If it would be possible, it should be fixed. Web pages must not have access to the browser's interface. You should think about writing an extension or standalone application for this. May 28, 2013 at 18:43
  • 1
    If doing so via an extension is all that's possible, that's still an acceptable answer; as long as methodologies are provided for all common browsers, so that I can provide extension-shims that expose a single interface across them all. May 29, 2013 at 1:20
  • 2
    Rather than opening the developer tools, have you thought of simply injecting a popout with the console log output in it when in your dev environment? This is what we do, for both JS errors and performance data. When running in a dev environment, we inject a small popout at the bottom of the page that the developer can click on to expand it and see the log as well as the round trip times to the server for each call (both regular postback and AJAX). May 30, 2013 at 18:11
  • Riateche is right. What you ask may ease debugging of code but it poses a security risk for browsers, even with plugins or any other methods. Browser is not sufficient for development needs like you ask. You should use IDE for that.
    – user568109
    Jun 1, 2013 at 4:41

6 Answers 6

19

Simply: You can't.

The Dev Tools are not sandboxed (unlike any web page), thus granting sandboxed environments the power to open and control an unsandboxed environment is a major security design flaw.

I hope this answers your question :-)

2
  • Closer than anything else. I can't see how it would be a security flaw to allow opening of an unsandboxed environment, though. (I never suggested that allowing control if it was a good idea.) Jun 6, 2013 at 19:44
  • @elliottcable If you can open, you can also control (because it means you have actual access to the object representing the dev panel, thus an exploit could possibly made to inject data in it). Jun 7, 2013 at 14:02
10
+150

You cannot directly use the Chrome's Dev Tools from your web pages. It is bundled with the browser.

But you can use it like a regular web application. Go to Chrome Developer Tools, then go to Contributing. You will find help on using Dev Tools for your app.

Setting up

  • Install Chrome Canary on Mac OS / Windows or download the latest Chromium build from the Chromium continuous builds archive on Linux
  • Clone Blink git repo from https://chromium.googlesource.com/chromium/blink.git
  • Set up a local web server that would serve files from WebKit/Source/WebCore/inspector on some port (8090)

Running

  • Run one copy of Chrome Canary with the following command line flags: --remote-debugging-port=9222 --user-data-dir=blink/chromeServerProfile --remote-debugging-frontend="http://localhost:8090/front_end/inspector.html". These flags cause Chrome to allow websocket connections into localhost:9222 and to serve the front-end UI from your local git repo. (Adjust the path to chromeServerProfile to be some writable directory in your system).
  • Open a sample page (eg www.chromium.org).
  • Run a second copy of Chrome Canary with the command line flag: --user-data-dir=/work/chromeClientProfile. Open http://localhost:9222. Among the thumbnails you will see the sample page from the other browser instance. Click on it to start remote debugging your sample page.
  • The DevTools web page that opens is served from the remote-debugging-frontend in the first browser instance, which serves from the git repo your local filesystem. Debug this Devtools Web page and edit its source like any other web app.

I hope this is what you need.

3
  • This answer is for debugging server-side JavaScript code, as far as I can tell, no? That's unrelated: the goal is to open Chrome DevTools, FireBug, etceteras from within a webpage in-development. i.e. when there's been an error. Jun 1, 2013 at 4:03
  • It is actually remote debugging. You can view the Dev Tools debugger by opening the URL of the started server. It is the closest I could find.
    – user568109
    Jun 1, 2013 at 4:35
  • To the downvoter. Can you leave a comment to explain the downvote.
    – user568109
    Jun 7, 2013 at 3:40
5

There's no way to control the web developer tool from an in-page script, other than through the Console API which provides mostly logging facilities. Letting scripts control more than that would be a serious security issue, since it would allow a web page to control parts of the browser.

The only API remotely related to what you're trying to do is the debugger command, which switches to the script pane only if the developer tools were already open.

But who are you trying to develop this feature for?

If it's for developers working on the site, then it's better to just use the existing developer tools manually, by setting breakpoints, or the pause on exceptions toggle.

If it's for end users, don't. Unless your site is supposed to be used by highly technical web developers, you're only going to scare away users if the developer tools suddenly pop up with errors.

If you really want to show errors you can implement your own logging framework and the UI for error reporting, which works with basic JS and doesn't depend on a specific browser environment.

2
  • X/Y description: the web-page for the results of some client-side tests. If there have been errors in the tests, it would be ideal to have the page open the developer-tools for me. Jun 6, 2013 at 19:47
  • For teaching purposes it would be cool to have a button you could press to open devtools, but certainly not essential.
    – jtr13
    Dec 5, 2019 at 22:51
0

here's another answer that proposes a solution to your mentioned use case/objective (detecting errors, getting & displaying console logs) and not the not possible objective in the title.

you can make and use a console wrapper and use it in your code and/or you can monkey patch the console functions if you use/import external js, but you need to apply it before loading them.

0

No, Any secure Browser will not allow a script to open an extension, as it leads to insecurity.

But, You may design an Add-On/extension OR Console API's to do the same.. for specific site.

Create an Add-On like this to achieve that requirement.

You can try sending keys 'CTRL' + SHIFT' + 'I' that may work for Chrome any FireFox (in I.E you need to use 'F12'

I am using it when required as few utils in this add-on use to work better then the built-in.

EDIT: Now a days Chrome is advanced with many new advancements source.

I hope this helps!

-1

Hate to answer such an old question, but was surprised to not see this as an answer, so I thought I'd add it in case it can help someone in the future.

Assuming you have access to the source code, you can place an alert("open devtools"); statement immediately before the first line you're interested in debugging. This alert will give you an opportunity to open DevTools and set a breakpoint on that first line before clearing the alert thus allowing the code to continue and hitting the breakpoint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.