Greasy Fork is available in English.

Github Gist Share

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

2014-03-19 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

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