View the generated source of a page. 72 bytes.
Alternative version with non-english support (96 bytes):
function(){open('data:text/plain;charset=utf-8,'+encodeURI(document.documentElement.outerHTML))}
function(){ | |
open( | |
'data:,'+encodeURI( | |
document.documentElement.outerHTML | |
) | |
) | |
} |
function(){open('data:,'+encodeURI(document.documentElement.outerHTML))} |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2012 Sindre Sorhus <http://sindresorhus.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
{ | |
"name": "viewSource", | |
"description": "View the generated source of a page.", | |
"keywords": [ | |
"140bytes", | |
"view", | |
"source", | |
"html" | |
] | |
} |
<!doctype html> | |
<title>viewSource</title> | |
<script> | |
var viewSource = function(){open('data:,'+encodeURI(document.documentElement.outerHTML))}; | |
viewSource(); | |
</script> |
Doesn't need to show doctype, since it's only for viewing, though I could of course just prepend it.
outerHTML is supported in every browser that is usually supported, but you don't specify which you require:
Feature | Firefox (Gecko) | Chrome | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 11.0 (11) | 0.2 | 4.0 | 7 | 1.3 |
That only leaves out pre-11 Firefox Versions, which are still in use out there. Nice enough.
or just use view-source:<url>
:D)
You can just do this:
function(){open('data:,'+encodeURI(document.documentElement.outerHTML))}
Much shorter...
@williammalo You're absolutely right, thanks :)
I have no idea why my brain insisted on double escaping...
how about instead:
function(){with(document)body.innerText=documentElement.outerHTML}
Data URL's default MIME is text/plain;charset=us-ascii
while encodeURI
returns escaped Utf-8 string. So non-english text will most probably be “broken”.
Use it as a bookmarklet:
<!doctype html>
<title>viewSource</title>
<a href="javascript:(function(){open('data:,'+encodeURI(document.documentElement.outerHTML))})()">view source</a>
nice work, @sindresorhus.
Doesn't show DOCTYPE; outerHTML not supported in all browsers. It could be possible to re-get the source by ajax request, yet this only works for those pages who are obtainable via GET request.