最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect if chrome panels are enabled - Stack Overflow

programmeradmin3浏览0评论

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 0
Add a ment  | 

1 Answer 1

Reset to default 12

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.

发布评论

评论列表(0)

  1. 暂无评论