37

I would like to use the WebKit web inspector in a custom browser based on WebKit on OS X. Is it possible to use the web inspector on a webview object? If so, how?

user53937
  • 623
  • 1
  • 9
  • 10

6 Answers6

41

A REALLY easy way to do it... is in the terminal.... ⌘ ⬆ ☺

defaults write com.yourcompany.yourbundleid WebKitDeveloperExtras TRUE

(With OSX 10.8, use defaults write com.yourcompany.yourbundleid WebKitDeveloperExtras -bool true instead).

NOTE: You MUST change com.yourcompany.yourbundleid to YOUR specific "apps" (or whatever company's app bundle ID it may be) before this will work!**

The nice thing about this... there is NO WAY to forget to take it out, turn it off, comment it out, etc... This is a LOCAL setting... and can be set for ANY webkit enabled app... If you don't see a Developer Menu, or whatnot.. fret not..

Right☝ (click) on the Webview and go to "Show Inspector".

Also, along the same lines, the following may do something as well.. but I havent tried it, so not sure..

defaults write com.yourcompany.yourbundleid IncludeDebugMenu 1

Laurent Perrin
  • 14,671
  • 5
  • 50
  • 49
Alex Gray
  • 16,007
  • 9
  • 96
  • 118
  • I had to use `defaults write com.yourcompany.yourbundleid WebKitDeveloperExtras -bool true` for it to work. – thomthom Nov 21 '13 at 14:40
  • It was called "Inspect Element" instead of "Show Inspector," in this day and age, but close enough. – ijoseph Apr 19 '20 at 19:19
  • Where does this bundle identifier come from? I’m just compiling a binary that links against webview, but I’m not providing any bundle identifier to it… – weberc2 Jan 29 '23 at 03:15
36

Set WebKitDeveloperExtras to YES in your default user defaults when you send -[NSUserDefaults registerDefaults:]. This applies app-wide, of course.

Remember that the user can change it to NO, so don't assume that it's YES—if it ever matters (e.g., when customizing the contextual menu), always check.

Warning: This preference doesn't necessarily only enable the Element Inspector. Apple may extend it in the future to also control, say, a Debug menu in your menu bar. You may find this an unacceptable risk.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • This works! Awesome! Side note: It sets the setting on the first load, which doesn't take effect until the app is restarted. Not sure how to get around this -- perhaps supply a default properties file... – user53937 Mar 30 '09 at 02:21
  • 1
    loglibrarian: Set it earlier. If applicationWillFinishLaunching: isn't good enough, set it in your main() function. – Peter Hosey Mar 30 '09 at 06:29
  • Ok I know I'm pushing my luck here, but is there a way to programatically launch the web inspector? Perhaps a private api? – user53937 Mar 30 '09 at 16:49
  • 2
    Private API is private because it will go away someday. (Or become public with no changes, if you're EXTREMELY lucky.) If you're feeling bold, class-dump WebKit and try using the WebInspectorWindowController. Be sure you guard everything so that you fail gracefully if it changes. – Peter Hosey Mar 30 '09 at 17:35
  • Or bolder (?) just download the source from WebKit.org – uchuugaka Sep 20 '15 at 17:39
23

Apple recommends:

defaults write com.example.myApp WebKitDeveloperExtras -bool true

However, as far as I can tell, this may no longer work if you are running your application out of sandbox (which you may need to do during some stages of development). In case you run in to this problem, what worked for me is to set the value for the WebKitDeveloperExtras key in the NSGlobalDomain:

defaults write NSGlobalDomain WebKitDeveloperExtras -bool true
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Milos
  • 2,728
  • 22
  • 24
8

in my case (MacOSX 10.6.5) it didn't work.

I had to do the following in the windowDidLoad method of my webView WindowController:

/* Initialize webInspector. */
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"WebKitDeveloperExtras"];
[[NSUserDefaults standardUserDefaults] synchronize];
Rémy SAISSY
  • 81
  • 1
  • 3
  • I found my app needed to relaunch for this to take effect. Is it that or does this need to happen before the webview is instantiated? – uchuugaka Sep 20 '15 at 17:46
1

I tried doing so, but couldn't see the web inspector.

Isn't it the same as [[NSUserDefaults standarduserDefaults] registerDefaults:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"WebKitDeveloperExtras"]]?

charles
  • 11,212
  • 3
  • 31
  • 46
JongAm Park
  • 623
  • 7
  • 9
  • Minus that ] at the front, yes. – Peter Hosey Dec 09 '09 at 19:12
  • Thanks. From webkit-dev mailing list I received another response. It should be done in its earliest moment like +initialize, or it can be issued on Unix prompt. defaults write com.yourcompany.programname WebKitDeveloperExtras -bool true – JongAm Park Dec 10 '09 at 18:35
0

macOS Ventura 13.3+

There is a new property isInspectable on WKWebView, false by default but turning it on will enable the inspector.

wkWebView.isInspectable = true
vicegax
  • 4,709
  • 28
  • 37