检测是否启用了Chrome面板 [英] Detect if chrome panels are enabled

查看:154
本文介绍了检测是否启用了Chrome面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目前,您可以使用以下代码创建面板:

  chrome.windows.create({url:[url],width:500,height:516,type:'panel'}); 

当chrome中的面板被禁用时,会打开一个弹出窗口。
但问题是面板没有在每个chrome构建中启用。但人们可以通过chrome://标志手动启用它。因此,当标志被禁用时,我想将用户重定向到该页面,以便他们可以启用面板。

解决方案

您可以检测打开的窗口是一个面板,它在 alwaysOnTop 布尔属性,创建rel =noreferrer> chrome.windows.create

  chrome.windows.create({
url:'... url ...',// ...
type:'panel'
},function( windowInfo){
//如果windowInfo.alwaysOnTop为true,那么它是一个面板。
//否则,它只是一个弹出
});

如果您想检测是否启用标志,请创建窗口,读取值,然后去掉它。由于创建过程是异步的,因此必须使用回调来实现值检索。

  var _isPanelEnabled; 
var _isPanelEnabledQueue = [];
function getPanelFlagState(callback){
if(typeof callback!='function')throw Error('callback function required');
if(typeof _isPanelEnabled =='boolean'){
callback(_isPanelEnabled); //使用缓存的结果
return;
}
_isPanelEnabledQueue.push(callback);

if(_isPanelEnabled =='checking')
return;

_isPanelEnabled ='检查';
chrome.windows.create({
url:'about:blank',
type:'panel'
},function(windowInfo){
_isPanelEnabled = windowInfo .alwaysOnTop;
chrome.windows.remove(windowInfo.id);

//处理所有排队的回调
while(callback = _isPanelEnabledQueue.shift()){
回调(windowInfo.alwaysOnTop);
}
});

//用法:
getPanelFlagState(function(isEnabled){
alert('Panels''isEnabled);
});

由于只能通过重新加载Chrome浏览器来切换标志,因此缓存该标志(如功能所示)。为了确保窗口创建测试只发生一次,回调会排队。


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.

解决方案

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.

这篇关于检测是否启用了Chrome面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆