简体   繁体   English

Chrome扩展程序,无法获取Cookie

[英]Chrome extension, Can't get cookies

I'm trying to get cookies from my Chrome extension when the app is loading: 应用加载时,我正在尝试从Chrome扩展程序中获取Cookie:

chrome.cookies.getAll({ "url": config.cookie.cookieUrl }, function (cookies) {
            if (callback) callback(cookies);
        });

Sometimes I get the error: 有时我得到错误:

Error during cookies.getAll: No accessible cookie store found for the current execution context. cookies.getAll期间发生错误:找不到针对当前执行上下文的可访问cookie存储。

These posts didn't help me: 这些帖子对我没有帮助:

  1. Stackoverflow discussion Stackoverflow讨论

  2. Google discussion Google讨论

I recently stumbled upon this issue as well and it looks like it's far from being resolved by the chromium project: https://bugs.chromium.org/p/chromium/issues/detail?id=113994 我最近也偶然发现了这个问题,看来铬项目还没有解决这个问题: https : //bugs.chromium.org/p/chromium/issues/detail?id=113994

The solution I found to work is checking first if there's a cookie store available: 我发现可以使用的解决方案是先检查是否有Cookie存储可用:

    function isCookieStoreAvailable() {
        return new Promise(function (resolve, reject) {
            chrome.cookies.getAllCookieStores(function (cookieStores) {                
                if (cookieStores.length) {
                    resolve();
                } else {
                    reject();
                }
            });
        });
    }

Then retrying several times until it becomes available. 然后重试几次直到可用。

Another thing I did was to reload the extension after 10 seconds of retrying but that's only as a last resort: 我要做的另一件事是在重试10秒后重新加载扩展,但这只是最后的选择:

chrome.runtime.reload()

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM