Created
April 12, 2016 06:58
-
-
Save hasegawayosuke/9963eb63bc42f296a888ae2958c14190 to your computer and use it in GitHub Desktop.
gistのrawコンテンツを取得する
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
/* | |
* https://gist.github.com/username/123456 のようなURLを受け取ると、そのrawコンテンツを取得しcallbackに渡す | |
*/ | |
function retrieveGistContent( gistUrl, callback ){ | |
var t = ""; | |
var _write = document.write; | |
if( !/^https:\/\/gist\.github\.com\/[^\/]+\/[\da-fA-F]+$/.test( gistUrl ) ) return; | |
document.write = function(){ | |
for( var i = 0; i < arguments.length; i++ ){ | |
t += arguments[ i ]; | |
} | |
}; | |
var elm = document.createElement( "script" ); | |
elm.onload = function(){ | |
var m = /(https:\/\/gist\.github\.com\/[^\/]+\/[\da-fA-F]+\/raw\/[\da-fA-F]+\/[^\\"]+)/.exec( t ); | |
if( m !== null ){ | |
var url = m[ 1 ].replace( /^https:\/\/gist\.github/, "https://gist.githubusercontent" ); | |
var xhr = new XMLHttpRequest(); | |
xhr.open( "GET", url, true ); | |
xhr.onload = function(){ | |
callback( xhr.responseText ); | |
}; | |
xhr.send( null ); | |
} | |
document.write = _write; | |
}; | |
elm.src = gistUrl + ".js"; | |
document.body.appendChild( elm ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment