本文介绍了chrome ... addListener,如何等待chrome.storage.sync.get?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的扩展程序正在修改一些网址。它工作正常,但现在我想检查是否在设置中启用了修改。

My extension is modifying some URLs. It worked fine, but now I want to check if modifying is enabled in the settings or not.

chrome.webRequest.onBeforeRequest.addListener
(
    modifyUrl,
    {urls: ['http://somewebsite/*'], types: ['main_frame']},
    ['blocking']
);

问题是我不知道如何等待这个值。我必须在退出 modifyUrl 之前获取设置。这可能吗?如果这是C#,我可能会在调用 sync.get 之后使用 ManualResetEvent 等。

The problem is that I do not know how to wait for the value. I must get the setting before exiting modifyUrl. Is this possible? If this were C#, I would probably have used ManualResetEvent or something after calling the sync.get.

function modifyUrl(details)
{
    chrome.storage.sync.get("someSetting",
          function (data)
          {
            //I can get the setting here
          }
    );

    //how to know the setting here?
    if(enabled in the setting)
    {
        return {redirectUrl: some different url};
    }
}


推荐答案

参见以获取详细说明。

简而言之,阻塞 onBeforeRequest事件是同步的,因此它不能依赖异步函数,例如所有chrome。* API回调来获取其返回值。

In short, a blocking onBeforeRequest event is synchronous so it can't depend on asynchronous functions such as all chrome.* API callbacks to derive its return value.

将数据缓存在全局变量中,并使用在其他地方更改时更新它,如果您使用chrome.storage.sync(它在配置文件同步时更新)很容易发生。

Cache the data in a global variable, and use chrome.storage.onChanged to update it when changed elsewhere, which can easily happen if you use chrome.storage.sync (it's updated on profile sync).

var settingEnabled; // a global variable
chrome.storage.sync.get("someSetting", function (data) {
    var settingEnabled = data.someSetting;
});

chrome.webRequest.onBeforeRequest.addListener(
    modifyUrl,
    {urls: ['http://somewebsite/*'], types: ['main_frame']},
    ['blocking']
);

function modifyUrl(details) {
    if (settingEnabled) {
        return {redirectUrl: 'http://example.com'};
    }
}

chrome.storage.onChanged.addListener(function(changes, area) {
    if (area == "sync" && "someSetting" in changes) {
        settingEnabled = changes.someSetting.newValue;
    }
});

当然,如果你想完全禁用处理,只需分离监听器:

Of course, in case you want to disable processing completely, simply detach the listener:

function toggleListener(enable) {
    if (enable) {
        chrome.webRequest.onBeforeRequest.addListener(
            modifyUrl,
            {urls: ['http://somewebsite/*'], types: ['main_frame']},
            ['blocking']
        );
    } else {
        chrome.webRequest.onBeforeRequest.removeListener(modifyUrl);
    }
}

chrome.storage.onChanged.addListener(function(changes, area) {
    if (area == "sync" && "someSetting" in changes) {
        toggleListener(changes.someSetting.newValue);
    }
});

chrome.storage.sync.get("someSetting", function (data) {
    toggleListener(data.someSetting);
});

另一个有趣的方法是。

这篇关于chrome ... addListener,如何等待chrome.storage.sync.get?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:27