Skip to content

Instantly share code, notes, and snippets.

@trevordixon
Last active January 17, 2019 23:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trevordixon/0245e9c1af32269b25cad74602161ef5 to your computer and use it in GitHub Desktop.
Save trevordixon/0245e9c1af32269b25cad74602161ef5 to your computer and use it in GitHub Desktop.
This script downloads all of your lds.org contacts as a vCard file which can be imported into Google Contacts, MacOS Contacts, and many other contact managers. Sign in to lds.org, then open the developer console (http://debugbrowser.com/#chrome) and paste the following code into the console.
// First, get the unit number.
const getUnitNumber = fetch('https://www.lds.org/mobiledirectory/services/ludrs/1.1/mem/mobile/current-user-unitNo', {
credentials: 'include',
})
.then(response => response.json())
.then(response => response.message);
// Then download all contacts.
const getContacts = getUnitNumber.then(unitNumber => fetch(`https://www.lds.org/mobiledirectory/services/v2/ldstools/member-detaillist-with-callings/${unitNumber}`, {
credentials: 'include',
}))
.then(response => response.json());
// Finally, turn into vCard, then download the vCard file as 'members.vcf'.
getContacts.then(vcard).then(saveAs.bind(this, 'members.vcf'));
/* ************************************************************************** */
// Takes the JSON response and returns a string of vCard contact entries.
function vcard(response) {
return response.households.reduce((members, hh) => {
for (let p of [hh.headOfHouse, hh.spouse]) {
if (!p) continue;
const phone = (p.phone || '').trim();
const email = (p.email || '').trim();
let addr = hh.desc1 || '';
if (hh.desc2) addr += ' ' + hh.desc2;
members += `BEGIN:VCARD
VERSION:3.0
N:${p.surname};${p.givenName};;;
EMAIL;type=INTERNET;type=HOME;type=pref:${email}
TEL;type=HOME;type=VOICE;type=pref:${phone}
ADR;type=HOME;type=pref:;;${addr};;;;
END:VCARD
`;
}
return members;
}, '');
}
// Takes a filename and a string and downloads the given text with the provided filename.
function saveAs(filename, text) {
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment