Github Gist Share

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

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