renderer.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const { remote, shell } = require('electron')
  2. const fs = require('fs')
  3. const path = require('path')
  4. const URL = require('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('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) => {
  18. e.preventDefault()
  19. shell.openExternal(url)
  20. }
  21. link.addEventListener('click', openLinkExternally)
  22. link.addEventListener('auxclick', openLinkExternally)
  23. }
  24. function replaceText (selector, text) {
  25. const element = document.querySelector(selector)
  26. if (element) {
  27. element.innerText = text
  28. }
  29. }
  30. replaceText('.electron-version', `Electron v${process.versions.electron}`)
  31. replaceText('.chrome-version', `Chromium v${process.versions.chrome}`)
  32. replaceText('.node-version', `Node v${process.versions.node}`)
  33. replaceText('.v8-version', `v8 v${process.versions.v8}`)
  34. replaceText('.command-example', `${electronPath} path-to-app`)
  35. function getOcticonSvg (name) {
  36. const octiconPath = path.resolve(__dirname, 'node_modules', 'octicons', 'build', 'svg', `${name}.svg`)
  37. if (fs.existsSync(octiconPath)) {
  38. const content = fs.readFileSync(octiconPath, 'utf8')
  39. const div = document.createElement('div')
  40. div.innerHTML = content
  41. return div
  42. }
  43. return null
  44. }
  45. function loadSVG (element) {
  46. for (const cssClass of element.classList) {
  47. if (cssClass.startsWith('octicon-')) {
  48. const icon = getOcticonSvg(cssClass.substr(8))
  49. if (icon) {
  50. icon.classList = element.classList
  51. element.parentNode.insertBefore(icon, element)
  52. element.remove()
  53. break
  54. }
  55. }
  56. }
  57. }
  58. for (const element of document.querySelectorAll('.octicon')) {
  59. loadSVG(element)
  60. }
  61. }
  62. window.addEventListener('load', initialize)