Guest User

Untitled

a guest
Apr 12th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. // non-blocking GitHub Gist embed code jQuery plugin
  2. // usage:
  3. // 1. <div data-gist=your_gist_id><a href="http://gist.github.com/your_gist_id">your_gist_filename on GitHub Gist</a></div> in html
  4. // 2. $('<div/>').embedGist(your_gist_id).appendTo('article'); in javascript
  5. ;(function ($) {
  6. $.fn.embedGist = (function () {
  7. var gistWriteFunc = {},
  8. gistWrited = {},
  9. addGist = function (gistId, $el) {
  10. if (!gistWriteFunc[gistId]) {
  11. gistWriteFunc[gistId] = function (str) {
  12. $el.empty();
  13. $(str).appendTo($el);
  14. gistWrited[gistId] = true;
  15. };
  16. $('head').append('<script src="https://gist.github.com/' +
  17. gistId + '.js"></script>');
  18. }
  19. };
  20.  
  21. document.origWrite = document.write;
  22. document.write = function (str) {
  23. var match, gistId;
  24. if (str.indexOf('<link rel="stylesheet" href="https://gist.github.com/stylesheets/gist/embed.css"/>') === 0) {
  25. if (!$('#gist-embed-css').length) {
  26. $(str).attr('id', 'gist-embed-css').appendTo('head');
  27. }
  28. } else if ((match = str.match(/div id="gist-(\d+)"/)) != null) {
  29. gistId = match[1];
  30. if (!gistWrited[gistId] && gistWriteFunc[gistId]) gistWriteFunc[gistId](str);
  31. } else {
  32. document.origWrite.apply(document, arguments);
  33. }
  34. };
  35.  
  36. return function (gistId) {
  37. if (typeof gistId != 'undefined') {
  38. addGist(gistId, $(this));
  39. } else {
  40. $(this).each(function (i, el) {
  41. var $el = $(el),
  42. gistId = $el.data('gist-id');
  43. if (!gistId) return;
  44. addGist(gistId, $el);
  45. });
  46. }
  47. return this;
  48. };
  49. }());
  50. $(function () {
  51. $('div[data-gist-id]').embedGist();
  52. });
  53. }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment