New script - github.com

12/9/2019, 5:20:26 PM

Από την 11/12/2019. Δείτε την τελευταία έκδοση.

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

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το 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        New script - github.com
// @namespace   Violentmonkey Scripts
// @match       https://github.com/*
// @grant       none
// @version     1.0
// @author      [email protected]
// @description 12/9/2019, 5:20:26 PM
// ==/UserScript==
if (document.querySelector('#readme article')) {
  const canvas = document.createElement('div')
  canvas.style = `
    position: fixed;
    top: 10vh;
    right: 8vw;
    width: 300px;
    border-radius: 5px;
    background: white;
    border: solid 1px #e0e0e0;
    overflow-y: auto;
    max-height: 60vh;
`
  const markdownDOM = document.querySelector('#readme article')
  function recursiveNodeReader(node, nodeLevel = 1) {
    let n = node.querySelector('h1, h2, h3, h4, h5')
    let result = []
    do {
      if (['h1', 'h2', 'h3', 'h4', 'h5'].includes(n.tagName.toLowerCase())) {
        result.push({
          text: n.textContent,
          level:
            ['h1', 'h2', 'h3', 'h4', 'h5'].indexOf(n.tagName.toLowerCase()) + 1,
          link: n.querySelector('a').href,
        })
      }
    } while ((n = n.nextElementSibling))
    return result
  }

  function renderDOM(domTree) {
    return domTree
      .map(
        node =>
          `<a style="display:block" href="${node.link}" level="${node.level}">${
            node.level > 1 ? '&nbsp;'.repeat(node.level - 2) + '└' : '─'
          }${node.text}</a>`
      )
      .join('')
  }
  const body = document.createElement('div')
  const header = document.createElement('div')
  header.textContent = 'Outline'
  header.style = `
    padding: .5rem .7rem;
    font-size: 2rem;
    position: sticky;
    top: 0px;
    background: white;
  `

  body.style = `
    padding: .2rem .3rem;
  `

  body.addEventListener('mousedown', e => e.stopPropagation(), {
    capture: true,
  })
  body.addEventListener('click', e => e.stopPropagation(), { capture: true })
  let isMove = false
  let position = {
    x: 0,
    y: 0,
  }
  let target = null
  function eleMove(e) {
    if (!isMove) return
    target.style.top = e.clientY - position.y + 'px'
    target.style.left = e.clientX - position.x + 'px'
    target.style.right = ''
  }
  canvas.addEventListener(
    'mousedown',
    function(e) {
      isMove = true
      position.x = e.offsetX
      position.y = e.offsetY
      target = this
      window.addEventListener('mousemove', eleMove, true)
    }
  )
  canvas.addEventListener(
    'mouseup',
    () => {
      isMove = false
      window.removeEventListener('mousemove', eleMove, true)
    }
  )

  body.innerHTML = renderDOM(recursiveNodeReader(markdownDOM))
  canvas.appendChild(header)
  canvas.appendChild(body)

  document.body.appendChild(canvas)
}