-
-
Save jed/1091363 to your computer and use it in GitHub Desktop.
extract html from embedded github gists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "extractGist", | |
"description": "extracts html from embedded github gists", | |
"keywords": [ | |
"gist", | |
"embed", | |
"html" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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">"script"</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> |
i'm not sure it's a huge issue even so, since document.write
is so seldom used after page load.
that's true.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.