Add Sourcegraph Button to GitHub

Add a 'Sourcrgraph' Button on GitHub repository & file page.

Versione datata 04/03/2020. Vedi la nuova versione l'ultima versione.

  1. // ==UserScript==
  2. // @name Add Sourcegraph Button to GitHub
  3. // @description Add a 'Sourcrgraph' Button on GitHub repository & file page.
  4. // @version 4
  5. // @grant none
  6. // @inject-into auto
  7. // @supportURL https://github.com/whtsky/userscripts/issues
  8. // @match https://github.com/*
  9. // @namespace https://greatest.deepsurf.us/users/164794
  10. // ==/UserScript==
  11.  
  12. function getSourceGraphUrl() {
  13. var pats = [
  14. [
  15. '^/([^/]+)/([^/]+)/tree/([^/]+)$',
  16. '/github.com/$1/$2@$3',
  17. '^/github.com/([^/]+)/([^/@]+)@([^/]+)$',
  18. '/$1/$2/tree/$3',
  19. ],
  20. [
  21. '^/([^/]+)/([^/]+)/tree/([^/]+)/(.+)$',
  22. '/github.com/$1/$2@$3/-/tree/$4',
  23. '^/github.com/([^/]+)/([^/@]+)@([^/]+)/-/tree/(.+)$',
  24. '/$1/$2/tree/$3/$4',
  25. ],
  26. ['^/([^/]+)/([^/]+)/blob/([^/]+)/(.+)$', '/github.com/$1/$2@$3/-/blob/$4', '', ''],
  27. ['^/([^/]+)/([^/]+)$', '/github.com/$1/$2', '^/github.com/([^/]+)/([^/]+)$', '/$1/$2'],
  28. ['^/([^/]+)$', '/$1', '^/([^/]+)$', '/$1'],
  29. ]
  30. var pathname = window.location.pathname
  31. for (var i = 0; i < pats.length; i++) {
  32. var pat = pats[i]
  33. var r, pathname2
  34. if (window.location.hostname === 'github.com') {
  35. if (pat[0] === '') {
  36. continue
  37. }
  38. r = new RegExp(pat[0])
  39. if (pathname.match(r)) {
  40. pathname2 = pathname.replace(r, pat[1])
  41. return 'https://sourcegraph.com' + pathname2
  42. }
  43. } else {
  44. if (pat[2] === '') {
  45. continue
  46. }
  47. r = new RegExp(pat[2])
  48. if (pathname.match(r)) {
  49. pathname2 = pathname.replace(r, pat[3])
  50. return 'https://github.com' + pathname2
  51. }
  52. }
  53. }
  54. }
  55.  
  56. function goToSourcegraph(event) {
  57. event.preventDefault()
  58. const sourceGraphUrl = getSourceGraphUrl()
  59. if (sourceGraphUrl) {
  60. window.location = sourceGraphUrl
  61. } else {
  62. alert('Unable to jump to Sourcegraph (no matching URL pattern).')
  63. }
  64. }
  65.  
  66. function createButton() {
  67. if (document.querySelector('#userscript__sourcegraph')) {
  68. return
  69. }
  70. const targetBtn = document.querySelector('#raw-url') || document.querySelector('.file-navigation a.BtnGroup-item')
  71. if (targetBtn) {
  72. const newBtn = targetBtn.cloneNode(false)
  73. newBtn.setAttribute('id', 'userscript__sourcegraph')
  74. newBtn.setAttribute('class', 'btn btn-sm BtnGroup-item')
  75. newBtn.textContent = 'Sourcegraph'
  76. newBtn.href = ''
  77. newBtn.addEventListener('click', goToSourcegraph)
  78. targetBtn.parentNode.insertBefore(newBtn, targetBtn)
  79. targetBtn.parentNode.addEventListener('mouseenter', () => {
  80. newBtn.href = getSourceGraphUrl()
  81. })
  82. }
  83. }
  84.  
  85. const observer = new MutationObserver(function() {
  86. observer.disconnect()
  87. createButton()
  88. observer.observe(document.body, { childList: true, subtree: true })
  89. })
  90. observer.observe(document.body, { childList: true, subtree: true })
  91.  
  92. createButton()