Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Forked from 140bytes/LICENSE.txt
Created May 14, 2012 10:35
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sindresorhus/2693191 to your computer and use it in GitHub Desktop.
Save sindresorhus/2693191 to your computer and use it in GitHub Desktop.
View source - view the generated source of a page (140byt.es)

View source (140byt.es)

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>
@atk
Copy link

atk commented May 14, 2012

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.

@sindresorhus
Copy link
Author

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

@atk
Copy link

atk commented May 14, 2012

That only leaves out pre-11 Firefox Versions, which are still in use out there. Nice enough.

@skylamer
Copy link

or just use view-source:<url> :D)

@williammalo
Copy link

You can just do this:

function(){open('data:,'+encodeURI(document.documentElement.outerHTML))}

Much shorter...

@sindresorhus
Copy link
Author

@williammalo You're absolutely right, thanks :)

I have no idea why my brain insisted on double escaping...

@jed
Copy link

jed commented May 15, 2012

how about instead:

function(){with(document)body.innerText=documentElement.outerHTML}

@subzey
Copy link

subzey commented May 15, 2012

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”.

@fhemberger
Copy link

Use it as a bookmarklet:

<!doctype html> 
<title>viewSource</title>
<a href="javascript:(function(){open('data:,'+encodeURI(document.documentElement.outerHTML))})()">view source</a>

@sindresorhus
Copy link
Author

@jed I could, but I wanted it to open in a new window, so not to destroy the site you're on. Firefox doesn't support innerText, so you would need to use textContent.

@subzey I didn't know that, but you are correct. Added an alternative version.

@jed
Copy link

jed commented May 16, 2012

nice work, @sindresorhus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment