diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index a4366649b3..95dc491ed4 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -101,6 +101,13 @@ NativeWindow::NativeWindow(content::WebContents* web_contents, // Read icon before window is created. options.Get(switches::kIcon, &icon_); + // The "preload" option must be absolute path. + if (options.Get(switches::kPreloadScript, &preload_script_) && + !preload_script_.IsAbsolute()) { + LOG(ERROR) << "Path of \"preload\" script must be absolute."; + preload_script_.clear(); + } + // Be compatible with old API of "node-integration" option. std::string old_string_token; if (options.Get(switches::kNodeIntegration, &old_string_token) && @@ -349,6 +356,10 @@ void NativeWindow::AppendExtraCommandLineSwitches( command_line->AppendSwitchASCII(switches::kNodeIntegration, node_integration_ ? "true" : "false"); + // Append --preload. + if (!preload_script_.empty()) + command_line->AppendSwitchPath(switches::kPreloadScript, preload_script_); + // Append --zoom-factor. if (zoom_factor_ != 1.0) command_line->AppendSwitchASCII(switches::kZoomFactor, diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 7a21083ab3..a57a1a9dbe 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -318,6 +318,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, // Web preferences. mate::PersistentDictionary web_preferences_; + // The script to load before page's JavaScript starts to run. + base::FilePath preload_script_; + // Page's default zoom factor. double zoom_factor_; diff --git a/atom/common/options_switches.cc b/atom/common/options_switches.cc index e6e65f204b..4bbc5e6075 100644 --- a/atom/common/options_switches.cc +++ b/atom/common/options_switches.cc @@ -67,7 +67,7 @@ const char kEnablePlugins[] = "enable-plugins"; const char kGuestInstanceID[] = "guest-instance-id"; // Script that will be loaded by guest WebContents before other scripts. -const char kPreloadScript[] = "preload-script"; +const char kPreloadScript[] = "preload"; // Web runtime features. const char kExperimentalFeatures[] = "experimental-features"; diff --git a/atom/renderer/lib/init.coffee b/atom/renderer/lib/init.coffee index 5203ec8530..69e76e98e1 100644 --- a/atom/renderer/lib/init.coffee +++ b/atom/renderer/lib/init.coffee @@ -31,7 +31,7 @@ for arg in process.argv require('web-frame').setName 'ATOM_SHELL_GUEST_WEB_VIEW' else if arg.indexOf('--node-integration=') == 0 nodeIntegration = arg.substr arg.indexOf('=') + 1 - else if arg.indexOf('--preload-script=') == 0 + else if arg.indexOf('--preload=') == 0 preloadScript = arg.substr arg.indexOf('=') + 1 if location.protocol is 'chrome-devtools:' diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 0a77909810..222df14dab 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -61,7 +61,11 @@ You can also create a window without chrome by using * `enable-larger-than-screen` Boolean - Enable the window to be resized larger than screen. * `dark-theme` Boolean - Forces using dark theme for the window, only works on - some GTK+3 desktop environments. + some GTK+3 desktop environments + * `preload` String - Specifies a script that will be loaded before other + scripts run in the window. This script will always have access to node APIs + no matter whether node integration is turned on for the window, and the path + of `preload` script has to be absolute path. * `web-preferences` Object - Settings of web page's features * `javascript` Boolean * `web-security` Boolean diff --git a/spec/api-browser-window-spec.coffee b/spec/api-browser-window-spec.coffee index 79dbaa435e..c81e1731d5 100644 --- a/spec/api-browser-window-spec.coffee +++ b/spec/api-browser-window-spec.coffee @@ -136,6 +136,16 @@ describe 'browser-window module', -> assert.equal after[0], size.width assert.equal after[1], size.height + describe '"preload" options', -> + it 'loads the script before other scripts in window', (done) -> + preload = path.join fixtures, 'module', 'set-global.js' + remote.require('ipc').once 'preload', (event, test) -> + assert.equal(test, 'preload') + done() + w.destroy() + w = new BrowserWindow(show: false, width: 400, height: 400, preload: preload) + w.loadUrl 'file://' + path.join(fixtures, 'api', 'preload.html') + describe 'beforeunload handler', -> it 'returning true would not prevent close', (done) -> w.on 'closed', -> diff --git a/spec/fixtures/api/preload.html b/spec/fixtures/api/preload.html new file mode 100644 index 0000000000..22dee23444 --- /dev/null +++ b/spec/fixtures/api/preload.html @@ -0,0 +1,9 @@ + +
+ + + diff --git a/spec/fixtures/module/set-global.js b/spec/fixtures/module/set-global.js new file mode 100644 index 0000000000..f39919ff9d --- /dev/null +++ b/spec/fixtures/module/set-global.js @@ -0,0 +1 @@ +window.test = 'preload';