Contacts

The Contacts API gives privileged Web applications an access to the user's address book maintained in the system and allow reading & modifying the contacts through the vCard-like format.

The initial version of the API was created for Firefox OS and implemented in Firefox, but disabled due to implementation flaws. There is no working implementation available as of today and no interest in that area is visible.

API glimpse

navigator.contacts.find(filterAndSortOptions)
Returns a Promise resolved with the array of contacts from the address book according to the criteria specified.
navigator.contacts.save(new Contact())
Returns a Promise resolved when the newly created contact is inserted into the address book.
navigator.contacts.remove(contact)
Returns a Promise resolved when the contact is removed from the address book.
navigator.contacts.addEventListener('contactschange', listener)
An event fired when the address book data has changed, containing all the added, removed and changed contact entries.

Live Demo

 
  • function readContacts() {
      var api = navigator.contacts || navigator.mozContacts;
    
      if (api) {
        var criteria = {
          sortBy: 'familyName',
          sortOrder: 'ascending'
        };
    
        var finder = api.find(criteria);
        if (finder && finder.then) {
          finder.then(function (contacts) {
              consoleLog('Found ' + contacts.length + ' contacts.');
              if (contacts.length) {
                consoleLog('First contact: ' + contacts[0].givenName[0] + ' ' + contacts[0].familyName[0]);
              }
            })
            .catch(function (err) {
              consoleLog('Fetching contacts failed: ' + err.name);
            });
        } else {
          consoleLog('Only obsolete Contacts API accessible.');
        }
      } else {
        consoleLog('Contacts API not supported.');
      }
    }
    
    function consoleLog(data) {
      var logElement = document.getElementById('log');
      logElement.innerHTML += data + '\n';
    }
  • <p>
      <button class="btn btn-lg btn-default" onclick="readContacts()">Read Contacts</button>
    </p>
    
    <pre id="log"></pre>
    

Resources