Skip to content

Instantly share code, notes, and snippets.

@macdonst
Last active December 19, 2015 07:18
Show Gist options
  • Save macdonst/5917319 to your computer and use it in GitHub Desktop.
Save macdonst/5917319 to your computer and use it in GitHub Desktop.
Backup, Remove and Restore your Contacts
function backupAllTheContacts() {
navigator.contacts.find(["*"], function(contacts) {
console.log("contacts.length = " + contacts.length);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile("contacts.bak", {create: true, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.onwriteend = function() {
console.log("backup complete");
};
writer.write(JSON.stringify(contacts));
}, onError);
}, onError);
}, onError);
}, onError, {"multiple": true});
}
[
{
"id": "4",
"rawId": "4",
"displayName": "123 456",
"name": {
"familyName": "456",
"formatted": "123 456",
"givenName": "123"
},
"nickname": null,
"phoneNumbers": null,
"emails": null,
"addresses": null,
"ims": [
{
"type": -1,
"value": ".adgjm",
"id": "8",
"pref": false
}
],
"organizations": null,
"birthday": null,
"note": null,
"photos": null,
"categories": null,
"urls": null
},
{
"id": "5",
"rawId": "5",
"displayName": "Dooney Evans",
"name": {
"middleName": "",
"familyName": "Evans",
"formatted": "Dooney Evans",
"givenName": "Dooney"
},
"nickname": null,
"phoneNumbers": [
{
"type": "work",
"value": "512-555-1234",
"id": "11",
"pref": false
}
],
"emails": null,
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": null,
"photos": null,
"categories": null,
"urls": null
},
{
"id": "18",
"rawId": "18",
"displayName": "John Doe",
"name": {
"familyName": "Doe",
"formatted": "John Doe",
"givenName": "John"
},
"nickname": null,
"phoneNumbers": null,
"emails": null,
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": null,
"photos": null,
"categories": null,
"urls": null
},
{
"id": "19",
"rawId": "19",
"displayName": "Rob Doe",
"name": {
"familyName": "Doe",
"formatted": "Rob Doe",
"givenName": "Rob"
},
"nickname": null,
"phoneNumbers": null,
"emails": null,
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": null,
"photos": null,
"categories": null,
"urls": null
}
]
function deleteAllTheContacts() {
var deleteContact = function(contacts) {
console.log("length = " + contacts.length);
// No contacts left, stop saving
if (contacts.length == 0) {
console.log("All contacts removed");
return;
}
var contact = contacts.pop();
contact.remove(function() {
deleteContact(contacts);
}, null);
};
navigator.contacts.find(["*"], deleteContact, onError, {"multiple": true});
}
function restoreContacts() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile("contacts.bak", {create: false, exclusive: false}, function(fileEntry) {
fileEntry.file(function(fp) {
var reader = new FileReader();
reader.onloadend = function (evt) {
console.log("read success");
saveAllTheContacts(JSON.parse(evt.target.result));
};
reader.readAsText(fp);
}, onError);
}, onError);
}, onError);
}
function saveAllTheContacts(contacts) {
var saveContacts = function() {
// No contacts left, stop saving
if (contacts.length == 0) {
console.log("contacts restored");
return;
}
var contactData = contacts.pop();
contactData.id = null;
contactData.rawId = null;
var contact = navigator.contacts.create(contactData);
contact.save(function() {
saveContacts();
}, null);
};
saveContacts();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment