Google Chrome Sync check if enabled via API/Extension?
Asked Answered
I

6

23

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 Chrome Sync, and would like to check/inform the user if it's not configured.

Before posting this question I checked the obvious places (Chrome Extension APIs, StackExchange, and Google), but so far I haven't had any luck.

If anyone has an idea/solution I'd appreciate the help.

Cheers.

Indurate answered 16/1, 2012 at 7:45 Comment(0)
C
7

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.

Chanukah answered 12/3, 2013 at 1:29 Comment(9)
What if the user signed out?Cattegat
You check if the status of the ajax response is 200-OK, if is any other thing (eg. 302-Redirect) the user signed out and you can't be synced if you are not logged in.Chanukah
The chrome browser can be logged in, but that doesn't mean the cookies for that user are present are they? Currently I'm using my chrome browser synced using account A, and I am logged in to google.com using account B.Eada
Yeah I was wrong, sorry, it only works for 95% of all the cases. Those where is completely normal to be logged in and logged in with the same account because Chrome logs you in as soon as you sync.Chanukah
What if the users' sync account is different than the account with which he/she logged in to googleFavela
Inside an extension you can check if that's the case making an hxr request to gmail, google.com or some other major google domain where the google account is displayedChanukah
Scraping Google's pages isn't the best idea ever. Especially if you don't know what you're doing and you cause infinite loops in your requests... ;-)Urethrectomy
Nice idea, you can even find those values by names, but don't forget to add ?hl=en or something at the end, if not, different origin client langauge will give different results. Something like this: https://www.google.com/settings/chrome/sync?hl=enQuadrat
This does not work anymore. Google blocked it. No error, just getting error code (fail), no more information.Quadrat
B
1

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.

Burn answered 9/3, 2013 at 11:41 Comment(2)
Good idea...but it does not work. The size limit stays the sameFavela
Oeh I like this one =) It doesn't work, but it's very clever!Hominid
Q
1

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.

Quadrat answered 18/1, 2015 at 11:40 Comment(0)
S
1

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.

Susysuter answered 18/1, 2015 at 11:50 Comment(0)
F
1

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.

Favela answered 17/4, 2015 at 5:50 Comment(1)
You need to add another permission as well, 'identity.email' otherwise email and id will be empty strings.Pentameter
S
1

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

Sapsago answered 17/9, 2019 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.