-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
142 lines (122 loc) · 4.43 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var clientId =
"173437994395-37f2altagfva3rovf6969s5pn5a9juob.apps.googleusercontent.com";
var apiKey = "AIzaSyAjJh97OmqCGjY7adqnDk0hWxIwY5RRib4";
var scopes = "https://www.googleapis.com/auth/contacts.readonly";
function authClick() {
gapi.client.setApiKey(apiKey);
window.setTimeout(authorize);
}
function authorize() {
gapi.auth.authorize(
{ client_id: clientId, scope: scopes, immediate: false },
authResult
);
}
$(document).ready( function () {
$('#myTable').DataTable();
} );
/**
* Handle response from authorization server.
* @param {Object} authResult Authorization result.
*/
function authResult(_Result) {
var _Div = document.getElementById("divauthresult");
if (_Result && !_Result.error) {
$.get(
"https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" +
_Result.access_token +
"&max-results=500&v=3.0",
function loadPeopleApi() {
gapi.client.load(
"https://people.googleapis.com/$discovery/rest",
"v1",
showContacts
);
}
);
} else {
// Auth Error, allowing the user to initiate authorization by
_Div.innerText = ":( Authtentication Error : " + _Result.error;
}
}
/**
* Load Google People client library. List Contact requested info
*/
/**
* Show Contacts Details display on a table pagesize = 100 connections.
*/
function showContacts() {
var request = gapi.client.people.people.connections.list({
resourceName: "people/me",
pageSize: 500,
"requestMask.includeField":
"person.phone_numbers,person.organizations,person.email_addresses,person.names",
});
request.execute(function (resp) {
if (typeof resp.connections !== "undefined") {
var connections = [resp.connections][0];
if (connections.length > 0) {
var _Html =
"<table class='table table-striped table-bordered' style='width:100%'><tr><th>Name</th><th>Email</th><th>Company</th><th>Phone</th></tr>";
var _EmptyCell = "<td id='example'> - </td>";
console.log('connections', connections);
var contactsData = new Array();
for (i = 0; i < connections.length; i++) {
var person = [connections[i]][0];
var individial = {};
_Html += "<tr>";
if (person.names && person.names.length > 0){
individial['name'] = person.names[0].displayName;
_Html += "<td>" + person.names[0].displayName + "</td>";
}
else _Html += _EmptyCell;
if (person.emailAddresses && person.emailAddresses.length > 0){
individial['email'] = person.emailAddresses[0].value;
_Html += "<td>" + person.emailAddresses[0].value + "</td>";
}
else _Html += _EmptyCell;
if (person.organizations && person.organizations.length > 0){
individial['organisation'] = person.organizations[0].name;
_Html += "<td>" + person.organizations[0].name + "</td>";
}
else _Html += _EmptyCell;
if (person.phoneNumbers && person.phoneNumbers.length > 0){
individial['phone_number'] = person.phoneNumbers[0].value;
_Html += "<td>" + person.phoneNumbers[0].value + "</td>";
}
else _Html += _EmptyCell;
_Html += "</tr>";
contactsData.push(individial);
}
divtableresult.innerHTML = "Contacts found : <br>" + _Html;
saveContactInJsonFile(contactsData);
} else {
divtableresult.innerHTML = "";
divauthresult.innerText = "No Contacts found!";
}
} else {
divtableresult.innerHTML = "";
divauthresult.innerText = "No Contacts found!";
}
});
}
function saveContactInJsonFile(contacts){
$('#loadJson').on('click',function(e){
if(contacts)
saveData(contacts, 'data.json');
});
}
var saveData = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (data, fileName) {
var json = JSON.stringify(data),
blob = new Blob([json], {type: "octet/stream"}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
}());