Skip to content

Commit d8db825

Browse files
prevent crash on win.showDevTools in normal build flavor
1 parent c45f2f8 commit d8db825

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed

docs/References/Window.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ Turn the window's native shadow on/off. Useful for frameless, transparent window
297297
The behavior of the function is changed since 0.13.0. Please see [Migration Notes from 0.12 to 0.13](../For Users/Migration/From 0.12 to 0.13.md).
298298

299299
* `iframe` `{String} or {HTMLIFrameElement}` _Optional_ the id or the element of the `<iframe>` to be jailed on. By default, the DevTools is shown for entire window.
300-
* `callback(dev_win)` `{Function}` callback with the native window of the DevTools window.
300+
* `callback(dev_win, error)` `{Function}` callback with the native window of the DevTools window.
301301

302302
Open the devtools to inspect the window.
303303

@@ -309,6 +309,14 @@ This function returns a `Window` object via the callback. You can use any proper
309309

310310
See also in [webview reference](webview Tag.md) on how to open DevTools for webview or open DevTools in a webview.
311311

312+
```js
313+
win.showDevTools((dev_win, error) => {
314+
if (error) {
315+
// handle error
316+
}
317+
});
318+
```
319+
312320
## win.closeDevTools()
313321

314322
!!! note
@@ -320,9 +328,9 @@ Close the devtools window.
320328

321329
Enumerate the printers in the system. The callback function will receive an array of JSON objects for the printer information. The device name of the JSON object can be used as parameter in `nw.Window.print()`.
322330

323-
## win.isDevToolsOpen()
331+
## win.isDevToolsOpen(callback)
324332
```javascript
325-
nw.Window.isOpenDevTools((status) => console.log(status))
333+
win.isDevToolsOpen((status) => console.log(status))
326334
```
327335

328336
!!! note

src/resources/api_nw_newwin.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,15 +374,35 @@ NWWindow.prototype.toggleKioskMode = function() {
374374
currentNWWindowInternal.toggleKioskModeInternal(this.cWindow.id);
375375
};
376376

377-
NWWindow.prototype.showDevTools = function(frm, callback) {
378-
var id = '';
379-
if (typeof frm === 'string')
380-
id = frm;
381-
var f = null;
377+
/**
378+
* Open the devtools to inspect the window.
379+
*
380+
* @param {string | HTMLIFrameElement} [iframe] The `id` or element of the `<iframe>` to be jailed on. By default, the DevTools is shown for entire window.
381+
* @param {(dev_win: Window, error: Error) => void} callback Callback with the native window of the DevTools window.
382+
* @returns {void}
383+
*/
384+
NWWindow.prototype.showDevTools = function(iframe, callback) {
385+
let error;
386+
if (process.versions['nw-flavor'] !== 'sdk') {
387+
error = new Error('This API is only available on SDK build flavor. Current build flavor is ' + process.versions['nw-flavor']);
388+
callback(undefined, error);
389+
return;
390+
}
391+
if (process.versions['nw-flavor'] === 'sdk') {
392+
if (nw.App.argv.includes('--disable-devtools')) {
393+
error = new Error('DevTools is disabled by --disable-devtools command line flag.');
394+
callback(undefined, error);
395+
return;
396+
}
397+
}
398+
let id = '';
399+
if (typeof iframe === 'string')
400+
id = iframe;
401+
let f = null;
382402
if (id)
383403
f = this.window.getElementById(id);
384404
else
385-
f = frm || null;
405+
f = iframe || null;
386406
nwNatives.setDevToolsJail(f);
387407
currentNWWindowInternal.showDevTools2Internal(this.cWindow.id, callback);
388408
};
@@ -760,11 +780,13 @@ apiBridge.registerCustomHook(function(bindingsAPI) {
760780
callback(cwindows.map(create_nw_win));
761781
});
762782
});
783+
763784
apiFunctions.setHandleRequest('isDevToolsOpen', function(callback) {
764785
return chrome.windows.getAll({ populate: true, windowTypes: ['devtools'] }, function(wins) {
765786
callback(wins.length > 0);
766787
})
767788
});
789+
768790
apiFunctions.setHandleRequest('open', function(url, params, callback) {
769791
var options = {'url': url, 'setSelfAsOpener': true, 'type': 'popup'};
770792
//FIXME: unify this conversion code with nwjs/default.js

src/resources/api_nw_window.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,18 +378,39 @@ apiBridge.registerCustomHook(function(bindingsAPI) {
378378
currentNWWindowInternal.toggleKioskModeInternal();
379379
};
380380

381-
NWWindow.prototype.showDevTools = function(frm, callback) {
382-
var id = '';
383-
if (typeof frm === 'string')
384-
id = frm;
385-
var f = null;
381+
/**
382+
* Open the devtools to inspect the window.
383+
*
384+
* @param {string | HTMLIFrameElement} [iframe] The `id` or element of the `<iframe>` to be jailed on. By default, the DevTools is shown for entire window.
385+
* @param {(dev_win: Window, error: Error) => void} callback Callback with the native window of the DevTools window.
386+
* @returns {void}
387+
*/
388+
NWWindow.prototype.showDevTools = function(iframe, callback) {
389+
let error;
390+
if (process.versions['nw-flavor'] !== 'sdk') {
391+
error = new Error('This API is only available on SDK build flavor. Current build flavor is ' + process.versions['nw-flavor']);
392+
callback(undefined, error);
393+
return;
394+
}
395+
if (process.versions['nw-flavor'] === 'sdk') {
396+
if (nw.App.argv.includes('--disable-devtools')) {
397+
error = new Error('DevTools is disabled by --disable-devtools command line flag.');
398+
callback(undefined, error);
399+
return;
400+
}
401+
}
402+
let id = '';
403+
if (typeof iframe === 'string')
404+
id = iframe;
405+
let f = null;
386406
if (id)
387407
f = this.appWindow.contentWindow.getElementById(id);
388408
else
389-
f = frm || null;
409+
f = iframe || null;
390410
nwNatives.setDevToolsJail(f);
391411
currentNWWindowInternal.showDevToolsInternal(callback);
392412
};
413+
393414
NWWindow.prototype.capturePage = function (callback, options) {
394415
var cb = callback;
395416
if (!options)

0 commit comments

Comments
 (0)