init.js 5.3 KB

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