Github Gist Share

Share your GitHub Gist to Twitter, Dabblet & as userscript.

As of 2014-11-04. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

  1. // ==UserScript==
  2. // @name Github Gist Share
  3. // @namespace https://github.com/jerone/UserScripts/
  4. // @description Share your GitHub Gist to Twitter, Dabblet & as userscript.
  5. // @author jerone
  6. // @copyright 2014+, jerone (http://jeroenvanwarmerdam.nl)
  7. // @license GNU GPLv3
  8. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Gist_Share
  9. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Gist_Share
  10. // @include *://gist.github.com/*
  11. // @version 4.4
  12. // @grant none
  13. // ==/UserScript==
  14. /* global unsafeWindow */
  15.  
  16. (function() {
  17.  
  18. String.format = function(string) {
  19. var args = Array.prototype.slice.call(arguments, 1, arguments.length);
  20. return string.replace(/{(\d+)}/g, function(match, number) {
  21. return typeof args[number] !== "undefined" ? args[number] : match;
  22. });
  23. };
  24.  
  25. var socials = {
  26. Twitter: {
  27. show: function(/*url, user, description, files, stars, forks, revisions*/) { return true; },
  28. submit: function(url, user, description, files, stars, forks, revisions) {
  29. var stats = [];
  30. if (files > 1) {
  31. stats.push(files + " files");
  32. }
  33. if (stars === 1) {
  34. stats.push(stars + " star");
  35. } else if (stars > 1) {
  36. stats.push(stars + " stars");
  37. }
  38. if (forks === 1) {
  39. stats.push(forks + " fork");
  40. } else if (forks > 1) {
  41. stats.push(forks + " forks");
  42. }
  43. if (revisions > 1) {
  44. stats.push(revisions + " revisions");
  45. }
  46.  
  47. var tweet = String.format("Check out {0} #gist {1} on @github{2} |",
  48. user === document.querySelector(".name").textContent.trim() ? "my" : user + "'s",
  49. description ? "\"" + description + "\"" : "",
  50. stats.length > 0 ? " | " + stats.join(", ") : "");
  51.  
  52. return "https://twitter.com/intent/tweet?original_referer=" + encodeURIComponent(url) +
  53. "&source=tweetbutton&url=" + encodeURIComponent(url) +
  54. "&text=" + encodeURIComponent(tweet);
  55. },
  56. icon: "https://si0.twimg.com/favicons/favicon.ico"
  57. },
  58. Dabblet: {
  59. /*
  60. * The following urls should be converted to dabblet:
  61. * _______
  62. * - https://gist.github.com/jerone/3810309
  63. * _______
  64. * - https://gist.github.com/jerone/3810309/revisions
  65. * _______
  66. * - https://gist.github.com/jerone/3810309/forks
  67. * _______
  68. * - https://gist.github.com/jerone/3810309/stars
  69. * ________________________________________________
  70. * - https://gist.github.com/jerone/3810309/f2815cc6796ea985f74b8f5f3c717e8de3b12d37
  71. * ________________________________________________
  72. * - https://gist.github.com/3810309/f2815cc6796ea985f74b8f5f3c717e8de3b12d37
  73. *
  74. */
  75. show: function(/*url, user, description, files, stars, forks, revisions*/) { return true; },
  76. submit: function(url, user/*, description, files, stars, forks, revisions*/) {
  77. var linkLong;
  78. if ((linkLong = document.querySelector(".site-container.js-site-container")) && linkLong.dataset.url) {
  79. var linkLongParts = linkLong.dataset.url.split("/");
  80. linkLongParts.shift();
  81. if (/^(?:revisions|forks|stars)$/gi.test(linkLongParts[linkLongParts.length - 1])) {
  82. linkLongParts.pop();
  83. }
  84. if (new RegExp(user, "gi").test(linkLongParts[0])) {
  85. linkLongParts.shift();
  86. }
  87. url = "/" + linkLongParts.join("/");
  88. } else {
  89. url = url.replace(new RegExp("https?:\/\/gist.github.com/" + user, "gi"), "");
  90. }
  91. return "http://dabblet.com/gist" + url;
  92. },
  93. icon: "http://dabblet.com/favicon.ico"
  94. },
  95. UserScript: {
  96. show: function(/*url, user, description, files, stars, forks, revisions*/) {
  97. return !!document.querySelector(".file[id^='file-'] .raw-url[href$='.user.js']");
  98. },
  99. submit: function(/*url, user, description, files, stars, forks, revisions*/) {
  100. return (document.querySelector(".file[id^='file-'] .raw-url[href$='.user.js']") || { href: "" }).href.trim();
  101. },
  102. icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAKwSURBVHjabJNJTBNRGID/mc5MQYVWVNCGTbEtNZGDBj1ogolEMR5UJA2LBmMoIokxERIj8ehJjx6MYIQoJgq4JIa6gEARkKJFTa2iFFtKWwp2oeDCzNQ+31DQCc5L/nmT/P/3749ACAFBECBxiEPFFds0Ws399DRVhtX2udc97ig0PmgOLBkIbOwjAR8uMRRdvXF7pqv/NfrqnEAOlxsdLas6j3Wk2AEpCRcbKvLydrdu1WUr0lXrITEhAZKUSkhQKvKwXiY2ppbDRzCcv29P/ZZsDaSqUkCJYVJGwKMnHTDlmWgTZ/CvjkW4sKTScP1WC+oZsKAxpwv5gyEUnAkj2xc70p88Y8Y2a8VBxT0gispOGa413UVDb23IMe6OwaEw+jTqQKMOF3pptqBSw7k74hLEPaDUOu0VmpFDV58ZCJIAkiDB5fUBz0eApmjQqbOgrqa69HhVbZO4jKUfmiBJBctysHJFPPiDYbA7J4DjeJDLaWAYGVAyErIy0uDs6RPH9OXVtULWYgfEmN3emJK8BlYrEsHl8cEvloX4ODnEyRlgKGZhV1iOhcz0VNixM7dOCCp2EBkeMF3u6DaNqDasg1U4CzlFxxSRKMyz8xjmsPAQwNmRsc2jxGPkR0esHp7n9RBFrYbyUi1DUzh1GujFG0UBQrNz8P7DR3j+9NklqTEK3VVkbNLkVNZc9AwNW5Hb60PT/gCamg6gEbsT3XvYjvIP6i9gu2ShhOWb+BvLD13O9o3azWrVdy4K3wKhv5HfWW1Q39BY19nechPbzQrVwX9bhU+iIqnyQMF+mPvJQr/FCsHwDJgG30ADhl8Y2wQ4jIUVkpdaZRnPcd6AfxomJ32AIhEwdvaC8XG7JLwwvmXPmVFn52Tu2lvQjN9Crn3M6bWY+6otr3oGpWCB/SPAAJaJRguGUxB0AAAAAElFTkSuQmCC"
  103. }
  104. };
  105.  
  106. function addMenuItem() {
  107. var link, url, menu, li, user, description, files, stars, forks, revisions;
  108.  
  109. if ((link = document.querySelector(".js-current-repository")) && (menu = document.querySelector('.sunken-menu-group'))) { // check if we're on an actual gists;
  110. url = link.href;
  111. user = document.querySelector(".author.vcard").textContent.trim();
  112. description = (document.querySelector(".gist-description") || link || { textContent: "" }).textContent.trim();
  113. files = document.querySelectorAll(".file[id^='file-']").length;
  114. stars = parseInt((menu.querySelector("a[href$='/stars'] .counter") || { textContent: "0" }).textContent.trim(), 10);
  115. forks = parseInt((menu.querySelector("a[href$='/forks'] .counter") || { textContent: "0" }).textContent.trim(), 10);
  116. revisions = parseInt((menu.querySelector("a[href$='/revisions'] .counter") || { textContent: "0" }).textContent.trim(), 10);
  117.  
  118. menu.appendChild(li = document.createElement("li"));
  119. li.id = "Github_Gist_Share";
  120.  
  121. for (var key in socials) {
  122. if (socials.hasOwnProperty(key)) {
  123. var social = socials[key],
  124. socialA = document.createElement("a"),
  125. socialImg = document.createElement("img");
  126.  
  127. if (social.show(url, user, description, files, stars, forks, revisions) !== true) { continue; }
  128.  
  129. li.appendChild(socialA);
  130. socialA.appendChild(socialImg);
  131. socialA.id = String.format("{0}_{1}", li.id, key.replace(/\s+/g, "_"));
  132. socialA.classList.add("sunken-menu-item");
  133. socialA.href = social.submit && social.submit(url, user, description, files, stars, forks, revisions);
  134. socialA.title = String.format("[{0}] {1}", key, socialA.href);
  135. socialA.style.display = "inline-block";
  136. socialA.target = "_blank";
  137. socialImg.src = social.icon;
  138. socialImg.alt = key;
  139. }
  140. }
  141. }
  142. }
  143.  
  144. // init;
  145. addMenuItem();
  146.  
  147. // on pjax;
  148. unsafeWindow.$(document).on("pjax:success", addMenuItem);
  149.  
  150. })();