Skip to content

Instantly share code, notes, and snippets.

@hasegawayosuke
Created April 12, 2016 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasegawayosuke/9963eb63bc42f296a888ae2958c14190 to your computer and use it in GitHub Desktop.
Save hasegawayosuke/9963eb63bc42f296a888ae2958c14190 to your computer and use it in GitHub Desktop.
gistのrawコンテンツを取得する
/*
* 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