renderer.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { remote, shell } from 'electron'
  2. import * as fs from 'fs'
  3. import * as path from 'path'
  4. import * as URL from 'url'
  5. function initialize () {
  6. // Find the shortest path to the electron binary
  7. const absoluteElectronPath = remote.process.execPath
  8. const relativeElectronPath = path.relative(process.cwd(), absoluteElectronPath)
  9. const electronPath = absoluteElectronPath.length < relativeElectronPath.length
  10. ? absoluteElectronPath
  11. : relativeElectronPath
  12. for (const link of document.querySelectorAll<HTMLLinkElement>('a[href]')) {
  13. // safely add `?utm_source=default_app
  14. const parsedUrl = URL.parse(link.getAttribute('href')!, true)
  15. parsedUrl.query = { ...parsedUrl.query, utm_source: 'default_app' }
  16. const url = URL.format(parsedUrl)
  17. const openLinkExternally = (e: Event) => {
  18. e.preventDefault()
  19. shell.openExternalSync(url)
  20. }
  21. link.addEventListener('click', openLinkExternally)
  22. link.addEventListener('auxclick', openLinkExternally)
  23. }
  24. document.querySelector<HTMLAnchorElement>('.electron-version')!.innerText = `Electron v${process.versions.electron}`
  25. document.querySelector<HTMLAnchorElement>('.chrome-version')!.innerText = `Chromium v${process.versions.chrome}`
  26. document.querySelector<HTMLAnchorElement>('.node-version')!.innerText = `Node v${process.versions.node}`
  27. document.querySelector<HTMLAnchorElement>('.v8-version')!.innerText = `v8 v${process.versions.v8}`
  28. document.querySelector<HTMLAnchorElement>('.command-example')!.innerText = `${electronPath} path-to-app`
  29. function getOcticonSvg (name: string) {
  30. const octiconPath = path.resolve(__dirname, 'octicon', `${name}.svg`)
  31. if (fs.existsSync(octiconPath)) {
  32. const content = fs.readFileSync(octiconPath, 'utf8')
  33. const div = document.createElement('div')
  34. div.innerHTML = content
  35. return div
  36. }
  37. return null
  38. }
  39. function loadSVG (element: HTMLSpanElement) {
  40. for (const cssClass of element.classList) {
  41. if (cssClass.startsWith('octicon-')) {
  42. const icon = getOcticonSvg(cssClass.substr(8))
  43. if (icon) {
  44. for (const elemClass of element.classList) {
  45. icon.classList.add(elemClass)
  46. }
  47. element.before(icon)
  48. element.remove()
  49. break
  50. }
  51. }
  52. }
  53. }
  54. for (const element of document.querySelectorAll<HTMLSpanElement>('.octicon')) {
  55. loadSVG(element)
  56. }
  57. }
  58. window.addEventListener('load', initialize)