View Generated Source in Safari

So we all love the feature that Web Developer gives us under Firefox called “View Generated Source”, as it lets us put some nice Mootools AJAX into our applications and see what we’ve stuffed up during our development cycle (i.e. bad XML, or nothing at all ;)).

I’ve been developing an iPhone Web Application which of course doesn’t render exactly the same under Firefox as it does under Safari/Mobile Safari, so it makes it really hard to debug when you’re trying to get the AJAX requests spot on. So out of frustration I decided to Google it and found the following solution (I can’t recall where exactly I found it, but it was somewhere along the lines of an old cached page, so I’m posting it here not to claim ownership, but hopefully make it really easy for everyone else to find!

View Generated Source (AppleScript)

global jsCode
global htmlSource
set jsCode to "document.documentElement.outerHTML"
tell application "Safari"
	set htmlSource to (do JavaScript jsCode in front document)
end tell
tell application "TextEdit"
	activate
	make new document
	set text of front document to htmlSource
end tell

This will run pretty quickly, and I compiled mine as an application (From the AppleScript editor, it’s under File > Save As, then an option in format to save as), which takes a few moments to launch the first run, then you’re away racing!

I do use TextMate but couldn’t work out how to get it to use that, but that’s down to my lack of AppleScript knowledge. If you do know, please post a comment and I’ll edit this post with that version as well!

Enjoy!


About this entry