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: "[url]", width: 500, height: 516, type: 'panel'});
When panels in chrome are disabled, it opens a popup. But the problem is that panels are not enabled on every chrome build. but people can enable it by hand on chrome://flags. So when flags are disabled, I want to redirect people to that page so they can enable panels.
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: "[url]", width: 500, height: 516, type: 'panel'});
When panels in chrome are disabled, it opens a popup. But the problem is that panels are not enabled on every chrome build. but people can enable it by hand on chrome://flags. So when flags are disabled, I want to redirect people to that page so they can enable panels.
Share Improve this question asked Nov 29, 2012 at 16:57 user1544892user1544892 01 Answer
Reset to default 12You 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.