Google Chrome Sync check if enabled via API/Extension?

后端 未结 6 2068
一个人的身影
一个人的身影 2021-01-01 13:14

Is it possible to programmatically check if Chrome Sync is configured in Google Chrome?

The reason I ask is that I am coding an Extension for Chrome that depends on

6条回答
  •  一个人的身影
    2021-01-01 14:14

    Google has a page to see the status of your synced account; the URL of that page is https://www.google.com/settings/chrome/sync. Something you can do to see if an account is synced is opening that status page using cross-domain connections that Google allows for extensions (if it doesn't even return 200-OK status is not synced) and then you use a little JavaScript to extract the "Last Sync" date from that page; after that just save it using the chrome.storage.sync API and then a few seconds later check again the "Last Sync" date, if it changed then you can be 99% sure is synced (or if you want to cover the case of slow synchronizations just use setInterval to wait for it).

    You can use the following JavaScript to extract the date:

    NodeList.prototype.forEach = Array.prototype.forEach;
    var date = null;
    document.querySelectorAll("*").forEach(function(ele){
        var matches = ele.innerText.match(/\b\d{4}\s\d{2}:\d{2}:\d{2}\b.*/);
        if (matches) date = matches[0];
    });
    

    The first time you save that value

    chrome.storage.sync.set({date: date});
    

    And the second time you should compare both values adding something like this:

    chrome.storage.sync.get("date", function (old) {
        if (date !== old) {
            // Is sync!
        } else {
            // not sync.
        }
    });
    

    Good luck.

提交回复
热议问题