Github Gist Share

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

As of 2014-05-29. 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.2
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16.  
  17. String.format = function(string) {
  18. var args = Array.prototype.slice.call(arguments, 1, arguments.length);
  19. return string.replace(/{(\d+)}/g, function(match, number) {
  20. return typeof args[number] != "undefined" ? args[number] : match;
  21. });
  22. };
  23.  
  24. var socials = {
  25. Twitter: {
  26. show: function(url, user, description, files, stars, forks, revisions) {
  27. return true;
  28. },
  29. submit: function(url, user, description, files, stars, forks, revisions) {
  30. var stats = [];
  31. if (files > 1) {
  32. stats.push(files + " files");
  33. }
  34. if (stars == 1) {
  35. stats.push(stars + " star");
  36. } else if (stars > 1) {
  37. stats.push(stars + " stars");
  38. }
  39. if (forks == 1) {
  40. stats.push(forks + " fork");
  41. } else if (forks > 1) {
  42. stats.push(forks + " forks");
  43. }
  44. if (revisions > 1) {
  45. stats.push(revisions + " revisions");
  46. }
  47.  
  48. var tweet = String.format("Check out {0} #gist {1} on @github{2} |",
  49. user == document.querySelector(".name").textContent.trim() ? "my" : user + "'s",
  50. description ? "\"" + description + "\"" : "",
  51. stats.length > 0 ? " | " + stats.join(", ") : "");
  52.  
  53. return "https://twitter.com/intent/tweet?original_referer=" + encodeURIComponent(url) +
  54. "&source=tweetbutton&url=" + encodeURIComponent(url) +
  55. "&text=" + encodeURIComponent(tweet);
  56. },
  57. icon: "https://si0.twimg.com/favicons/favicon.ico"
  58. },
  59. Dabblet: {
  60. /*
  61. * The following urls should be converted to dabblet:
  62. * _______
  63. * - https://gist.github.com/jerone/3810309
  64. * _______
  65. * - https://gist.github.com/jerone/3810309/revisions
  66. * _______
  67. * - https://gist.github.com/jerone/3810309/forks
  68. * _______
  69. * - https://gist.github.com/jerone/3810309/stars
  70. * ________________________________________________
  71. * - https://gist.github.com/jerone/3810309/f2815cc6796ea985f74b8f5f3c717e8de3b12d37
  72. * ________________________________________________
  73. * - https://gist.github.com/3810309/f2815cc6796ea985f74b8f5f3c717e8de3b12d37
  74. *
  75. */
  76. show: function(url, user, description, files, stars, forks, revisions) {
  77. // already defined in another UserScript: http://userscripts.org/users/31497/scripts
  78. return !document.getElementById("Github_Gist_Dabblet");
  79. },
  80. submit: function(url, user, description, files, stars, forks, revisions) {
  81. var linkLong;
  82. if ((linkLong = document.querySelector(".site-container.js-site-container")) && linkLong.dataset.url) {
  83. var linkLongParts = linkLong.dataset.url.split("/");
  84. linkLongParts.shift();
  85. if (/^(?:revisions|forks|stars)$/gi.test(linkLongParts[linkLongParts.length - 1])) {
  86. linkLongParts.pop();
  87. }
  88. if (new RegExp(user, "gi").test(linkLongParts[0])) {
  89. linkLongParts.shift();
  90. }
  91. url = "/" + linkLongParts.join("/");
  92. } else {
  93. url = url.replace(new RegExp("https?:\/\/gist\.github\.com/" + user, "gi"), "");
  94. }
  95. return "http://dabblet.com/gist" + url;
  96. },
  97. icon: "http://dabblet.com/favicon.ico"
  98. },
  99. UserScript: {
  100. show: function(url, user, description, files, stars, forks, revisions) {
  101. return !!document.querySelector(".bubble[id^='file-'] .raw-url[href$='.user.js']");
  102. },
  103. submit: function(url, user, description, files, stars, forks, revisions) {
  104. return (document.querySelector(".bubble[id^='file-'] .raw-url[href$='.user.js']") || { href: "" }).href.trim();
  105. },
  106. icon: "https://userscripts.org/images/script_icon.png"
  107. }
  108. };
  109.  
  110. function addMenuItem() {
  111. var link, url, menu, li, user, description, files, stars, forks, revisions;
  112.  
  113. if ((link = document.querySelector("[name='link-field']")) && (menu = document.querySelector('ul.menu.gisttabs'))) { // check if we're on an actual gists;
  114. url = link.value;
  115. user = document.querySelector(".author.vcard").textContent.trim();
  116. description = (document.querySelector(".gist-description") || document.querySelector(".js-current-repository") || { textContent: "" }).textContent.trim();
  117. files = document.querySelectorAll(".bubble[id^='file-']").length;
  118. stars = (menu.querySelector("a[href$='/stars'] .counter") || { textContent: "" }).textContent.trim();
  119. forks = (menu.querySelector("a[href$='/forks'] .counter") || { textContent: "" }).textContent.trim();
  120. revisions = (menu.querySelector("a[href$='/revisions'] .counter") || { textContent: "" }).textContent.trim();
  121.  
  122. menu.appendChild(li = document.createElement("li"));
  123. li.id = "Github_Gist_Share";
  124.  
  125. for (var key in socials) {
  126. var social = socials[key],
  127. socialA = document.createElement("a"),
  128. socialImg = document.createElement("img");
  129.  
  130. if (social.show(url, user, description, files, stars, forks, revisions) !== true) continue;
  131.  
  132. li.appendChild(socialA);
  133. socialA.appendChild(socialImg);
  134. socialA.id = String.format("{0}_{1}", li.id, key.replace(/\s+/g, "_"));
  135. socialA.href = social.submit && social.submit(url, user, description, files, stars, forks, revisions);
  136. socialA.title = String.format("[{0}] {1}", key, socialA.href);
  137. socialA.style.display = "inline-block";
  138. socialA.target = "_blank";
  139. socialImg.src = social.icon;
  140. socialImg.alt = key;
  141. }
  142. }
  143. }
  144.  
  145. // init;
  146. addMenuItem();
  147.  
  148. // on pjax;
  149. unsafeWindow.$(document).on("pjax:success", addMenuItem);
  150.  
  151. })();