I would like to detect if panels are enabled in chrome, in javascript.
Currently, you can create a panel with this code:
chrome.windows.create({ url:
You can detect if the opened window is a panel using the alwaysOnTop
boolean property in the callback of chrome.windows.create:
chrome.windows.create({
url: '...url...', // ...
type: 'panel'
}, function(windowInfo) {
// if windowInfo.alwaysOnTop is true , then it's a panel.
// Otherwise, it is just a popup
});
If you want to detect whether flags are enabled or not, create the window, read the value, then remove it. Because the creation process is asynchrous, the value retrieval must be implemented using a callback.
var _isPanelEnabled;
var _isPanelEnabledQueue = [];
function getPanelFlagState(callback) {
if (typeof callback != 'function') throw Error('callback function required');
if (typeof _isPanelEnabled == 'boolean') {
callback(_isPanelEnabled); // Use cached result
return;
}
_isPanelEnabledQueue.push(callback);
if (_isPanelEnabled == 'checking')
return;
_isPanelEnabled = 'checking';
chrome.windows.create({
url: 'about:blank',
type: 'panel'
}, function(windowInfo) {
_isPanelEnabled = windowInfo.alwaysOnTop;
chrome.windows.remove(windowInfo.id);
// Handle all queued callbacks
while (callback = _isPanelEnabledQueue.shift()) {
callback(windowInfo.alwaysOnTop);
}
});
}
// Usage:
getPanelFlagState(function(isEnabled) {
alert('Panels are ' + isEnabled);
});
Because the flag can only be toggled by reloading the Chrome browser, it makes sense to cache the value of the flag (as shown in the function). To make sure that the window creation test happens only once, the callbacks are queued.