Skip to content

Instantly share code, notes, and snippets.

@tec27
Created January 21, 2012 23:42
Show Gist options
  • Save tec27/1654534 to your computer and use it in GitHub Desktop.
Save tec27/1654534 to your computer and use it in GitHub Desktop.
Gist Link -> Gist Embed for TeamLiquid (greasemonkey/userJS)
// ==UserScript==
// @name LiquidGist
// @description Turns Github Gist links on TeamLiquid threads into Gist embeds automatically
// @author tec27
// @version 0.3
// @include http://www.teamliquid.net/forum/viewmessage.php*
// @include http://teamliquid.net/forum/viewmessage.php*
// @include http://www.teamliquid.net/forum/postmessage.php
// @include http://teamliquid.net/forum/postmessage.php
// ==/UserScript==
var liquidGist = function($) {
"use strict";
function installPlugin() {
var loadStyles = function(linkTag) {
$('head').first().append(linkTag);
};
var gistIdRegex = /^https:\/\/gist\.github\.com\/(\d+)$/;
// this function can handle any number of links, and will do them in a serial fashion
// (first one finishes -> embed the next one) using the same iframe. I originally wrote this
// to be the jQuery plugin, but I think its better to try and request all the gists at once
// instead. If anyone wants to use the serial method, you can simply wrap it with a function
// that calls doEmbedGists(this) and it will work out fine
function doEmbedGists(gistLinks) {
if(!gistLinks.length) return;
var curLink;
var iFrame = $('<iframe src="about:blank"></iframe>').hide().prependTo('body');
var iFrameDoc = iFrame[0].contentWindow.document;
// monkeypatch for first document.write
var writeStyle = function(linkTag) {
if(loadStyles) {
loadStyles(linkTag);
loadStyles = null; // ensure loadStyles is only called once
}
iFrameDoc.write = writeGist;
};
// monkeypatch for second document.write
var writeGist = function(gistContent) {
curLink.replaceWith(gistContent);
if(gistLinks.length)
return injectGist(); // inject the script for the next link
else
return done();
};
// used to inject each Gist into the iFrame
var injectGist = function() {
var result;
iFrameDoc.write = writeStyle;
console.dir(iFrameDoc);
do {
curLink = $(Array.prototype.shift.call(gistLinks));
result = gistIdRegex.exec(curLink.attr('href'));
} while(gistLinks.length && (!result || !result[1]));
if(!gistLinks.length && (!result || !result[1])) return done();
var gistId = result[1];
var gistScript = iFrameDoc.createElement('script');
gistScript.type = 'text/javascript';
gistScript.src = 'https://gist.github.com/' + gistId + '.js';
iFrameDoc.body.appendChild(gistScript);
};
var done = function() {
iFrame.remove();
};
injectGist();
return this;
};
$.fn.embedGists = function() {
return this.each(function(i, el) {
var gistLinks = [];
gistLinks.push(el);
doEmbedGists(gistLinks);
});
}
}
installPlugin();
$(document).ready(function() {
$('a[href^="https://gist.github.com/"]').embedGists();
});
};
(function() {
"use strict";
var el = document.createElement('script');
el.type = 'text/javascript';
el.textContent = '(' + liquidGist.toString() + ')(window.jQuery);';
document.body.appendChild(el);
})();
@tec27
Copy link
Author

tec27 commented Jan 21, 2012

Works with Opera and Chrome. Isn't working with Firefox for some non-apparent reason. It seems to download the js file from github, but then my document.write hooks never get called.

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