Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created July 19, 2011 05:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jed/1091363 to your computer and use it in GitHub Desktop.
Save jed/1091363 to your computer and use it in GitHub Desktop.
extract html from embedded github gists
function(
a, // gist url
b, // callback
c, // placeholder
d // placeholder
){
with( // scope
d = document // to the document, cached as `d`,
) body // and to the body
.appendChild( // append an
createElement( // element with nodeName
"script" // `script`
) // and
).src = a, // set its src to the gist url.
c = write, // then cache the `document.write` function,
d.write = function( // and replace it with a function that takes
e // some html,
){ a // which
? a = 0 // ignores it the first time it's called
: d.write = ( // and otherwise reverts document.write
b(e), // (but first calling the callback with the html)
c // back to the original.
)
}
}
function(a,b,c,d){with(d=document)body.appendChild(createElement("script")).src=a,c=write,d.write=function(e){a?a=0:d.write=(b(e),c)}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
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": "extractGist",
"description": "extracts html from embedded github gists",
"keywords": [
"gist",
"embed",
"html"
]
}
<!DOCTYPE html>
<title>extractGist</title>
<link rel="stylesheet" href="https://gist.github.com/stylesheets/gist/embed.css"/>
<div>Expected value:</div>
<div class="gist-data gist-syntax"> <div class="gist-highlight"><pre><div class='line' id='LC1'><span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">,</span><span class="nx">c</span><span class="p">,</span><span class="nx">d</span><span class="p">){</span><span class="kd">with</span><span class="p">(</span><span class="nx">d</span><span class="o">=</span><span class="nb">document</span><span class="p">)</span><span class="nx">body</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">&quot;script&quot;</span><span class="p">)).</span><span class="nx">src</span><span class="o">=</span><span class="nx">a</span><span class="p">,</span><span class="nx">c</span><span class="o">=</span><span class="nx">write</span><span class="p">,</span><span class="nx">d</span><span class="p">.</span><span class="nx">write</span><span class="o">=</span><span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">){</span><span class="nx">a</span><span class="o">?</span><span class="nx">a</span><span class="o">=</span><span class="mi">0</span><span class="o">:</span><span class="nx">d</span><span class="p">.</span><span class="nx">write</span><span class="o">=</span><span class="p">(</span><span class="nx">b</span><span class="p">(</span><span class="nx">e</span><span class="p">),</span><span class="nx">c</span><span class="p">)}}</span></div></pre></div> </div>
<div>Actual value:</div>
<div id="ret"></div>
<script>
var extractGist = function(a,b,c,d){with(d=document)body.appendChild(createElement("script")).src=a,c=write,d.write=function(e){a?a=0:d.write=(b(e),c)}}
extractGist("https://gist.github.com/1091363.js?file=index.js", function(a) {
var div = document.createElement("div")
, ret = document.getElementById("ret")
div.innerHTML = a
ret.parentNode.replaceChild(div.firstChild.childNodes[1].childNodes[1], ret)
})
</script>
@atk
Copy link

atk commented Jul 19, 2011

In some browsers, resetting document.write to its former value removes its scope, which leads to failure. I don't know if the "with(d=document)" can change something about it. I will test this later thoroughly.

@jed
Copy link
Author

jed commented Jul 19, 2011

i'm not sure it's a huge issue even so, since document.write is so seldom used after page load.

@atk
Copy link

atk commented Jul 19, 2011

that's true.

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