Greasy Fork - Scripts Links

Add more scripts' links on scripts listing.

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

  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 2.0
  7. // @author LouCypher
  8. // @license MIT License
  9. // @contributionURL http://loucypher.github.io/userscripts/donate.html?Greasy+Fork+-+Scripts+Links
  10. // @resource LICENSE https://raw.github.com/LouCypher/userscripts/master/greasyfork/scripts-links/LICENSE.txt
  11. // @resource CHANGELOG https://raw.github.com/LouCypher/userscripts/master/greasyfork/scripts-links/CHANGELOG.txt
  12. // @include https://greasyfork.org/scripts*
  13. // @include https://greasyfork.org/users/*
  14. // @run-at document-end
  15. // @grant GM_addStyle
  16. // @grant GM_log
  17. // ==/UserScript==
  18.  
  19.  
  20. function $(aSelector, aNode) {
  21. return (aNode || document).querySelector(aSelector);
  22. }
  23.  
  24. function $$(aSelector, aNode) {
  25. return (aNode || document).querySelectorAll(aSelector);
  26. }
  27.  
  28. function createElement(aNodeName) {
  29. return document.createElement(aNodeName);
  30. }
  31.  
  32. function addLink(aURL, aText, aTitle, aPingURL) {
  33. var link = createElement("a");
  34. link.className = "script-link";
  35. link.href = aURL;
  36. link.textContent = aText;
  37. if (aTitle)
  38. link.title = aTitle;
  39. if (aPingURL)
  40. link.dataset.pingUrl = aPingURL;
  41. return link;
  42. }
  43.  
  44. function separate(aNode, aSpaces) {
  45. return aNode.appendChild(document.createTextNode(aSpaces ? " | " : "|"));
  46. }
  47.  
  48. var user = $(".user-profile-link a"); // Check if you're logged in
  49.  
  50. /* Get CSRF authenticity token */
  51. var csrfToken = $("head").children["csrf-token"];
  52. var authToken = csrfToken ? "?authenticity_token=" +
  53. encodeURIComponent(csrfToken.content)
  54. : null;
  55.  
  56. var scriptBoxes = $$(".script-list article");
  57. //GM_log(scriptBoxes.length);
  58. if (scriptBoxes.length) {
  59. GM_addStyle(".script-list li{height:200px!important}");
  60.  
  61. var box, node, href;
  62. for (var i = 0; i < scriptBoxes.length; i++) {
  63. box = scriptBoxes[i];
  64. node = box.insertBefore(createElement("p"), $("footer", box));
  65. href = $("h2 a", box).getAttribute("href"); // Script page URL
  66.  
  67. node.appendChild(addLink(href + "/code.user.js", "install", "",
  68. href + "/install-ping"
  69. + (authToken ? authToken : "")));
  70.  
  71. separate(node, true);
  72. node.appendChild(addLink(href + "/code", "code"));
  73.  
  74. separate(node, true);
  75. node.appendChild(addLink(href + "/versions", "versions"));
  76.  
  77. separate(node, true);
  78. node.appendChild(addLink(href + "/feedback", "feedback"));
  79.  
  80. // Add 'update' link if you're logged in and it's your own script
  81. if (user && $(".script-list-author a", box).href === user.href) {
  82. separate(node, true);
  83. node.appendChild(addLink(href + "/versions/new", "update"));
  84. }
  85. }
  86. }
  87.  
  88. /* Sonny's Greasy Fork user script */
  89. document.addEventListener("DOMContentLoaded", function() {
  90. var scriptTable = $("#script-table");
  91. if (scriptTable) {
  92. GM_addStyle("#script-table tr > td:nth-child(2) span{float:right}" +
  93. "#script-table tr > td:nth-child(2) a.script-link" +
  94. "{display:inline;padding:0 .5em}");
  95.  
  96. var cells = $$("td:nth-child(2)", scriptTable);
  97. //GM_log(cells.length);
  98. if (cells.length) {
  99. var cell, node, link, href, authorColumn, mine;
  100. for (var i = 0; i < cells.length; i++) {
  101. cell = cells[i];
  102. link = $("a", cell); // Script page link
  103. href = link.getAttribute("href"); // Script page URL
  104.  
  105. node = cell.appendChild(createElement("span"));
  106.  
  107. node.appendChild(addLink(href + "/code.user.js", "I",
  108. "Install " + link.textContent,
  109. href + "/install-ping"
  110. + (authToken ? authToken : "")));
  111.  
  112. separate(node);
  113. node.appendChild(addLink(href + "/code", "C", "Code"));
  114.  
  115. separate(node);
  116. node.appendChild(addLink(href + "/versions", "V", "Version"));
  117.  
  118. separate(node);
  119. node.appendChild(addLink(href + "/feedback", "F", "Feedback"));
  120.  
  121. // Check if you're logged in and it's own your script
  122. authorColumn = $("td:nth-child(4) a", cell.parentNode);
  123. if (authorColumn)
  124. mine = authorColumn.href === user.href;
  125. else
  126. mine = location.href.indexOf(user.href) === 0;
  127. // Add 'update' link if you're logged in and it's your own script
  128. if (user && mine) {
  129. separate(node);
  130. node.appendChild(addLink(href + "/versions/new", "U",
  131. "Update " + link.textContent));
  132. }
  133. }
  134. }
  135. }
  136. });