Skip to content
Flávio Monteiro edited this page Dec 20, 2016 · 26 revisions

NOTE: some content in this wiki applies only to 0.12 and earlier versions. For official documentation on 0.13 and later, see http://docs.nwjs.io

Since v0.3.1

Synopsis

// Load native UI library
var gui = require('nw.gui');

// Print arguments
console.log(gui.App.argv);

// Quit current app
gui.App.quit();

// Get the name field in manifest
gui.App.manifest.name

Reference

argv

Get the command line arguments when starting the app.

fullArgv

Get all the command line arguments when starting the app. Because node-webkit itself used switches like --no-sandbox and --process-per-tab, it would confuse the app when the switches were meant to be given to node-webkit, so App.argv just filtered such switches (arguments' precedence were kept). You can get the switches to be filtered with App.filteredArgv.

dataPath

since v0.6.1

Get the application's data path in user's directory. Windows: %LOCALAPPDATA%/<name>; Linux: ~/.config/<name>; OSX: ~/Library/Application Support/<name> where <name> is the field in the manifest.

manifest

since v0.7.0

Get the JSON object of the manifest file.

clearCache()

Since v0.6.0

Clear the HTTP cache in memory and the one on disk. This method call is synchronized.

closeAllWindows()

since v0.3.2

Send the close event to all windows of current app, if no window is blocking the close event, then the app will quit after all windows have done shutdown. Use this method to quit an app will give windows a chance to save data.

crashBrowser(), crashRenderer()

since v0.8.0

These 2 functions crashes the browser process and the renderer process respectively, to test the Crash dump feature.

getProxyForURL(url)

since v0.6.3
Query the proxy to be used for loading url in DOM. The return value is in the same format used in PAC (e.g. "DIRECT", "PROXY localhost:8080").

setProxyConfig(config)

since v0.11.1
Set the proxy config which the web engine will be used to request network resources.
Rule:

    // Parses the rules from a string, indicating which proxies to use.                                                                                                                        
    //                                                                                                                                                                                         
    //   proxy-uri = [<proxy-scheme>"://"]<proxy-host>[":"<proxy-port>]                                                                                                                        
    //                                                                                                                                                                                         
    //   proxy-uri-list = <proxy-uri>[","<proxy-uri-list>]                                                                                                                                     
    //                                                                                                                                                                                         
    //   url-scheme = "http" | "https" | "ftp" | "socks"                                                                                                                                       
    //                                                                                                                                                                                         
    //   scheme-proxies = [<url-scheme>"="]<proxy-uri-list>                                                                                                                                    
    //                                                                                                                                                                                         
    //   proxy-rules = scheme-proxies[";"<scheme-proxies>]                                                                                                                                     
    //                                                                                                                                                                                         
    // Thus, the proxy-rules string should be a semicolon-separated list of                                                                                                                    
    // ordered proxies that apply to a particular URL scheme. Unless specified,                                                                                                                
    // the proxy scheme for proxy-uris is assumed to be http.                                                                                                                                  
    //                                                                                                                                                                                         
    // Some special cases:                                                                                                                                                                     
    //  * If the scheme is omitted from the first proxy list, that list applies                                                                                                                
    //    to all URL schemes and subsequent lists are ignored.                                                                                                                                 
    //  * If a scheme is omitted from any proxy list after a list where a scheme                                                                                                               
    //    has been provided, the list without a scheme is ignored.                                                                                                                             
    //  * If the url-scheme is set to 'socks', that sets a fallback list that                                                                                                                  
    //    to all otherwise unspecified url-schemes, however the default proxy-                                                                                                                 
    //    scheme for proxy urls in the 'socks' list is understood to be                                                                                                                        
    //    socks4:// if unspecified.                                                                                                                                                            
    //                                                                                                                                                                                         
    // For example:                                                                                                                                                                            
    //   "http=foopy:80;ftp=foopy2"  -- use HTTP proxy "foopy:80" for http://                                                                                                                  
    //                                  URLs, and HTTP proxy "foopy2:80" for                                                                                                                   
    //                                  ftp:// URLs.                                                                                                                                           
    //   "foopy:80"                  -- use HTTP proxy "foopy:80" for all URLs.                                                                                                                
    //   "foopy:80,bar,direct://"    -- use HTTP proxy "foopy:80" for all URLs,                                                                                                                
    //                                  failing over to "bar" if "foopy:80" is                                                                                                                 
    //                                  unavailable, and after that using no                                                                                                                   
    //                                  proxy.                                                                                                                                                 
    //   "socks4://foopy"            -- use SOCKS v4 proxy "foopy:1080" for all                                                                                                                
    //                                  URLs.                                                                                                                                                  
    //   "http=foop,socks5://bar.com -- use HTTP proxy "foopy" for http URLs,                                                                                                                  
    //                                  and fail over to the SOCKS5 proxy                                                                                                                      
    //                                  "bar.com" if "foop" is unavailable.                                                                                                                    
    //   "http=foopy,direct://       -- use HTTP proxy "foopy" for http URLs,                                                                                                                  
    //                                  and use no proxy if "foopy" is                                                                                                                         
    //                                  unavailable.                                                                                                                                           
    //   "http=foopy;socks=foopy2   --  use HTTP proxy "foopy" for http URLs,                                                                                                                  
    //                                  and use socks4://foopy2 for all other                                                                                                                  
    //                                  URLs.                                          

quit()

Quit current app. This method will not send close event to windows and app will just quit quietly.

setCrashDumpDir(dir)

since v0.8.0

Set the directory where the minidump file will be saved on crash. For more information, see Crash dump

addOriginAccessWhitelistEntry(sourceOrigin, destinationProtocol, destinationHost, allowDestinationSubdomains)

since v0.10.0-rc1
Add an entry to the whitelist used for controlling cross-origin access. Suppose you want to allow HTTP redirecting from github.com to the page of your app, use something like this with the App-protocol:

App.addOriginAccessWhitelistEntry('http://github.com/', 'app', 'myapp', true);

Use App.removeOriginAccessWhitelistEntry with exactly the same arguments to do the contrary.

removeOriginAccessWhitelistEntry(sourceOrigin, destinationProtocol, destinationHost, allowDestinationSubdomains)

since v0.10.0-rc1
Remove an entry from the whitelist used for controlling cross-origin access. See addOriginAccessWhitelistEntry above.

registerGlobalHotKey(shortcut);

since v0.10.0

Register a global keyboard shortcut (also known as system-wide hot key) to the system.

For more information, please see Shortcut.

unregisterGlobalHotKey(shortcut);

since v0.10.0

Unregisters a global keyboard shortcut.

For more information, please see Shortcut.

Events

Following events can be listened by using App.on() function, for more information on how to receive events, you can visit EventEmitter.

open

Since v0.3.2

Emitted when users opened a file with your app. There is a single parameter of this event callback: Since v0.7.0, it is the full command line of the program; before that it's the argument in the command line and the event is sent multiple times for each of the arguments. For more on this, see Handling files and arguments.

reopen

since v0.7.3

This is a Mac specific feature. This event is sent when the user clicks the dock icon for an already running application.

Clone this wiki locally