GitHub PHP Hyperlinks

Enhances browsing through PHP code on GitHub by linking referenced classes

As of 2016-07-25. See the latest version.

  1. // ==UserScript==
  2. // @name GitHub PHP Hyperlinks
  3. // @namespace https://github.com/Koopzington
  4. // @version 0.4
  5. // @description Enhances browsing through PHP code on GitHub by linking referenced classes
  6. // @author koopzington@gmail.com
  7. // @match https://github.com/*
  8. // @grant GM_xmlhttpRequest
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. // Also execute this script if content is getting loaded via pjax
  15. document.addEventListener("pjax:complete", function () {
  16. start();
  17. });
  18. start();
  19.  
  20. function start() {
  21. // Check if currently viewed file is a PHP file
  22. if (window.location.href.split('.php').length == 2) {
  23. // Grab reponame
  24. var repoName = window.location.href.split('/');
  25. var status = repoName[6];
  26. repoName = repoName[3] + '/' + repoName[4];
  27. var nsRoots = [];
  28. var dependencies = [];
  29. var imports = [];
  30. var filenamespace;
  31. parseFile();
  32. }
  33.  
  34. function parseFile() {
  35. // Grab namespace of current class
  36. var namespaceXPath = "//span[@class='pl-k' and .='namespace']/following-sibling::span";
  37. filenamespace = document.evaluate(namespaceXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  38. // Check if file actually has a namespace
  39. if (filenamespace !== null) {
  40. // Now let's grab all use statements
  41. var useXpath = "//span[@class='pl-k' and .='use'][not(preceding::span[@class ='pl-k' and .='class'])]/following-sibling::span";
  42. var iterator = document.evaluate(useXpath, document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
  43. var thisNode = iterator.iterateNext();
  44.  
  45. while (thisNode) {
  46. var newImport = {};
  47. newImport.name = thisNode.textContent;
  48. thisNode = iterator.iterateNext();
  49. // Check if use statement has an alias
  50. if (thisNode && thisNode.textContent == "as") {
  51. thisNode = iterator.iterateNext();
  52. newImport.alias = thisNode.textContent;
  53. thisNode = iterator.iterateNext();
  54. } else {
  55. var split = newImport.name.split('\\');
  56. newImport.alias = split[split.length - 1];
  57. }
  58. imports.push(newImport);
  59. }
  60.  
  61. // Grab composer.json from current repo
  62. GM_xmlhttpRequest({
  63. method: "GET",
  64. url: "https://api.github.com/repos/" + repoName + '/contents/composer.json?ref=' + status,
  65. onload: function (responseDetails) {
  66. if (responseDetails.status == 200) {
  67. var data = JSON.parse(atob(JSON.parse(responseDetails.responseText).content));
  68. var req;
  69. checkAutoload(data, repoName);
  70. if (data.hasOwnProperty('require')) {
  71. for (req in data.require) {
  72. dependencies.push(req);
  73. }
  74. }
  75. if (data.hasOwnProperty('require-dev')) {
  76. for (req in data['require-dev']) {
  77. dependencies.push(req);
  78. }
  79. }
  80. addExternalRoots();
  81. }
  82. }
  83. });
  84. }
  85. }
  86.  
  87. function addExternalRoots() {
  88. var promises = [];
  89. for (var i = 0; i < dependencies.length; ++i) {
  90. promises.push(getComposerOf(dependencies[i]));
  91. }
  92. Promise.all(promises).then(function () {
  93. grabFilesOnSameNamespace();
  94. });
  95. }
  96.  
  97. function grabFilesOnSameNamespace() {
  98. // Find out root namespace of file
  99. var currentNamespace = filenamespace.innerHTML;
  100. var currentRoot;
  101. for (var ns in nsRoots) {
  102. if (currentNamespace.substring(0, nsRoots[ns].root.length - 1) + '\\' == nsRoots[ns].root) {
  103. currentNamespace = currentNamespace.substring(nsRoots[ns].root.length);
  104. currentRoot = nsRoots[ns];
  105. }
  106. }
  107. // Now we get all classes that are in the same namespace as our current class
  108. GM_xmlhttpRequest({
  109. method: "GET",
  110. url: "https://api.github.com/repos/" + repoName + '/contents/' + currentRoot.path + currentNamespace,
  111. onload: function (responseDetails) {
  112. if (responseDetails.status == 200) {
  113. var data = JSON.parse(responseDetails.responseText);
  114. for (var i = 0; i < data.length; ++i) {
  115. if (data[i].name.split('.php').length == 2) {
  116. var classname = data[i].name.split('.php')[0];
  117. imports.push({
  118. name: filenamespace.innerHTML + '\\' + classname,
  119. alias: classname
  120. });
  121. }
  122. }
  123. }
  124. editDOM();
  125. }
  126. });
  127. }
  128.  
  129. function getComposerOf(repo) {
  130. return new Promise(function (resolve, reject) {
  131. GM_xmlhttpRequest({
  132. method: "GET",
  133. url: "https://packagist.org/p/" + repo + '.json',
  134. onload: function (responseDetails) {
  135. if (responseDetails.status == 200) {
  136. var reqData = JSON.parse(responseDetails.responseText).packages[repo];
  137. if (reqData.hasOwnProperty('dev-master')) {
  138. checkAutoload(reqData['dev-master']);
  139. }
  140. }
  141. resolve();
  142. }
  143. });
  144. });
  145. }
  146.  
  147. function checkAutoload(data, repoName) {
  148. if (data.hasOwnProperty('autoload')) {
  149. var path;
  150. var repo;
  151. if (repoName !== undefined) {
  152. repo = repoName;
  153. } else {
  154. repo = data.source.url.split('github.com/')[1].split('.git')[0];
  155. }
  156. if (data.autoload.hasOwnProperty('psr-4')) {
  157. for (var ns4 in data.autoload['psr-4']) {
  158. path = data.autoload['psr-4'][ns4];
  159. if (path.substring(path.length - 1) != '/') {
  160. path = path + '/';
  161. }
  162. nsRoots.push({
  163. root: ns4,
  164. path: path,
  165. repo: repo
  166. });
  167. }
  168. }
  169. if (data.autoload.hasOwnProperty('psr-0')) {
  170. for (var ns0 in data.autoload['psr-0']) {
  171. path = data.autoload['psr-0'][ns0];
  172. if (path.substring(path.length - 1) != '/') {
  173. path = path + '/';
  174. }
  175. path = path + ns0.substring(0, ns0.length - 1) + '/';
  176. path = path.replace(/\\/g, '/');
  177. nsRoots.push({
  178. root: ns0,
  179. path: path,
  180. repo: repo
  181. });
  182. }
  183. }
  184. }
  185. }
  186.  
  187. function editDOM() {
  188. var currentRoot;
  189. var currentNamespace;
  190. var k;
  191. var toBeModified = [];
  192. var thisNode;
  193. var iterator;
  194. var currentStatus;
  195.  
  196. for (var j = 0; j < imports.length; ++j) {
  197. currentRoot = undefined;
  198. currentNamespace = undefined;
  199. for (var ns in nsRoots) {
  200. if (imports[j].name.substring(0, nsRoots[ns].root.length) == nsRoots[ns].root) {
  201. currentNamespace = imports[j].name.substring(nsRoots[ns].root.length);
  202. currentRoot = nsRoots[ns];
  203. }
  204. }
  205. if (currentRoot !== undefined) {
  206. if (currentRoot.repo == repoName) {
  207. currentStatus = status;
  208. } else {
  209. currentStatus = 'master';
  210. }
  211. // Find all direct uses of the classes and replace the content with links
  212. classXpath = "//span[.='" + imports[j].alias + "']";
  213. iterator = document.evaluate(classXpath, document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
  214. thisNode = iterator.iterateNext();
  215.  
  216. while (thisNode) {
  217. toBeModified.push(thisNode);
  218. thisNode = iterator.iterateNext();
  219. }
  220. for (k = 0; k < toBeModified.length; ++k) {
  221. toBeModified[k].innerHTML = '<a style="color: inherit;" href="https://github.com/' + currentRoot.repo + '/blob/' + currentStatus + '/' + currentRoot.path + currentNamespace + '.php">' + toBeModified[k].innerHTML + '</a>';
  222. }
  223.  
  224. // Do the same thing again, but this time for subnamespaces (e.g. "Element\")
  225. classXpath = "//span[@class='pl-c1' and .='" + imports[j].alias + "\\']";
  226. iterator = document.evaluate(classXpath, document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
  227. thisNode = iterator.iterateNext();
  228. toBeModified = [];
  229. while (thisNode) {
  230. toBeModified.push(thisNode);
  231. thisNode = iterator.iterateNext();
  232. }
  233. for (k = 0; k < toBeModified.length; ++k) {
  234. toBeModified[k].innerHTML = '<a style="color: inherit;" href="https://github.com/' + currentRoot.repo + '/tree/' + currentStatus + '/' + currentRoot.path + currentNamespace + '">' + toBeModified[k].innerHTML + '</a>';
  235. }
  236.  
  237. // Do the same thing again, but this time for classes with subnamespaces (e.g. Element\Select::class
  238. classXpath = "//span[@class='pl-c1' and .='" + imports[j].alias + "\\']/following-sibling::span[1]";
  239. iterator = document.evaluate(classXpath, document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
  240. thisNode = iterator.iterateNext();
  241. toBeModified = [];
  242. while (thisNode) {
  243. toBeModified.push(thisNode);
  244. thisNode = iterator.iterateNext();
  245. }
  246. for (k = 0; k < toBeModified.length; ++k) {
  247. toBeModified[k].innerHTML = '<a style="color: inherit;" href="https://github.com/' + currentRoot.repo + '/blob/' + currentStatus + '/' + currentRoot.path + currentNamespace + '/' + toBeModified[k].innerHTML + '.php">' + toBeModified[k].innerHTML + '</a>';
  248. }
  249.  
  250. // Add a Hyperlink to the use statement
  251. var classXpath = "//span[@class='pl-c1' and .='" + imports[j].name + "']";
  252. var node = document.evaluate(classXpath, document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
  253. if (node !== null) {
  254. // Use the amount of results of the upper search for subnamespace usages to determine if a link to a directory or to a file should be generated
  255. if (toBeModified.length > 0) {
  256. node.innerHTML = '<a style="color: inherit;" href="https://github.com/' + currentRoot.repo + '/tree/' + currentStatus + '/' + currentRoot.path + currentNamespace + '">' + node.innerHTML + '</a>';
  257. } else {
  258. node.innerHTML = '<a style="color: inherit;" href="https://github.com/' + currentRoot.repo + '/blob/' + currentStatus + '/' + currentRoot.path + currentNamespace + '.php">' + node.innerHTML + '</a>';
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }());