Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Forked from 140bytes/LICENSE.txt
Created May 14, 2012 10:35
Show Gist options
  • Select an option

  • Save sindresorhus/2693191 to your computer and use it in GitHub Desktop.

Select an option

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

atk commented May 14, 2012

Copy link
Copy Markdown

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
Copy Markdown
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

atk commented May 14, 2012

Copy link
Copy Markdown

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

@skylamer

Copy link
Copy Markdown

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

@williammalo

Copy link
Copy Markdown

You can just do this:

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

Much shorter...

@sindresorhus

Copy link
Copy Markdown
Author

@williammalo You're absolutely right, thanks :)

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

@jed

jed commented May 15, 2012

Copy link
Copy Markdown

how about instead:

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

@subzey

subzey commented May 15, 2012

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown
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

jed commented May 16, 2012

Copy link
Copy Markdown

nice work, @sindresorhus.

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