init.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // atom-renderer.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 electron = require('electron')
  20. // Call webFrame method.
  21. electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (event, method, args) => {
  22. electron.webFrame[method](...args)
  23. })
  24. electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_SYNC_WEB_FRAME_METHOD', (event, requestId, method, args) => {
  25. const result = electron.webFrame[method](...args)
  26. event.sender.send(`ELECTRON_INTERNAL_BROWSER_SYNC_WEB_FRAME_RESPONSE_${requestId}`, result)
  27. })
  28. electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (event, requestId, method, args) => {
  29. electron.webFrame[method](...args, function (result) {
  30. event.sender.send(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, result)
  31. })
  32. })
  33. // Process command line arguments.
  34. let nodeIntegration = 'false'
  35. let preloadScript = null
  36. let isBackgroundPage = false
  37. for (let arg of process.argv) {
  38. if (arg.indexOf('--guest-instance-id=') === 0) {
  39. // This is a guest web view.
  40. process.guestInstanceId = parseInt(arg.substr(arg.indexOf('=') + 1))
  41. } else if (arg.indexOf('--opener-id=') === 0) {
  42. // This is a guest BrowserWindow.
  43. process.openerId = parseInt(arg.substr(arg.indexOf('=') + 1))
  44. } else if (arg.indexOf('--node-integration=') === 0) {
  45. nodeIntegration = arg.substr(arg.indexOf('=') + 1)
  46. } else if (arg.indexOf('--preload=') === 0) {
  47. preloadScript = arg.substr(arg.indexOf('=') + 1)
  48. } else if (arg === '--background-page') {
  49. isBackgroundPage = true
  50. }
  51. }
  52. if (window.location.protocol === 'chrome-devtools:') {
  53. // Override some inspector APIs.
  54. require('./inspector')
  55. nodeIntegration = 'true'
  56. } else if (window.location.protocol === 'chrome-extension:') {
  57. // Add implementations of chrome API.
  58. require('./chrome-api').injectTo(window.location.hostname, isBackgroundPage, window)
  59. nodeIntegration = 'false'
  60. } else {
  61. // Override default web functions.
  62. require('./override')
  63. // Inject content scripts.
  64. require('./content-scripts-injector')
  65. // Load webview tag implementation.
  66. if (nodeIntegration === 'true' && process.guestInstanceId == null) {
  67. require('./web-view/web-view')
  68. require('./web-view/web-view-attributes')
  69. }
  70. }
  71. if (nodeIntegration === 'true') {
  72. // Export node bindings to global.
  73. global.require = require
  74. global.module = module
  75. // Set the __filename to the path of html file if it is file: protocol.
  76. if (window.location.protocol === 'file:') {
  77. var pathname = process.platform === 'win32' && window.location.pathname[0] === '/' ? window.location.pathname.substr(1) : window.location.pathname
  78. global.__filename = path.normalize(decodeURIComponent(pathname))
  79. global.__dirname = path.dirname(global.__filename)
  80. // Set module's filename so relative require can work as expected.
  81. module.filename = global.__filename
  82. // Also search for module under the html file.
  83. module.paths = module.paths.concat(Module._nodeModulePaths(global.__dirname))
  84. } else {
  85. global.__filename = __filename
  86. global.__dirname = __dirname
  87. }
  88. // Redirect window.onerror to uncaughtException.
  89. window.onerror = function (message, filename, lineno, colno, error) {
  90. if (global.process.listeners('uncaughtException').length > 0) {
  91. global.process.emit('uncaughtException', error)
  92. return true
  93. } else {
  94. return false
  95. }
  96. }
  97. } else {
  98. // Delete Node's symbols after the Environment has been loaded.
  99. process.once('loaded', function () {
  100. delete global.process
  101. delete global.setImmediate
  102. delete global.clearImmediate
  103. delete global.global
  104. })
  105. }
  106. // Load the script specfied by the "preload" attribute.
  107. if (preloadScript) {
  108. try {
  109. require(preloadScript)
  110. } catch (error) {
  111. console.error('Unable to load preload script: ' + preloadScript)
  112. console.error(error.stack || error.message)
  113. }
  114. }