Github Gist Share

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

As of 2014-03-06. See the latest version.

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