Google Chrome Sync check if enabled via API/Extension?

后端 未结 6 2025
一个人的身影
一个人的身影 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 13:54

    Alternative way to find if service is synced or not:

    Get https://www.google.com/settings/chrome/sync?hl=en with language set to en, and then:

    var is_sync = 0;
    NodeList.prototype.forEach = Array.prototype.forEach;
    document.querySelectorAll(".Tda.CP").forEach(function(ele){
        if(ele.innerHTML == 'Omnibox History')
        is_sync = parseInt(ele.parentElement.querySelector('.FP.Uda').innerHTML);
    });
    

    Please note that you may want to verify last synchronization date, that is .zU.EP span, I got something like this:

    Last time synced on Wednesday, November 12, 2014 at 1:03:29 PM UTC+1

    This is the function I wrote to convert that to time:

    last_sync = new Date(document.querySelector('.zU.EP').innerHTML.replace('Last time synced on ','').match(/.*\sat/)[0].replace(' at','')).getTime();
    

    Maybe not the best, but works, and gives an idea.

    0 讨论(0)
  • 2021-01-01 13:58

    There is an active feature request I made for this:

    https://code.google.com/p/chromium/issues/detail?id=361109

    However, until this is implemented, there is no way to tell. Sync may be enabled for the account, but disabled on a particular machine or disabled for extensions/apps only.

    0 讨论(0)
  • 2021-01-01 14:06

    I haven't tested this, it's just an idea. Like you, I haven't been able to find any documentation.

    The docs at https://developer.chrome.com/extensions/storage.html state that:

    Even if a user disables syncing, storage.sync will still work. In this case, it will behave identically to storage.local.

    However, storage.sync and storage.local have different QUOTA_BYTES limits; 102,400 and 5,242,880 respectively. According to the docs;

    Updates that would cause this limit to be exceeded fail immediately and set runtime.lastError.

    So, what happens if you try to set an object of, say, 200,000 bytes into storage.sync? If sync is enabled it should fail - but if sync is disabled, and thus storage.sync is behaving like storage.local, will it succeed? If that does succeed, then you can simply try to set a very big object into sync, check if it succeeded, then remove it, and determine if sync is enabled based on that.

    0 讨论(0)
  • 2021-01-01 14:13

    Chrome now provides chrome.identity.getProfileUserInfo api. You can use this api to check if user is signed into chrome or not. You will need to add identity permission in your manifest.json (though it won't ask user for any extra permission and I think it's because we don't actually use oAuth)

    Here's the code snippet:

    manifest.json

    ....
    "permissions": [
      "identity"
    ]
    ....
    

    background page

    chrome.identity.getProfileUserInfo(function(data) {
        if (data.id) {
            alert("User is signed in to chrome!");
        }
        else
        {
            alert("User is not signed in to chrome.");
        }
    });
    

    It's possible that user is signed in to chrome but has specifically disabled sync but such cases will be very rare. This is the closest method I could find.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-01 14:20

    I was able to get the status of sync is turned on or no along with the email ids. Here is the code :

    create a manifest.json file with below content

        {
          "name": "Test turn on sync",
          "version": "1.0",
          "description": "Extension",
          "permissions":[
          "identity", 
          "identity.email"
          ],
          "background": {
          "scripts": ["background.js"],
          "persistent": false
       },
          "manifest_version": 2
     }
    

    Create a background.js file in the same folder

    chrome.identity.getProfileUserInfo(function(data) {
    if (data.id) {
        alert(data);
    }
    else
    {
        alert("User is not signed in to chrome.");
    }
    

    });

    Add the folder as chrome extension Run the extension from chrome with the help of identity.email in permissions, you will be able to get even the email-id of the user who has turned on sync, along with status P.S : I modified the code from below answered thread

    0 讨论(0)
提交回复
热议问题