-
Notifications
You must be signed in to change notification settings - Fork 0
/
homeWardBookmarklet.js
69 lines (58 loc) · 2.85 KB
/
homeWardBookmarklet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
javascript:(function () {
const memberListUrl = 'https://www.lds.org/mls/mbr/services/report/member-list?lang=eng';
const memberProfileUrl = 'https://www.lds.org/mls/mbr/records/member-profile/service/';
let csvMembersWithParents = 'Name,"Father\'s Name","Father\'s Unit","Mother\'s Name","Mother\'s Unit"\n';
let memberProfileCounter;
generateHomeWardList();
function generateHomeWardList() {
requestJson(memberListUrl, onGetMemberListSuccess, () => console.error('Failed to get member list.'));
}
function onGetMemberListSuccess(list) {
memberProfileCounter = list.length;
for (let i = 0; i < list.length; i++) {
requestJson(memberProfileUrl + list[i].id + '?lang=eng', onGetMemberProfileSuccess, () => console.error('Failed to get member profile ' + list[i].id));
}
}
function onGetMemberProfileSuccess(member) {
let name = member.individual.name;
let fatherName = member.family.parents && member.family.parents.father && member.family.parents.father.name ? member.family.parents.father.name : '';
let fatherUnit = member.family.parents && member.family.parents.father && member.family.parents.father.unitNumber ? member.family.parents.father.unitNumber : '';
let motherName = member.family.parents && member.family.parents.mother && member.family.parents.mother.name ? member.family.parents.mother.name : '';
let motherUnit = member.family.parents && member.family.parents.mother && member.family.parents.mother.unitNumber ? member.family.parents.mother.unitNumber : '';
csvMembersWithParents += '"' + name + '","' + fatherName + '",' + fatherUnit + ',"' + motherName + '",' + motherUnit + '\n';
memberProfileCounter--;
if (memberProfileCounter === 0) {
saveMembersWithParents(csvMembersWithParents);
}
}
function requestJson(url, onSuccess, onError) {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
if (onSuccess) {
let response = JSON.parse(xhr.responseText);
onSuccess(response);
}
} else {
if (onError) {
onError();
}
}
}
};
xhr.open('GET', url);
xhr.send();
}
function saveMembersWithParents(text) {
let blob = new Blob([text], {type: 'text/plain'});
var a = document.createElement('a');
a.style = 'display: none';
a.href = window.URL.createObjectURL(blob);
a.download = 'homeWard.csv';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}
}());