Add Sourcegraph Button to GitHub

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

Από την 13/02/2020. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name    Add Sourcegraph Button to GitHub
// @description Add a 'Sourcrgraph' Button on GitHub repository & file page.
// @version 3
// @grant   none
// @inject-into auto
// @supportURL https://github.com/whtsky/userscripts/issues
// @match   https://github.com/*
// @namespace https://greasyfork.org/users/164794
// ==/UserScript==

function goToSourcegraph(event) {
  event.preventDefault()
  var pats = [
    [
      '^/([^/]+)/([^/]+)/tree/([^/]+)$',
      '/github.com/$1/$2@$3',
      '^/github.com/([^/]+)/([^/@]+)@([^/]+)$',
      '/$1/$2/tree/$3',
    ],
    [
      '^/([^/]+)/([^/]+)/tree/([^/]+)/(.+)$',
      '/github.com/$1/$2@$3/-/tree/$4',
      '^/github.com/([^/]+)/([^/@]+)@([^/]+)/-/tree/(.+)$',
      '/$1/$2/tree/$3/$4',
    ],
    ['^/([^/]+)/([^/]+)/blob/([^/]+)/(.+)$', '/github.com/$1/$2@$3/-/blob/$4', '', ''],
    ['^/([^/]+)/([^/]+)$', '/github.com/$1/$2', '^/github.com/([^/]+)/([^/]+)$', '/$1/$2'],
    ['^/([^/]+)$', '/$1', '^/([^/]+)$', '/$1'],
  ]
  var pathname = window.location.pathname
  for (var i = 0; i < pats.length; i++) {
    var pat = pats[i]
    var r, pathname2
    if (window.location.hostname === 'github.com') {
      if (pat[0] === '') {
        continue
      }
      r = new RegExp(pat[0])
      if (pathname.match(r)) {
        pathname2 = pathname.replace(r, pat[1])
        window.location = 'https://sourcegraph.com' + pathname2
        return
      }
    } else {
      if (pat[2] === '') {
        continue
      }
      r = new RegExp(pat[2])
      if (pathname.match(r)) {
        pathname2 = pathname.replace(r, pat[3])
        window.location = 'https://github.com' + pathname2
        return
      }
    }
  }
  alert('Unable to jump to Sourcegraph (no matching URL pattern).')
}

function createButton() {
  if (document.querySelector('#userscript__sourcegraph')) {
    return
  }
  const targetBtn =
    document.querySelector('#raw-url') || document.querySelector(':not(.commit-links-group) > a.BtnGroup-item')
  if (targetBtn) {
    const newBtn = targetBtn.cloneNode(false)
    newBtn.setAttribute('id', 'userscript__sourcegraph')
    newBtn.setAttribute('class', 'btn btn-sm BtnGroup-item')
    newBtn.textContent = 'Sourcegraph'
    newBtn.href = ''
    newBtn.addEventListener('click', goToSourcegraph)
    targetBtn.parentNode.insertBefore(newBtn, targetBtn)
  }
}

const observer = new MutationObserver(function() {
  observer.disconnect()
  createButton()
  observer.observe(document.body, { childList: true, subtree: true })
})
observer.observe(document.body, { childList: true, subtree: true })

createButton()