init.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. 'use strict'
  2. const events = require('events')
  3. const path = require('path')
  4. const Module = require('module')
  5. // We modified the original process.argv to let node.js load the
  6. // init.js, we need to restore it here.
  7. process.argv.splice(1, 1)
  8. // Clear search paths.
  9. require('../common/reset-search-paths')
  10. // Import common settings.
  11. require('../common/init')
  12. var globalPaths = Module.globalPaths
  13. // Expose public APIs.
  14. globalPaths.push(path.join(__dirname, 'api', 'exports'))
  15. // The global variable will be used by ipc for event dispatching
  16. var v8Util = process.atomBinding('v8_util')
  17. v8Util.setHiddenValue(global, 'ipc', new events.EventEmitter())
  18. // Use electron module after everything is ready.
  19. const {
  20. warnAboutNodeWithRemoteContent,
  21. warnAboutDisabledWebSecurity,
  22. warnAboutInsecureContentAllowed,
  23. warnAboutExperimentalFeatures,
  24. warnAboutEnableBlinkFeatures,
  25. warnAboutInsecureResources,
  26. warnAboutInsecureCSP,
  27. warnAboutAllowedPopups,
  28. shouldLogSecurityWarnings
  29. } = require('./security-warnings')
  30. require('./web-frame-init')()
  31. // Process command line arguments.
  32. let nodeIntegration = 'false'
  33. let webviewTag = 'false'
  34. let preloadScript = null
  35. let preloadScripts = []
  36. let isBackgroundPage = false
  37. let appPath = null
  38. for (let arg of process.argv) {
  39. if (arg.indexOf('--guest-instance-id=') === 0) {
  40. // This is a guest web view.
  41. process.guestInstanceId = parseInt(arg.substr(arg.indexOf('=') + 1))
  42. } else if (arg.indexOf('--opener-id=') === 0) {
  43. // This is a guest BrowserWindow.
  44. process.openerId = parseInt(arg.substr(arg.indexOf('=') + 1))
  45. } else if (arg.indexOf('--node-integration=') === 0) {
  46. nodeIntegration = arg.substr(arg.indexOf('=') + 1)
  47. } else if (arg.indexOf('--preload=') === 0) {
  48. preloadScript = arg.substr(arg.indexOf('=') + 1)
  49. } else if (arg === '--background-page') {
  50. isBackgroundPage = true
  51. } else if (arg.indexOf('--app-path=') === 0) {
  52. appPath = arg.substr(arg.indexOf('=') + 1)
  53. } else if (arg.indexOf('--webview-tag=') === 0) {
  54. webviewTag = arg.substr(arg.indexOf('=') + 1)
  55. } else if (arg.indexOf('--preload-scripts') === 0) {
  56. preloadScripts = arg.substr(arg.indexOf('=') + 1).split(path.delimiter)
  57. }
  58. }
  59. // The webContents preload script is loaded after the session preload scripts.
  60. if (preloadScript) {
  61. preloadScripts.push(preloadScript)
  62. }
  63. if (window.location.protocol === 'chrome-devtools:') {
  64. // Override some inspector APIs.
  65. require('./inspector')
  66. nodeIntegration = 'false'
  67. } else if (window.location.protocol === 'chrome-extension:') {
  68. // Add implementations of chrome API.
  69. require('./chrome-api').injectTo(window.location.hostname, isBackgroundPage, window)
  70. nodeIntegration = 'false'
  71. } else if (window.location.protocol === 'chrome:') {
  72. // Disable node integration for chrome UI scheme.
  73. nodeIntegration = 'false'
  74. } else {
  75. // Override default web functions.
  76. require('./override')
  77. // Inject content scripts.
  78. require('./content-scripts-injector')
  79. // Load webview tag implementation.
  80. if (webviewTag === 'true' && process.guestInstanceId == null) {
  81. require('./web-view/web-view')
  82. require('./web-view/web-view-attributes')
  83. }
  84. }
  85. if (nodeIntegration === 'true') {
  86. // Export node bindings to global.
  87. global.require = require
  88. global.module = module
  89. // Set the __filename to the path of html file if it is file: protocol.
  90. if (window.location.protocol === 'file:') {
  91. const location = window.location
  92. let pathname = location.pathname
  93. if (process.platform === 'win32') {
  94. if (pathname[0] === '/') pathname = pathname.substr(1)
  95. const isWindowsNetworkSharePath = location.hostname.length > 0 && globalPaths[0].startsWith('\\')
  96. if (isWindowsNetworkSharePath) {
  97. pathname = `//${location.host}/${pathname}`
  98. }
  99. }
  100. global.__filename = path.normalize(decodeURIComponent(pathname))
  101. global.__dirname = path.dirname(global.__filename)
  102. // Set module's filename so relative require can work as expected.
  103. module.filename = global.__filename
  104. // Also search for module under the html file.
  105. module.paths = module.paths.concat(Module._nodeModulePaths(global.__dirname))
  106. } else {
  107. global.__filename = __filename
  108. global.__dirname = __dirname
  109. if (appPath) {
  110. // Search for module under the app directory
  111. module.paths = module.paths.concat(Module._nodeModulePaths(appPath))
  112. }
  113. }
  114. // Redirect window.onerror to uncaughtException.
  115. window.onerror = function (message, filename, lineno, colno, error) {
  116. if (global.process.listeners('uncaughtException').length > 0) {
  117. global.process.emit('uncaughtException', error)
  118. return true
  119. } else {
  120. return false
  121. }
  122. }
  123. } else {
  124. // Delete Node's symbols after the Environment has been loaded.
  125. process.once('loaded', function () {
  126. delete global.process
  127. delete global.Buffer
  128. delete global.setImmediate
  129. delete global.clearImmediate
  130. delete global.global
  131. })
  132. }
  133. // Load the preload scripts.
  134. for (const preloadScript of preloadScripts) {
  135. try {
  136. require(preloadScript)
  137. } catch (error) {
  138. console.error('Unable to load preload script: ' + preloadScript)
  139. console.error(error.stack || error.message)
  140. }
  141. }
  142. // Warn about security issues
  143. window.addEventListener('load', function loadHandler () {
  144. if (shouldLogSecurityWarnings()) {
  145. if (nodeIntegration === 'true') {
  146. warnAboutNodeWithRemoteContent()
  147. }
  148. warnAboutDisabledWebSecurity()
  149. warnAboutInsecureResources()
  150. warnAboutInsecureContentAllowed()
  151. warnAboutExperimentalFeatures()
  152. warnAboutEnableBlinkFeatures()
  153. warnAboutInsecureCSP()
  154. warnAboutAllowedPopups()
  155. }
  156. window.removeEventListener('load', loadHandler)
  157. })