Last active
December 27, 2015 08:29
-
-
Save lsauer/7296307 to your computer and use it in GitHub Desktop.
Load github's embedded-gists in Google Blogger's Dynamic View templates, automatically
This file contains hidden or 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
// http://www.lsauer.com, lo sauer 2013 | |
//descr: load embedded-gists in Google Blogger's Dynamic View templates | |
//credits: https://github.com/kares/script.js for 'script.js' a helper to readout script's contents | |
//depends: jQuery to some extend, but dependence can be easily removed | |
//usage: e.g. following HTML can be used to load gists: | |
// <code data-src="https://gist.github.com/lsauer/6397381.js">Loading ....</code> | |
// <code data-src="https://gist.github.com/lsauer/2836373.js">Loading ....</code> | |
var timeoutfnLoadgist = function() { | |
var out = {}; | |
var finishedHandler = function(){ | |
console.log("finished loading", out); | |
$("code[data-src]").each(function(idx,el){ $(el).before(out[el.getAttribute('data-src')]);$(el).remove(); }); | |
}; | |
(function(self, out, fnfinished, sel){ | |
sel = sel || "code[data-src]"; | |
var scriptcnt = 0; | |
var DOMLoadScript = function(src, toel, fn){ //see islib v1.0 | |
var loadedHandler = function(){ | |
out[arguments.callee.target.src] = buffer; | |
buffer = ''; scriptcnt--; | |
if(scriptcnt==0){ | |
fnfinished(); | |
toggleWriter(true); | |
} | |
}; | |
var e = self.createElement('script'); | |
e.setAttribute('data-injected', 'true'); | |
e.setAttribute('type', 'text/javascript'); | |
e.setAttribute('src', src); | |
loadedHandler.target = e; | |
e.addEventListener('load', loadedHandler, false); | |
if(!toel){ toel = 'body' }; | |
var tgtel = (typeof toel == "string" ? self.querySelector(toel).parentNode : toel.parentNode ); | |
var attronload = tgtel.getAttribute('onload'); | |
if(attronload){ | |
e.setAttribute('onload', attronload); | |
} | |
(typeof toel == "string" ? self.querySelector(toel): toel).appendChild(e); | |
return e; | |
} | |
var buffer = ''; | |
var tmpwr = {write: self.write, writeln: self.writeln}; | |
var docwr = {write: function(s){buffer+=s;}, writeln: function (s){buffer+=s+'\n';}}; | |
function toggleWriter(isnative){ | |
isnative = isnative || 0; | |
self.write = isnative ? tmpwr.write : docwr.write; | |
self.writeln = isnative ? tmpwr.writeln : docwr.writeln; | |
} | |
toggleWriter(false); | |
$(sel).each(function(idx,el){ | |
scriptcnt++; | |
DOMLoadScript(el.getAttribute('data-src'), el); | |
}); | |
})(document, out, finishedHandler); | |
} | |
setTimeout(timeoutfnLoadgist, 1200); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment