Skip to content

Commit d83f24f

Browse files
author
socrateslee
committed
adjust several auth settings; better multi accout handle; add a button when popup auth blocked
1 parent 89f4654 commit d83f24f

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

js/mdreader.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
/**
2727
* Append a link in a <ul></ul>.
2828
*/
29-
function appendLink(parent, href, text) {
29+
function appendLink(parent, href, text, title) {
3030
var link = document.createElement("a");
3131
link.href = href;
3232
link.textContent = text;
33+
if(title !== undefined) {
34+
link.title = title;
35+
}
3336
parent.appendChild(document.createElement("li")).appendChild(link);
3437
}
3538

@@ -70,10 +73,15 @@
7073
'path': '/drive/v2/files/' + id,
7174
'method': 'GET'});
7275
callback = function(resp) {
73-
appendLink(topNav, getOutboundLink(id), resp.title);
74-
document.title = "[" + resp.title + "]" + "mdReader -- Markdown reader for Google Drive";
75-
if(resp.downloadUrl){
76-
getDocumentContent(resp.downloadUrl);
76+
if(!resp.error){
77+
appendLink(topNav, getOutboundLink(id), resp.title, 'Please right click copy the link address for sharing.');
78+
document.title = "[" + resp.title + "]" + "mdReader -- Markdown reader for Google Drive";
79+
if(resp.downloadUrl){
80+
getDocumentContent(resp.downloadUrl);
81+
}
82+
}
83+
else{
84+
checkAuthuser(null, id);
7785
}
7886
};
7987
request.execute(callback);
@@ -99,18 +107,29 @@
99107
}
100108

101109
/**
102-
* Popup a authorize window.
110+
* Init the authorize process.
111+
*/
112+
function doAuth(){
113+
mdHtmlDisplay.innerHTML = "<p><br/><br/>Please allow the popup window to authorize the access, <br/>"
114+
+ "or manually click <input type=\"button\" id=\"authorizeButton\" value=\"Authorize\" />"
115+
+ " if popup window is blocked.</p>";
116+
var authButton = document.getElementById('authorizeButton');
117+
authButton.onclick = popupAuth;
118+
popupAuth();
119+
}
120+
121+
/**
122+
* Do the popup auth.
103123
*/
104124
function popupAuth(){
105-
mdHtmlDisplay.innerHTML = "<p><br/><br/>Please allow the popup window to authorize the access.</p>";
106125
gapi.auth.authorize({'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false, 'authuser':''},
107126
handleAuthResult);
108127
}
109128

110129
/**
111130
* Check if userId, if not right popup a auth window
112131
*
113-
* @param (Object) the userid in page request.
132+
* @userId (Object) the userid in page request.
114133
* @docId (String) the document id.
115134
*/
116135
function checkAuthuser(userId, docId){
@@ -119,13 +138,13 @@
119138
'path': '/oauth2/v1/tokeninfo/?access_token=' + accessToken,
120139
'method': 'GET'});
121140
callback = function(resp) {
122-
if(resp.user_id.toString() !== userId.toString()) {
141+
if(userId===null || resp.user_id.toString() !== userId.toString()) {
123142
authuserIndex++;
124143
if(authuserIndex < authuserIndexLimit){
125144
checkAuth();
126145
}
127146
else{
128-
popupAuth();
147+
doAuth();
129148
}
130149
}
131150
else{
@@ -141,8 +160,6 @@
141160
* @param {Object} authResult Authorization result.
142161
*/
143162
function handleAuthResult(authResult) {
144-
var authButton = document.getElementById('authorizeButton');
145-
authButton.style.display = 'none';
146163
if (authResult && !authResult.error) {
147164
// Access token has been successfully retrieved, requests can be sent to the API.
148165
var queryState = qs("state");
@@ -162,8 +179,8 @@
162179
mdHtmlDisplay.innerHTML = "<p><br/><br/>No md file to view.</p>";
163180
}
164181
} else {
165-
// No access token could be retrieved, show the button to start the authorization flow.
166-
popupAuth();
182+
// No access token could be retrieved, start the authorization flow.
183+
doAuth();
167184
}
168185
}
169186

0 commit comments

Comments
 (0)