8

How can I pop up Webkit's Web Inspector from my WebView object programmatically?

I succeed to enable Webkit's Web Inspector on my WebView. It's working well, and now I can pop it up by clicking "Inspect Element" on context menu. And I want to do this with my push button. But I couldn't find a proper way to do this. My DOM knowledge is 10 years old, very newbie on HTML DOM of nowadays. Is there any way to do this?

I found a class document: InspectorController. I think this is a kind of key. But I cannot know what object exposes and how can I use this.

Environment:

  • Mac OS X 10.6
  • Xcode 3.2.1 (iPhone SDK, no plug-in)
Community
  • 1
  • 1
eonil
  • 83,476
  • 81
  • 317
  • 516

3 Answers3

11

Here's some code that should help you open it from cocoa programmatically:

@interface WebInspector : NSObject
{
    WebView *_webView;
}
- (id)initWithWebView:(WebView *)webView;
- (void)detach:(id)sender;
- (void)show:(id)sender;
- (void)showConsole:(id)sender;
@end

void MyWebKit::ShowInspector(bool console){
  if ( !m_webView )
      return;

  if( !m_inspector ){
    m_inspector = [[WebInspector alloc] initWithWebView:m_webView];
    [m_inspector detach:m_webView];
  }

  if(console){
    [m_inspector showConsole:m_webView];
  }
  else {
    [m_inspector show:m_webView];
  }
}

To extend it to the dom, just expose this function to JS.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alex MacCaw
  • 984
  • 1
  • 6
  • 19
  • I'm sorry but I cannot understand how I can apply this code to my program. It looks like C++, but I'm not good at C++. – eonil Feb 05 '10 at 20:01
  • @Eonil. I know this is old, but... The above code is based on the Cocoa framework and is written in Objective-C. – Old McStopher Aug 15 '11 at 16:25
  • @Old Oh thanks for commenting. I was newbie on Objective-C too. Now I can understand all the codes above. Thanks again :) – eonil Aug 16 '11 at 09:16
  • I'm not sure how to get this working. I'm getting errors "Variable has incomplete type 'void'", "Expected ';' after top level declarator" and "Expected identifier or '('", all on the first line of 'void MyWebKit::...'. What am I missing? I've never really used code that looks like this. Cheers. – Oliver Cooper Oct 28 '13 at 09:43
5

For those confused by @alex MacCaw's answer (it is c++), here is a more "normal" version..

in your .m file... declare the WebInspector header methods..

@interface WebInspector : NSObject  { WebView *_webView; }
- (id)initWithWebView:(WebView *)webView;
- (void)detach:     (id)sender;
- (void)show:       (id)sender;
- (void)showConsole:(id)sender;
@end

Then in that same file, be it your app delegate, or WebView subclass, or whatever... declare an ivar to "hold your inspector, and make a method to open it, using YOUR web view instance or property, or whatever. ...

@implementation AppController  { WebInspector *_inspector; }

- (IBAction)showInspector:(id)x {
   _inspector = _inspector = [WebInspector.alloc initWithWebView:_myWebView];
  [_inspector      detach:_myWebView];
  [_inspector showConsole:_myWebView];
}
....
Alex Gray
  • 16,007
  • 9
  • 96
  • 118
  • I would like the web inspector to show in its own window. I assume that is what 'detach' is supposed to do. If you look at the [source](http://www.opensource.apple.com/source/WebKit/WebKit-7534.48.3/win/WebInspector.cpp), detach doesn't do anything. Any ideas on how to accomplish this? – bkant Dec 03 '14 at 22:11
3

There is no public API for interacting with the WebInspector via the DOM or Cocoa. You should file an enhancement request at https://bugreport.apple.com/ asking for this API.

InspectorController is also an internal implementation detail of the WebInspector and its likely a bug that its on the documentation website.

Matt Lilek
  • 296
  • 2
  • 6