Add Sourcegraph Button to GitHub

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

Από την 18/06/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 5
// @grant   none
// @inject-into auto
// @supportURL https://github.com/whtsky/userscripts/issues
// @match   https://github.com/*
// @namespace https://greasyfork.org/users/164794
// ==/UserScript==

const pats = [
  ['^/([^/]+)/([^/]+)/tree/([^/]+)$', '/github.com/$1/$2@$3'],
  ['^/([^/]+)/([^/]+)/tree/([^/]+)/(.+)$', '/github.com/$1/$2@$3/-/tree/$4'],
  ['^/([^/]+)/([^/]+)/blob/([^/]+)/(.+)$', '/github.com/$1/$2@$3/-/blob/$4'],
  ['^/([^/]+)/([^/]+)/?$', '/github.com/$1/$2'],
  ['^/([^/]+)/?$', '/$1'],
].map(([reg, replaceValue]) => ({
  regexp: new RegExp(reg),
  replaceValue,
}))

const buttonID = 'userscript__sourcegraph'

function getSourceGraphUrl() {
  var pathname = window.location.pathname
  for (const { regexp, replaceValue } of pats) {
    if (pathname.match(regexp)) {
      const pathname2 = pathname.replace(regexp, replaceValue)
      return 'https://sourcegraph.com' + pathname2
    }
  }
}

/**
 * @returns {HTMLAnchorElement | null}
 */
function getCreatedButton() {
  return document.querySelector(`#${buttonID}`)
}

function createButton() {
  if (getCreatedButton()) {
    return
  }

  const targetBtn =
    document.querySelector('#raw-url') ||
    document.querySelector('.file-navigation a.BtnGroup-item') ||
    document.querySelector('.file-navigation a.d-md-block')
  if (targetBtn) {
    /**
     * @type {HTMLAnchorElement}
     */
    const newBtn = targetBtn.cloneNode(false)
    newBtn.setAttribute('id', buttonID)
    newBtn.textContent = 'Sourcegraph'
    newBtn.href = getSourceGraphUrl()
    targetBtn.parentNode.insertBefore(newBtn, targetBtn)
  }
}

window.addEventListener('popstate', function() {
  const button = getCreatedButton()
  if (button) {
    button.href = getSourceGraphUrl()
  }
})

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()