Skip to content

prevent crash on win.showDevTools in normal build flavor #8213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions docs/References/Window.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Turn the window's native shadow on/off. Useful for frameless, transparent window
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).

* `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.
* `callback(dev_win)` `{Function}` callback with the native window of the DevTools window.
* `callback(dev_win, error)` `{Function}` callback with the native window of the DevTools window.

Open the devtools to inspect the window.

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

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

```js
win.showDevTools((dev_win, error) => {
if (error) {
// handle error
}
});
```

## win.closeDevTools()

!!! note
Expand All @@ -320,9 +328,9 @@ Close the devtools window.

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()`.

## win.isDevToolsOpen()
## win.isDevToolsOpen(callback)
```javascript
nw.Window.isOpenDevTools((status) => console.log(status))
win.isDevToolsOpen((status) => console.log(status))
```

!!! note
Expand Down
34 changes: 28 additions & 6 deletions src/resources/api_nw_newwin.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,35 @@ NWWindow.prototype.toggleKioskMode = function() {
currentNWWindowInternal.toggleKioskModeInternal(this.cWindow.id);
};

NWWindow.prototype.showDevTools = function(frm, callback) {
var id = '';
if (typeof frm === 'string')
id = frm;
var f = null;
/**
* Open the devtools to inspect the window.
*
* @param {string | HTMLIFrameElement} [iframe] The `id` or element of the `<iframe>` to be jailed on. By default, the DevTools is shown for entire window.
* @param {(dev_win: Window, error: Error) => void} callback Callback with the native window of the DevTools window.
* @returns {void}
*/
NWWindow.prototype.showDevTools = function(iframe, callback) {
let error;
if (process.versions['nw-flavor'] !== 'sdk') {
error = new Error('This API is only available on SDK build flavor. Current build flavor is ' + process.versions['nw-flavor']);
callback(undefined, error);
return;
}
if (process.versions['nw-flavor'] === 'sdk') {
if (nw.App.argv.includes('--disable-devtools')) {
error = new Error('DevTools is disabled by --disable-devtools command line flag.');
callback(undefined, error);
return;
}
}
let id = '';
if (typeof iframe === 'string')
id = iframe;
let f = null;
if (id)
f = this.window.getElementById(id);
else
f = frm || null;
f = iframe || null;
nwNatives.setDevToolsJail(f);
currentNWWindowInternal.showDevTools2Internal(this.cWindow.id, callback);
};
Expand Down Expand Up @@ -760,11 +780,13 @@ apiBridge.registerCustomHook(function(bindingsAPI) {
callback(cwindows.map(create_nw_win));
});
});

apiFunctions.setHandleRequest('isDevToolsOpen', function(callback) {
return chrome.windows.getAll({ populate: true, windowTypes: ['devtools'] }, function(wins) {
callback(wins.length > 0);
})
});

apiFunctions.setHandleRequest('open', function(url, params, callback) {
var options = {'url': url, 'setSelfAsOpener': true, 'type': 'popup'};
//FIXME: unify this conversion code with nwjs/default.js
Expand Down
33 changes: 27 additions & 6 deletions src/resources/api_nw_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,39 @@ apiBridge.registerCustomHook(function(bindingsAPI) {
currentNWWindowInternal.toggleKioskModeInternal();
};

NWWindow.prototype.showDevTools = function(frm, callback) {
var id = '';
if (typeof frm === 'string')
id = frm;
var f = null;
/**
* Open the devtools to inspect the window.
*
* @param {string | HTMLIFrameElement} [iframe] The `id` or element of the `<iframe>` to be jailed on. By default, the DevTools is shown for entire window.
* @param {(dev_win: Window, error: Error) => void} callback Callback with the native window of the DevTools window.
* @returns {void}
*/
NWWindow.prototype.showDevTools = function(iframe, callback) {
let error;
if (process.versions['nw-flavor'] !== 'sdk') {
error = new Error('This API is only available on SDK build flavor. Current build flavor is ' + process.versions['nw-flavor']);
callback(undefined, error);
return;
}
if (process.versions['nw-flavor'] === 'sdk') {
if (nw.App.argv.includes('--disable-devtools')) {
error = new Error('DevTools is disabled by --disable-devtools command line flag.');
callback(undefined, error);
return;
}
}
let id = '';
if (typeof iframe === 'string')
id = iframe;
let f = null;
if (id)
f = this.appWindow.contentWindow.getElementById(id);
else
f = frm || null;
f = iframe || null;
nwNatives.setDevToolsJail(f);
currentNWWindowInternal.showDevToolsInternal(callback);
};

NWWindow.prototype.capturePage = function (callback, options) {
var cb = callback;
if (!options)
Expand Down