| 
									
										
										
										
											2016-11-10 12:25:26 -08:00
										 |  |  | ## Class: Debugger
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | > An alternate transport for Chrome's remote debugging protocol.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-23 11:20:56 -08:00
										 |  |  | Process: [Main](../glossary.md#main-process) | 
					
						
							| 
									
										
										
										
											2016-11-10 12:25:26 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Chrome Developer Tools has a [special binding][rdp] available at JavaScript | 
					
						
							|  |  |  | runtime that allows interacting with pages and instrumenting them. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 |  |  | const { BrowserWindow } = require('electron') | 
					
						
							| 
									
										
										
										
											2016-11-10 12:25:26 -08:00
										 |  |  | let win = new BrowserWindow() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | try { | 
					
						
							|  |  |  |   win.webContents.debugger.attach('1.1') | 
					
						
							|  |  |  | } catch (err) { | 
					
						
							|  |  |  |   console.log('Debugger attach failed : ', err) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | win.webContents.debugger.on('detach', (event, reason) => { | 
					
						
							|  |  |  |   console.log('Debugger detached due to : ', reason) | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | win.webContents.debugger.on('message', (event, method, params) => { | 
					
						
							|  |  |  |   if (method === 'Network.requestWillBeSent') { | 
					
						
							|  |  |  |     if (params.request.url === 'https://www.github.com') { | 
					
						
							|  |  |  |       win.webContents.debugger.detach() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | win.webContents.debugger.sendCommand('Network.enable') | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 08:29:01 -07:00
										 |  |  | ### Instance Events
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### Event: 'detach'
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * `event` Event | 
					
						
							|  |  |  | * `reason` String - Reason for detaching debugger. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Emitted when the debugging session is terminated. This happens either when | 
					
						
							|  |  |  | `webContents` is closed or devtools is invoked for the attached `webContents`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### Event: 'message'
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * `event` Event | 
					
						
							|  |  |  | * `method` String - Method name. | 
					
						
							| 
									
										
										
										
											2019-10-13 22:32:11 -07:00
										 |  |  | * `params` any - Event parameters defined by the 'parameters' | 
					
						
							| 
									
										
										
										
											2019-05-06 08:29:01 -07:00
										 |  |  |    attribute in the remote debugging protocol. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Emitted whenever the debugging target issues an instrumentation event. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [rdp]: https://chromedevtools.github.io/devtools-protocol/ | 
					
						
							|  |  |  | [`webContents.findInPage`]: web-contents.md#contentsfindinpagetext-options | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 12:25:26 -08:00
										 |  |  | ### Instance Methods
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `debugger.attach([protocolVersion])`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * `protocolVersion` String (optional) - Requested debugging protocol version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Attaches the debugger to the `webContents`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `debugger.isAttached()`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns `Boolean` - Whether a debugger is attached to the `webContents`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `debugger.detach()`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Detaches the debugger from the `webContents`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-13 18:23:53 +01:00
										 |  |  | #### `debugger.sendCommand(method[, commandParams])`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * `method` String - Method name, should be one of the methods defined by the | 
					
						
							|  |  |  |    [remote debugging protocol][rdp]. | 
					
						
							| 
									
										
										
										
											2019-08-05 10:45:58 -07:00
										 |  |  | * `commandParams` any (optional) - JSON object with request parameters. | 
					
						
							| 
									
										
										
										
											2019-02-13 18:23:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | Returns `Promise<any>` - A promise that resolves with the response defined by | 
					
						
							|  |  |  | the 'returns' attribute of the command description in the remote debugging protocol | 
					
						
							|  |  |  | or is rejected indicating the failure of the command. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Send given command to the debugging target. |