Greasy Fork is available in English.

Print Code

Print.

  1. // ==UserScript==
  2. // @name Print Code
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2.0
  5. // @description Print.
  6. // @author Feng Ya
  7. // @match https://github.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. ;(function () {
  12. 'use strict'
  13. // Your code here...
  14. if (!document.querySelector('div.file')) return
  15.  
  16. console.log('print code')
  17.  
  18. const btnPrint = document.createElement('a')
  19. btnPrint.classList.add('btn')
  20. btnPrint.classList.add('btn-sm')
  21. btnPrint.classList.add('BtnGroup-item')
  22. console.log(btnPrint.classList)
  23. btnPrint.innerHTML = 'Print'
  24. btnPrint.addEventListener('click', printCode)
  25.  
  26. document
  27. .querySelector('div.file-actions > div.BtnGroup')
  28. .appendChild(btnPrint)
  29.  
  30. function printCode () {
  31. const file = document.querySelector('div.file')
  32. const header = document.querySelector('div.file-header')
  33.  
  34. header.style.display = 'none'
  35.  
  36. const css = document.createElement('style')
  37. css.type = 'text/css'
  38. css.innerHTML = `
  39. .file { width: 210mm; }
  40. .blob-code { line-height: 1.2; }
  41. .blob-code-inner { font-size: 12pt; }
  42. `
  43. document.head.appendChild(css)
  44.  
  45. document.querySelectorAll('body > div').forEach(div => {
  46. div.style.display = 'none'
  47. })
  48.  
  49. document.body.appendChild(file)
  50. }
  51. })()