forked from orbitbot/chrome-extensions-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
71 lines (61 loc) · 1.86 KB
/
background.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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var oauth = ChromeExOAuth.initBackgroundPage({
'request_url' : 'https://www.google.com/accounts/OAuthGetRequestToken',
'authorize_url' : 'https://www.google.com/accounts/OAuthAuthorizeToken',
'access_url' : 'https://www.google.com/accounts/OAuthGetAccessToken',
'consumer_key' : 'anonymous',
'consumer_secret' : 'anonymous',
'scope' : 'http://www.google.com/m8/feeds/',
'app_name' : 'Sample - OAuth Contacts'
});
var contacts = null;
function setIcon() {
if (oauth.hasToken()) {
chrome.browserAction.setIcon({ 'path' : 'img/icon-19-on.png'});
} else {
chrome.browserAction.setIcon({ 'path' : 'img/icon-19-off.png'});
}
};
function onContacts(text, xhr) {
contacts = [];
var data = JSON.parse(text);
for (var i = 0, entry; entry = data.feed.entry[i]; i++) {
var contact = {
'name' : entry['title']['$t'],
'id' : entry['id']['$t'],
'emails' : []
};
if (entry['gd$email']) {
var emails = entry['gd$email'];
for (var j = 0, email; email = emails[j]; j++) {
contact['emails'].push(email['address']);
}
}
if (!contact['name']) {
contact['name'] = contact['emails'][0] || "<Unknown>";
}
contacts.push(contact);
}
chrome.tabs.create({ 'url' : 'contacts.html'});
};
function getContacts() {
oauth.authorize(function() {
console.log("on authorize");
setIcon();
var url = "http://www.google.com/m8/feeds/contacts/default/full";
oauth.sendSignedRequest(url, onContacts, {
'parameters' : {
'alt' : 'json',
'max-results' : 100
}
});
});
};
function logout() {
oauth.clearTokens();
setIcon();
};
setIcon();
chrome.browserAction.onClicked.addListener(getContacts);