Google Chrome extension: How to find out if a user

2020-08-04 10:12发布

问题:

As part of a Google Chrome extension that I'm building I need to be able to tell if a user has signed in to the Google Chrome browser while the extension is enabled.

How can I do this?

Please note that using OAuth2 (and thus, the chrome.identity API) is beyond the scope of my project so I need to find another way.

EDIT: my question is not a duplicate of this one because the solution in that thread no longer works.

回答1:

Check if LSID cookie is set:

chrome.cookies.get({url:'https://accounts.google.com', name:'LSID'}, function(cookie) {
    if (cookie) {
        console.log('Sign-in cookie:', cookie);
    }
});

manifest.json permissions: "cookies", "https://accounts.google.com/"



回答2:

chrome.identity.getAuthToken({interactive: false}, function (token) {
    if (!token) {
        if (chrome.runtime.lastError.message.match(/not signed in/)) {
            console.log("not singed in");
        } else {
            console.log("singed in");
        }
    }
});

And don't forget to add "identity" to permissions.