Greasy Fork is available in English.

GitHub: Add Flattr button

Add Flattr button on GitHub.com

  1. /*
  2. Add Flattr button on GitHub.com
  3. Copyright (C) 2012 LouCypher
  4.  
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>
  17. */
  18.  
  19. // ==UserScript==
  20. // @name GitHub: Add Flattr button
  21. // @namespace https://github.com/LouCypher
  22. // @description Add Flattr button on GitHub.com
  23. // @version 13.0
  24. // @author LouCypher
  25. // @license GPL
  26. // @icon http://i.imgur.com/VDx96.png
  27. // @resource license https://raw.github.com/LouCypher/userscripts/master/licenses/GPL/LICENSE.txt
  28. // @resource changelog https://github.com/LouCypher/userscripts/raw/master/github-flattr/changelog.txt
  29. // @include https://github.com/*
  30. // @include https://gist.github.com/*
  31. // @exclude https://github.com/dashboard/*
  32. // @exclude https://github.com/*/following
  33. // ==/UserScript==
  34.  
  35. (function() {
  36. var isGist = location.hostname == "gist.github.com";
  37. var buttons = $(".pagehead ul.pagehead-actions") || // repo/gist page
  38. $(".user-following-container"); // user profile page
  39. if (!buttons ||
  40.  
  41. // Thou shalt not Flattreth thyself.
  42. ($("li.text", buttons) && ($("li.text", buttons).textContent == "This is you!")) ||
  43.  
  44. // Thou canst not Flattreth organizations.
  45. // http://blog.flattr.net/2012/02/winter-update-github-tweets-extensions/#comment-8471
  46. $(".pagehead > .avatared > .organization-bit") ||
  47.  
  48. $("li a.minibutton.btn-back", buttons)
  49.  
  50. ) return;
  51.  
  52. var url = location.href;
  53.  
  54. if (isGist) {
  55. var owner = $(".author.vcard a");
  56. var user = $("#user-links .name");
  57. if (user && (user.href == owner.href)) return; // Thou shalt not Flattreth thine own gist.
  58.  
  59. var li = insertBefore(document.createElement("li"), $("li", buttons), buttons);
  60.  
  61. var button = li.appendChild(document.createElement("button"));
  62. button.className = "minibutton";
  63. button.appendChild(flattrButton($("a.js-current-repository").href));
  64. button.appendChild(button.firstChild.lastChild);
  65. button.addEventListener("click", function(e) {
  66. e.target.querySelector("a").click();
  67. }, false);
  68.  
  69. return;
  70. }
  71.  
  72. var username = $(".avatared > h1 > em") || $(".avatared > h1 > .name-only");
  73. var repoName = url.match(/[^(\.com\/)]\w+\/.[^\/]*/);
  74. if (!(repoName || username)) return;
  75.  
  76. var permalink = $(".js-current-repository");
  77.  
  78. var name = repoName ? repoName.toString() : username.textContent;
  79.  
  80. if (buttons.nodeName == "SPAN") {
  81. url = location.href.replace(location.search, "");
  82. var span = insertBefore(document.createElement("span"),
  83. buttons.firstChild, buttons);
  84. span.appendChild(flattrButton(url, "Flattr " + name, "minibutton"));
  85.  
  86. } else if (!$(".repohead ul.tabs li:last-child a[href$='settings']")) {
  87. // Thou shalt not Flattreth thine own repo.
  88. var li = insertBefore(document.createElement("li"),
  89. $("li.text", buttons) ? $("li.text", buttons).
  90. nextSibling
  91. : buttons.firstChild,
  92. buttons);
  93. li.appendChild(flattrButton(permalink ? permalink.href : url,
  94. "Flattr " + name, "minibutton"));
  95. }
  96.  
  97. var container = $("#js-repo-pjax-container");
  98. var committer = $(".authorship .author-name a", container);
  99. var user = $("#user .name"); // Logged in.
  100.  
  101. // Thou shalt not Flattreth thine own commit.
  102. if (user && committer && (user.href == committer.href)) return;
  103.  
  104. var browse = $(".commit > .browse-button", container);
  105. browse && (browse.style.marginLeft = ".5em")
  106. && insertBefore(flattrButton(url, "Flattr this commit!", "browse-button"),
  107. browse.nextSibling,
  108. browse.parentNode);
  109.  
  110. function $(aSelector, aNode) {
  111. return (aNode ? aNode : document).querySelector(aSelector);
  112. }
  113.  
  114. function insertBefore(aNode, aSibling, aParent) {
  115. return aParent.insertBefore(aNode, aSibling);
  116. }
  117.  
  118. function getCSSPrefix() {
  119. var ua = navigator.userAgent;
  120. if (/Firefox/.test(ua)) return "-moz-";
  121. if (/Opera/.test(ua)) return "-o-";
  122. if (/AppleWebKit/.test(ua)) return "-webkit-";
  123. }
  124.  
  125. // Flattr button
  126. function flattrButton(aURL, aTitle, aClassName) {
  127. var link = document.createElement("a");
  128. link.href = "https://flattr.com/submit/auto?url="
  129. + encodeURIComponent(aURL);
  130. link.title = aTitle ? aTitle : "";
  131. link.className = aClassName ? aClassName : "";
  132. link.target = "_blank";
  133. link.setAttribute("data-flattr-uid", "flattr");
  134. link.setAttribute("data-flattr-category", "software");
  135. link.setAttribute("data-flattr-tags", "software, github, opensource");
  136. link.innerHTML = '<img src="'
  137. + 'https://api.flattr.com/button/flattr-badge-small.png"'
  138. + ' alt="Flattr!" style="width: 12px; height: 12px;'
  139. + ' vertical-align: -1px; margin-right: .5em; "/>Flattr!';
  140. return link;
  141. }
  142. })()