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

喜夏-厌秋 提交于 2019-11-30 02:26:05

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.

Alex MacCaw

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.

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];
}
....
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!