- support retrieveSource(), even though it is inadvisable

- throw appropriate error for retrieveDocument()
- warn about undesirable behaviors even when translators are run under Firefox
This commit is contained in:
Simon Kornblith 2011-06-17 19:26:13 +00:00
parent 5e18d64395
commit edceb34153
2 changed files with 33 additions and 21 deletions

View file

@ -334,12 +334,9 @@ Zotero.Translate.Sandbox = {
translate._runHandler("select", items, callback); translate._runHandler("select", items, callback);
if(!haveAsyncCallback) { if(!haveAsyncCallback) {
if(translate.translator[0].browserSupport !== "g") { translate._debug("WARNING: No callback was provided for "+
Zotero.debug("Translate: WARNING: This translator is configured for "+ "Zotero.selectItems(). When executed outside of Firefox, a selectItems() call "+
"non-Firefox browser support, but no callback was provided for "+ "will require that this translator to be called multiple times.", 1);
"selectItems(). When executed outside of Firefox, a selectItems() call "+
"will require that this translator to be called multiple times.", 3);
}
if(returnedItems === null) { if(returnedItems === null) {
// The select handler is asynchronous, but this translator doesn't support // The select handler is asynchronous, but this translator doesn't support

View file

@ -785,6 +785,9 @@ Zotero.Utilities.Translate.prototype.processDocuments = function(urls, processor
* @return {Document} DOM document object * @return {Document} DOM document object
*/ */
Zotero.Utilities.Translate.prototype.retrieveDocument = function(url) { Zotero.Utilities.Translate.prototype.retrieveDocument = function(url) {
if(!Zotero.isFx) throw "Zotero.Utilities.retrieveDocument() is unsupported outside of Firefox";
this._translate._debug("WARNING: Zotero.Utilities.retrieveDocument() is unsupported outside of Firefox", 1);
url = this._convertURL(url); url = this._convertURL(url);
var mainThread = Zotero.mainThread; var mainThread = Zotero.mainThread;
@ -828,25 +831,37 @@ Zotero.Utilities.Translate.prototype.retrieveDocument = function(url) {
* @return {String} Request body * @return {String} Request body
*/ */
Zotero.Utilities.Translate.prototype.retrieveSource = function(url, body, headers, responseCharset) { Zotero.Utilities.Translate.prototype.retrieveSource = function(url, body, headers, responseCharset) {
/* Apparently, a synchronous XMLHttpRequest would have the behavior of this routine in FF3, but this._translate._debug("WARNING: Use of Zotero.Utilities.retrieveSource() is deprecated. "+
* in FF3.5, synchronous XHR blocks all JavaScript on the thread. See "The main thread will be frozen when Zotero.Utilities.retrieveSource() is called outside "+
* http://hacks.mozilla.org/2009/07/synchronous-xhr/. */ "of Firefox, and cross-domain requests will not work.", 1);
url = this._convertURL(url);
if(!headers) headers = null;
if(!responseCharset) responseCharset = null;
var mainThread = Zotero.mainThread; if(Zotero.isFx) {
var xmlhttp = false; /* Apparently, a synchronous XMLHttpRequest would have the behavior of this routine in FF3, but
var listener = function(aXmlhttp) { xmlhttp = aXmlhttp }; * in FF3.5, synchronous XHR blocks all JavaScript on the thread. See
* http://hacks.mozilla.org/2009/07/synchronous-xhr/. */
url = this._convertURL(url);
if(!headers) headers = null;
if(!responseCharset) responseCharset = null;
if(body) { var mainThread = Zotero.mainThread;
Zotero.HTTP.doPost(url, body, listener, headers, responseCharset); var xmlhttp = false;
var listener = function(aXmlhttp) { xmlhttp = aXmlhttp };
if(body) {
Zotero.HTTP.doPost(url, body, listener, headers, responseCharset);
} else {
Zotero.HTTP.doGet(url, listener, responseCharset);
}
while(!xmlhttp) mainThread.processNextEvent(true);
} else { } else {
Zotero.HTTP.doGet(url, listener, responseCharset); // Use a synchronous XMLHttpRequest, even though this is inadvisable
var xmlhttp = new XMLHttpRequest();
xmlhttp.open((body ? "POST" : "GET"), url, false);
xmlhttp.send(body ? body : null);
} }
while(!xmlhttp) mainThread.processNextEvent(true); if(xmlhttp.status >= 400) throw "Zotero.Utilities.retrieveSource() failed: "+xmlhttp.status+" "+xmlhttp.statusText;
if(xmlhttp.status >= 400) throw "retrieveSource failed: "+xmlhttp.status+" "+xmlhttp.statusText;
return xmlhttp.responseText; return xmlhttp.responseText;
} }