📄 gdata_condensed.js
字号:
if (xmlRequest.overrideMimeType) { //only need to do this for mozilla based browsers. IE would throw xmlRequest.overrideMimeType('text/xml'); } LOG_TRACE("Sending..." + xmlRequest, "CallXmlHttp", null); xmlRequest.send(params.payload); LOG_TRACE("Returned, readyState:" + xmlRequest.readyState, "CallXmlHttp", null); if (fAsync === false) { this.HandleOnXmlRequestFinished(xmlRequest, params); } } catch (e) { DBG_ALERT(e.toString()); DBG_ALERT('The exception happened in CallXmlHttp for ' + params.verb_ + ' at URI: ' + params.uri_); throw e; } LOG_TRACE("Exiting...", "CallXmlHttp", null); };/*** async callback method*/function GD_HandleAsyncData(request, params) { if (request.readyState != 4) { return; } LOG_TRACE("Callback done...", "GD_HandleAsyncData", null); GoogleDataFactory.getTransport(). HandleOnXmlRequestFinished(request, params);}/** * handling of errors/results of the xmlhttprequest */ GD_HttpTransport.prototype.HandleOnXmlRequestFinished = function(xmlRequest, params) { switch (xmlRequest.readyState) { case 1: case 2: case 3: DBG_ALERT('Bad Ready State: ' + xmlRequest.status); return false; case 4: if (xmlRequest.status > 299 || (params.allowedStatus !== -1 && xmlRequest.status != params.allowedStatus)) { if (params.retrying !== true) { /* * this might be a redirect/sharding issue, redo the whole thing * in javascript you can not handle the 302 return yourself, * so what happens is we get our auth token, we try to go to the * server, we get a redirect back now the redirect does not redo * the "postdata" nor the custom auth header, so it fails with * 401. Hence, redo it. But only once. */ if (xmlRequest.status === 401) { params = true; this.CallXmlHttp(params); } else if (xmlRequest.status === 412) { // the server want's to redirect us to a shard !! // get the cookie and save it, then retry var cookie = xmlRequest.getResponseHeader( GD_Transport.SETCOOKIE_HEADER); DBG_ASSERT(cookie != null, "we got no cookie back from the redirection server"); if (cookie != null) { this.setCookie(cookie); } // now retry params.retrying = true; this.CallXmlHttp(params); } } DBG_ALERT('Request resulted in a bad status code for the operation: ' + xmlRequest.status + " " + params.allowedStatus); } break; } if (params.callback) { LOG_TRACE("calling callback to feed code", "HandleOnXmlRequestFinished", null); params.callback.call(null, xmlRequest); }}/** * Gaia authentication transport. */function GD_GaiaTransport(serviceName) { GD_HttpTransport.call(this); this.serviceName_ = serviceName; this.gaiaToken_ = null; }UTIL_inherits(GD_GaiaTransport, GD_HttpTransport);GD_GaiaTransport.prototype.toString = function(opt_verbose) { return "GD_GaiaTransport() for " + this.serviceName_;};/*** set method GD_GaiaTransport.gaiaToken_*/GD_GaiaTransport.prototype.setToken = function(string) { this.gaiaToken_ = string; };/*** get method GD_GaiaTransport.gaiaToken_*/GD_GaiaTransport.prototype.getToken = function() { if (/\r|\n/.test(this.gaiaToken_) === true) { alert("potential attack pattern"); } return this.gaiaToken_; };/** * function to get an authtoken from the gaia servers * @param params {XmlHttpParameters} */GD_GaiaTransport.prototype.QueryAuthToken = function(params) { // Create a new request to the authentication URL. LOG_TRACE("Entering...", "QueryAuthToken()", null); var token = null; try { var xmlRequest = XMLX_getHttpRequest(); var response = null; // construct the payload var postData = GD_Transport.AUTH_USER + encodeURIComponent(this.getUserName()) + "&" + GD_Transport.AUTH_PWD + encodeURIComponent(this.getPassword()) + "&" + GD_Transport.AUTH_SOURCE + "&" + GD_Transport.AUTH_SERVICE + encodeURIComponent(this.serviceName_); LOG_TRACE("postData = " + postData, "QueryAuthToken()", null); var responseData = null; var request = xmlRequest; xmlRequest.onreadystatechange = function() { GD_GaiaHandleAsyncData(request, params); } xmlRequest.open("POST", GD_Transport.AUTH_HANDLER, true); xmlRequest.setRequestHeader("content-type", "application/x-www-form-urlencoded"); LOG_TRACE("sending..." + xmlRequest, "QueryAuthToken()", null); xmlRequest.send(postData); } catch (e) { DBG_ALERT(e.toString()); DBG_ALERT('The exception happened in QueryAuthData'); }};/** * callback for the QueryAuthToken * @param params {XmlHttpParameters} */function GD_GaiaHandleAsyncData(request, params) { LOG_TRACE("Entering callback..", "GD_GaiaHandleAsyncData", null); if (request.readyState != 4) { return; } GoogleDataFactory.getTransport().HandleOnQueryAuthRequestFinished( request, params); LOG_TRACE("QueryAuthCallback done...", "GD_GaiaHandleAsyncData", null); }/** * handling of errors/results of the xmlhttprequest * @param request {XMLHttpRequest} * @param params {XmlHttpParameters} */ GD_GaiaTransport.prototype.HandleOnQueryAuthRequestFinished = function( request, params) { var response = null; var token = null; LOG_TRACE("Entering...", "HandleOnQueryAuthRequestFinished", null); if (request.status > 299) { DBG_ALERT('Bad server response during auth operation: ' + this.xmlRequest.status); DBG_ALERT('body: ' + params.postData); } else { response = request.responseText; } DBG_ASSERT(response !== null, "we got no data back from the authentication server"); if (response !== null) { // now parse the result var result = response.match(/^Auth=(.*)/m); DBG_ASSERT(result !== null); DBG_ASSERT(result[0] !== null); DBG_ASSERT(result[1] !== null); if (result !== null) { token = result[1]; } } LOG_TRACE(token, "HandleOnQueryAuthRequestFinished()", null); this.setToken(token); // stack needs to unwind, as you can not call from XmlHttp.onreadystate // into another xmlHttp object window.setTimeout(function() { GD_GaiaContinue(params) }, 0); LOG_TRACE("exiting...", "HandleOnQueryAuthRequestFinished", null);}/** * callback for the QueryAuthToken */function GD_GaiaContinue(params) { LOG_TRACE("GD_GaiaContinue...", "GD_GaiaContinue", null); GoogleDataFactory.getTransport().DoRequest(params);}/*** check if we need to get a token* @returns true if we need to get a token*/GD_GaiaTransport.prototype.NeedToken = function() { if (UTIL_isPersistable(this.getUserName()) && this.getToken() == null) { return true; } return false;};/** * if a token is needed, will get one and set it */GD_GaiaTransport.prototype.PrepareAuthentication = function(params) { if (this.NeedToken()) { this.QueryAuthToken(params); } else { this.DoRequest(params); }};/*** sets the auth header for Gaia, if we have a token* @param xmlHttpRequestObject object to set the header on*/GD_GaiaTransport.prototype.SetHeaders = function(xmlHttpRequestObject) { LOG_TRACE("Entering...", "Gaia.SetHeaders", null); xmlHttpRequestObject.setRequestHeader(GD_Transport.NOREDIRECT_HEADER, "1"); if (this.getCookie() !== null) { // set a previously stored shard cookie LOG_TRACE("setting a stored cookie..." + this.getCookie(), "SetHeaders", null); xmlHttpRequestObject.setRequestHeader(GD_Transport.COOKIE_HEADER, this.getCookie(), null); } if (this.getToken() !== null) { xmlHttpRequestObject.setRequestHeader(GD_Transport.AUTH_HEADER, GD_Transport.AUTH_HEADERVALUE + this.getToken()); } this.Base.SetHeaders(xmlHttpRequestObject);};/*** GD_Factory() is used to access the underlying transport layer* one instance of the factory exists as a global*/function GD_Factory() { this.transPort_ = GD_getTransport(GD_Transport.TRANSPORT_GOOGLECALENDAR); };// set method GD_Factory.transPort_ GD_Factory.prototype.setTransport = function(value) { this.transPort_ = value; };// get method GD_Factory.transPort_GD_Factory.prototype.getTransport = function() { return this.transPort_; };/*** declaration of our global GoogleDataFactory, so that the rest * of the code can share this...*/var GoogleDataFactory = new GD_Factory(); // remove this if you merge if (window.GD_Loader) { // continue loading window.GD_Loader();}// end/* Copyright (c) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.*//*** @fileoverview* this file relies on detect.js to do the browser detection* it abstracts a minimal set of xpath methods so that* you can do cross browser xpath* the code seems to work fine in IE6+, Mozilla, Opera8.5+ and Safari2.0+*/var XMLX_ieProgId_ = undefined;/** initialize XMLX_ieProgId_ */(function () { // Nobody (on the web) is really sure which of the progid's listed is totally // necessary. It is known, for instance, that certain installations of IE // will not work with only Microsoft.XMLHTTP, as well as with MSXML2.XMLHTTP. // Safest course seems to be to do this -- include all known progids for // XmlHttp. if (typeof XMLHttpRequest == 'undefined' && typeof ActiveXObject != 'undefined') { var activeXIdents = [ "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "MICROSOFT.XMLHTTP.1.0", "MICROSOFT.XMLHTTP.1", "MICROSOFT.XMLHTTP" ]; for (var i = 0; i < activeXIdents.length; ++i) { var candidate = activeXIdents[i]; try { new ActiveXObject(candidate); XMLX_ieProgId_ = candidate; return; } catch (e) { // do nothing; try next choice } } // couldn't find any matches throw ("Could not create ActiveXObject. ActiveX might be disabled, or " + "msxml might not be installed"); }})();/** * helper function to return the correct XMLHttpRequest() object */function XMLX_getHttpRequest() { if (XMLX_ieProgId_ !== undefined) { return new ActiveXObject(XMLX_ieProgId_); } else { return new XMLHttpRequest(); }};/** * helper to return a brand new XMLDomDocument*/function XMLX_createDomDocument(optXml) { var doc = null; if (optXml) { if (typeof(optXml) != 'string') { optXml = XMLX_serializeNode(optXml); } } if (document.implementation.createDocument) { if (optXml) { var domParser = new DOMParser(); doc = domParser.parseFromString(optXml, "text/xml"); } else { doc = document.implementation.createDocument("", "", null); } } else { doc = new ActiveXObject("MSXML.DOMDocument"); if (optXml) { doc.loadXML(optXml); } } DBG_ASSERT(doc !== null, "Could not create a DOM document"); return doc; };/** * helper to serialize an xmlnode into text*/function XMLX_serializeNode(xmlNode) { var text = null; try { // this is for Gecko based browsers LOG_TRACE("Trying serializer", "XMLX_serializeNode", null); var xmlSerializer = new XMLSerializer(); // safari 2.02 seems to work fine with this. text = xmlSerializer.serializeToString(xmlNode); } catch ( e ) { try { // Internet Explorer. LOG_TRACE("Trying IE way to serialize", "XMLX_serializeNode", null); text = xmlNode.xml; } catch (e) { // everyone else LOG_TRACE("Trying UTIL_xmlText(node)", "XMLX_serializeNode", null); text = UTIL_xmlText(xmlNode); } } return text; };/** * encapsulates the xpath methods we are exposing*/function XMLX_getNodes(xmlNode, xpath) { DBG_ASSERT(xmlNode, "XML_getNodes: " + xpath); LOG_DEBUG("XML_getNodes: " + xpath); try { var document; if (xmlNode.ownerDocument !== null) { document = xmlNode.ownerDocument; } else { document = xmlNode; } if (Detect.XPathSupport()) { if (Detect.IE_5_5_newer()) { LOG_DEBUG("XML_getNodes: Inside IE5.5 path"); document.setProperty("SelectionLanguage", "XPath"); document.setProperty("SelectionNamespaces", XML.NAMESPACES); return xmlNode.selectNodes(xpath); } else { LOG_DEBUG("XML_getNodes: Inside MOZILLA path"); var nsResolver = function(prefix){ var s = XML.NAMESPACE_MAP[prefix]; if (s) { return s; } else { throw "Unknown prefix: '" + prefix+"'"; } }; var tempResult = document.evaluate(xpath, xmlNode, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); // now make it an array var nodeList = new Array(tempResult.snapshotLength); for (var i = 0; i < nodeList.length; ++i) { nodeList[i] = tempResult.snapshotItem(i); } return nodeList; } } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -