Greasy Fork - Scripts Links

Add more scripts' links on scripts listing.

当前为 2014-03-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @id greasy-fork-scripts-links@loucypher
  3. // @name Greasy Fork - Scripts Links
  4. // @namespace https://userscripts.org/users/12
  5. // @description Add more scripts' links on scripts listing.
  6. // @version 3.2
  7. // @author LouCypher
  8. // @license MIT License
  9. // @contributionURL http://loucypher.github.io/userscripts/donate.html?Greasy+Fork+-+Scripts+Links
  10. // @homepageURL https://greasyfork.org/scripts/174
  11. // @supportURL https://greasyfork.org/scripts/174/feedback
  12. // @resource LICENSE https://raw.github.com/LouCypher/userscripts/master/greasyfork/scripts-links/LICENSE.txt
  13. // @resource CHANGELOG https://raw.github.com/LouCypher/userscripts/master/greasyfork/scripts-links/CHANGELOG.txt
  14. // @include https://greasyfork.org/scripts*
  15. // @include https://greasyfork.org/users/*
  16. // @run-at document-end
  17. // @grant GM_addStyle
  18. // @grant GM_log
  19. // ==/UserScript==
  20.  
  21.  
  22. function $(aSelector, aNode) {
  23. return (aNode || document).querySelector(aSelector);
  24. }
  25.  
  26. function $$(aSelector, aNode) {
  27. return (aNode || document).querySelectorAll(aSelector);
  28. }
  29.  
  30. function createElement(aNodeName) {
  31. return document.createElement(aNodeName);
  32. }
  33.  
  34. function addLink(aURL, aText, aTitle, aPingURL) {
  35. var link = createElement("a");
  36. link.className = "script-link";
  37. link.href = aURL;
  38. link.textContent = aText;
  39. if (aTitle)
  40. link.title = aTitle;
  41. if (aPingURL)
  42. link.dataset.pingUrl = aPingURL;
  43. return link;
  44. }
  45.  
  46. function separate(aNode, aSpaces) {
  47. return aNode.appendChild(document.createTextNode(aSpaces ? " | " : "|"));
  48. }
  49.  
  50. function underReview(aEvent) {
  51. aEvent.preventDefault();
  52. var scriptTitle = aEvent.target.dataset.scriptTitle;
  53. alert(scriptTitle + " is currently under review");
  54. }
  55.  
  56. // Opera UserJS doesn't recognize GM API so we rewrite the functions
  57. if (typeof GM_addStyle !== "function" && GM_log !== "function") {
  58. function GM_addStyle(aCSS) {
  59. var style = document.createElement("style");
  60. style.type = "text/css";
  61. style.textContent = aCSS;
  62. $("head").appendChild(style);
  63. }
  64.  
  65. function GM_log(aText) {
  66. console.log(aText);
  67. }
  68. }
  69.  
  70. var blocked = /\/(94|115|116|117|119|120|121|122|123|172)$/;
  71. var user = $(".user-profile-link a"); // Check if you're logged in
  72.  
  73. /* Get CSRF authenticity token */
  74. var csrfToken = $("head").children["csrf-token"];
  75. var authToken = csrfToken ? "?authenticity_token=" +
  76. encodeURIComponent(csrfToken.content)
  77. : null;
  78.  
  79. var scriptBoxes = $$(".script-list article");
  80. //GM_log(scriptBoxes.length);
  81. if (scriptBoxes.length) {
  82. var box, node, name, href, install;
  83. for (var i = 0; i < scriptBoxes.length; i++) {
  84. box = scriptBoxes[i];
  85. name = $("h2 a", box).textContent; // Script title
  86. href = $("h2 a", box).getAttribute("href"); // Script page URL
  87. node = box.insertBefore(createElement("p"), $("footer", box));
  88.  
  89. install = node.appendChild(addLink(href + "/code.user.js", "install", "",
  90. href + "/install-ping"
  91. + (authToken ? authToken : "")));
  92. if (blocked.test(href)) {
  93. install.href = "/scripts/under-assessment";
  94. install.removeAttribute("data-ping-url");
  95. install.dataset.scriptTitle = name;
  96. install.addEventListener("click", underReview);
  97. }
  98.  
  99. separate(node, true);
  100. node.appendChild(addLink(href + "/code", "code"));
  101.  
  102. separate(node, true);
  103. node.appendChild(addLink(href + "/versions", "versions"));
  104.  
  105. separate(node, true);
  106. node.appendChild(addLink(href + "/feedback", "feedback"));
  107.  
  108. // Add 'update' link if you're logged in and it's your own script
  109. if (user && $(".script-list-author a", box).href === user.href) {
  110. separate(node, true);
  111. node.appendChild(addLink(href + "/versions/new", "update"));
  112. }
  113. }
  114. }
  115.  
  116. /* Sonny's Greasy Fork user script */
  117. document.addEventListener("DOMContentLoaded", function() {
  118. var scriptTable = $("#script-table");
  119. if (scriptTable) {
  120. GM_addStyle("#script-table tr > td:nth-child(2) span{float:right}" +
  121. "#script-table tr > td:nth-child(2) a.script-link" +
  122. "{display:inline;padding:0 .5em}");
  123.  
  124. var cells = $$("td:nth-child(2)", scriptTable);
  125. //GM_log(cells.length);
  126. if (cells.length) {
  127. var cell, node, link, name, href, install, userId, authorColumn, mine;
  128. for (var i = 0; i < cells.length; i++) {
  129. cell = cells[i];
  130. name = $("a", cell).textContent; // Script title
  131. link = $("a", cell); // Script page link
  132. href = link.getAttribute("href"); // Script page URL
  133.  
  134. node = cell.appendChild(createElement("span"));
  135.  
  136. install = node.appendChild(addLink(href + "/code.user.js", "I",
  137. "Install " + link.textContent,
  138. href + "/install-ping"
  139. + (authToken ? authToken : "")));
  140. if (blocked.test(href)) {
  141. install.href = "/scripts/under-assessment";
  142. install.removeAttribute("data-ping-url");
  143. install.dataset.scriptTitle = name;
  144. install.addEventListener("click", underReview);
  145. }
  146.  
  147. separate(node);
  148. node.appendChild(addLink(href + "/code", "C", "Code"));
  149.  
  150. separate(node);
  151. node.appendChild(addLink(href + "/versions", "V", "Version"));
  152.  
  153. separate(node);
  154. node.appendChild(addLink(href + "/feedback", "F", "Feedback"));
  155.  
  156. if (user) { // If logged in
  157. userId = parseInt(user.href.match(/\d+/));
  158.  
  159. // Check if it's own your script
  160. authorColumn = $("td:nth-child(4) a", cell.parentNode);
  161. if (authorColumn)
  162. mine = authorColumn.href === user.href;
  163. else
  164. mine = parseInt(location.href.match(/\d+/)) === userId;
  165.  
  166. // Add 'update' link if you're logged in and it's your own script
  167. if (mine) {
  168. separate(node);
  169. node.appendChild(addLink(href + "/versions/new", "U",
  170. "Update " + link.textContent));
  171. }
  172. }
  173. }
  174. }
  175. }
  176. });