|
26 | 26 | /** |
27 | 27 | * Append a link in a <ul></ul>. |
28 | 28 | */ |
29 | | - function appendLink(parent, href, text) { |
| 29 | + function appendLink(parent, href, text, title) { |
30 | 30 | var link = document.createElement("a"); |
31 | 31 | link.href = href; |
32 | 32 | link.textContent = text; |
| 33 | + if(title !== undefined) { |
| 34 | + link.title = title; |
| 35 | + } |
33 | 36 | parent.appendChild(document.createElement("li")).appendChild(link); |
34 | 37 | } |
35 | 38 |
|
|
70 | 73 | 'path': '/drive/v2/files/' + id, |
71 | 74 | 'method': 'GET'}); |
72 | 75 | 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); |
77 | 85 | } |
78 | 86 | }; |
79 | 87 | request.execute(callback); |
|
99 | 107 | } |
100 | 108 |
|
101 | 109 | /** |
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. |
103 | 123 | */ |
104 | 124 | function popupAuth(){ |
105 | | - mdHtmlDisplay.innerHTML = "<p><br/><br/>Please allow the popup window to authorize the access.</p>"; |
106 | 125 | gapi.auth.authorize({'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false, 'authuser':''}, |
107 | 126 | handleAuthResult); |
108 | 127 | } |
109 | 128 |
|
110 | 129 | /** |
111 | 130 | * Check if userId, if not right popup a auth window |
112 | 131 | * |
113 | | - * @param (Object) the userid in page request. |
| 132 | + * @userId (Object) the userid in page request. |
114 | 133 | * @docId (String) the document id. |
115 | 134 | */ |
116 | 135 | function checkAuthuser(userId, docId){ |
|
119 | 138 | 'path': '/oauth2/v1/tokeninfo/?access_token=' + accessToken, |
120 | 139 | 'method': 'GET'}); |
121 | 140 | callback = function(resp) { |
122 | | - if(resp.user_id.toString() !== userId.toString()) { |
| 141 | + if(userId===null || resp.user_id.toString() !== userId.toString()) { |
123 | 142 | authuserIndex++; |
124 | 143 | if(authuserIndex < authuserIndexLimit){ |
125 | 144 | checkAuth(); |
126 | 145 | } |
127 | 146 | else{ |
128 | | - popupAuth(); |
| 147 | + doAuth(); |
129 | 148 | } |
130 | 149 | } |
131 | 150 | else{ |
|
141 | 160 | * @param {Object} authResult Authorization result. |
142 | 161 | */ |
143 | 162 | function handleAuthResult(authResult) { |
144 | | - var authButton = document.getElementById('authorizeButton'); |
145 | | - authButton.style.display = 'none'; |
146 | 163 | if (authResult && !authResult.error) { |
147 | 164 | // Access token has been successfully retrieved, requests can be sent to the API. |
148 | 165 | var queryState = qs("state"); |
|
162 | 179 | mdHtmlDisplay.innerHTML = "<p><br/><br/>No md file to view.</p>"; |
163 | 180 | } |
164 | 181 | } 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(); |
167 | 184 | } |
168 | 185 | } |
169 | 186 |
|
|
0 commit comments