-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathswitch.html
More file actions
157 lines (142 loc) · 5.94 KB
/
switch.html
File metadata and controls
157 lines (142 loc) · 5.94 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html>
<head>
<title>Switch: Gmail</title>
<meta charset='utf-8' />
<link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.min.css" />
<link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.components.min.css" />
</head>
<body>
<H1>Switch: Gmail</H1>
<p><a href="index.html">Switch: Outlook</a></p>
<!--Add buttons to initiate Gmail API auth sequence and sign out-->
<button id="authorize-button" style="display: none;">Authorize</button>
<button id="signout-button" style="display: none;">Sign Out</button>
<ul id="content" class="ms-List"></ul>
<script type="text/javascript">
// Google APIs OAuth Web Client ID and API key from the Developer Console
var CLIENT_ID = '319613019463-n61aht0ftue16tt83lpp3msd1487jsk5.apps.googleusercontent.com';
// Array of Google API discovery doc URLs for APIs (from the Gmail API quickstart example)
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"];
// Authorization scopes required to read Gmail filters.
var SCOPES = 'https://www.googleapis.com/auth/gmail.settings.basic';
var authorizeButton = document.getElementById('authorize-button');
var signoutButton = document.getElementById('signout-button');
/**
* On load, called to load the auth2 library and API client library.
*/
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
/**
* Initializes the API client library and sets up sign-in state
* listeners.
*/
function initClient() {
gapi.client.init({
discoveryDocs: DISCOVERY_DOCS,
clientId: CLIENT_ID,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
});
}
/**
* Called when the signed in status changes, to update the UI
* appropriately. After a sign-in, the API is called.
*/
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = 'none';
signoutButton.style.display = 'block';
listFilters();
} else {
authorizeButton.style.display = 'block';
signoutButton.style.display = 'none';
}
}
/**
* Sign in the user upon button click.
*/
function handleAuthClick(event) {
gapi.auth2.getAuthInstance().signIn();
}
/**
* Sign out the user upon button click.
*/
function handleSignoutClick(event) {
gapi.auth2.getAuthInstance().signOut();
}
/**
* Append a span element to the div with id content containing the given message
* as its text node. Used to display the results of the API call.
*
* @param {string} message Text to be placed in pre element.
*/
function appendSpan(message) {
var parentDiv = document.getElementById('content');
var childSpan = document.createElement("span");
var textContent = document.createTextNode(message);
parentDiv.appendChild(childSpan).appendChild(textContent);
}
/**
* Append a div element to the div with id content containing the given message
* as its text node. Used to display the results of the API call.
*
* @param {string} message Text to be placed in pre element.
*/
function appendDiv(message) {
var parentDiv = document.getElementById('content');
var childDiv = document.createElement("div");
var textContent = document.createTextNode(message);
parentDiv.appendChild(childDiv).appendChild(textContent);
}
function appendListItem(primaryText, secondaryText) {
var parent = document.getElementById('content');
var listItem = document.createElement("li");
listItem.classList.add("ms-ListItem");
var primaryEl = document.createElement("span");
primaryEl.classList.add("ms-ListItem-primaryText");
primaryEl.appendChild(document.createTextNode(primaryText));
var secondaryEl = document.createElement("span");
secondaryEl.classList.add("ms-ListItem-secondaryText");
secondaryEl.appendChild(document.createTextNode(secondaryText));
listItem.appendChild(primaryEl);
listItem.appendChild(secondaryEl);
parent.appendChild(listItem);
}
/**
* Print all Filters in the authorized user's settings. If no filters
* are found an appropriate message is printed.
*/
function listFilters() {
gapi.client.gmail.users.settings.filters.list({
'userId': 'me'
}).then(function(response) {
var filters = response.result.filter;
appendListItem('Gmail filters:', '');
if (filters && filters.length > 0) {
for (i = 0; i < filters.length; i++) {
var filter = filters[i];
if (filter.criteria.from && filter.action.addLabelIds && filter.action.removeLabelIds=="INBOX") {
appendListItem("When email is from: " + filter.criteria.from,
"Remove from Inbox and apply label: " + filter.action.addLabelIds);
}
}
} else {
appendDiv('No Filters found.');
}
});
}
</script>
<script async defer src="https://apis.google.com/js/api.js"
onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>
</body>
</html>