Detect if chrome panels are enabled

后端 未结 1 1099
青春惊慌失措
青春惊慌失措 2021-01-01 05:25

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:         


        
相关标签:
1条回答
  • 2021-01-01 05:59

    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 讨论(0)
提交回复
热议问题