From 0f684fee6e9d13e4d450d328ef48b46def7882ca Mon Sep 17 00:00:00 2001 From: aurimasv Date: Thu, 5 Apr 2012 02:44:33 -0500 Subject: [PATCH] Move _detectWeb to Zotero.Translate.Web.prototype --- chrome/content/zotero/recognizePDF.js | 46 +++++-------------- .../zotero/xpcom/translation/translate.js | 18 +++++++- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/chrome/content/zotero/recognizePDF.js b/chrome/content/zotero/recognizePDF.js index 741210326c..8cf3f81848 100644 --- a/chrome/content/zotero/recognizePDF.js +++ b/chrome/content/zotero/recognizePDF.js @@ -463,43 +463,19 @@ Zotero_RecognizePDF.Recognizer.prototype._scrape = function(/**Zotero.Translate* this._hiddenBrowser.removeEventListener("pageshow", this._scrape.caller, true); translate.setDocument(this._hiddenBrowser.contentDocument); - //closure? - (function(me) { - me._detectWeb(translate, function(translate, itemType) { - if(itemType) { - translate.translate(me._libraryID, false); - } else { - me._queryGoogle(); - } - }); - })(this); + var me = this; + + translate.setHandler("translators", function(translate, detected) { + if(detected.length) { + translate.translate(me._libraryID, false); + } else { + me._queryGoogle(); + } + }); + + translate._detectWeb(); } -/** - * Performs detectWeb on a loaded page and calls callback - * with the itemType returned by the first matching translator or false - * @private - */ -Zotero_RecognizePDF.Recognizer.prototype._detectWeb = function(translate, callback) { - //do we need to use closure here for callback??? - (function(callback) { - translate.setHandler("translators", function(translate, detected) { - callback(translate, (detected.length && detected[0].itemType) || false ); - }); - })(callback); - - // Only one simultaneous instance allowed. - if(this._currentState === "detect") throw new Error("RecognizePDF: _scrape is already running"); - translate._currentState = "detect"; - translate._getAllTranslators = false; //though this shouldn't matter, since we're only going to load one translator - - var translators = new Array(); - translators.push(Zotero.Translators.get(translate.translator[0])); - if(!translators[0]) throw new Error('RecognizePDF: could not get translator ' + translate.translator[0]); - - translate._getTranslatorsTranslatorsReceived(translators); -}; - /** * Callback to pick first item in the Google Scholar item list * @private diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js index 2e6a03ba64..5b10f424f4 100644 --- a/chrome/content/zotero/xpcom/translation/translate.js +++ b/chrome/content/zotero/xpcom/translation/translate.js @@ -779,7 +779,7 @@ Zotero.Translate.Base.prototype = { * translators * valid: all * called: when a translator search initiated with Zotero.Translate.getTranslators() is - * complete + * complete or when _detectWeb is called and completes. * passed: an array of appropriate translators * returns: N/A * @param {Function} handler Callback function. All handlers will be passed the current @@ -1426,6 +1426,22 @@ Zotero.Translate.Web.prototype._getTranslatorsGetPotentialTranslators = function }); } +/** + * Run detectWeb on a loaded document using the set translator + */ +Zotero.Translate.Web.prototype._detectWeb = function() { + // Only one simultaneous instance allowed. + if(this._currentState === "detect") throw new Error("Translate: _detectWeb is already running"); + this._currentState = "detect"; + this._getAllTranslators = false; + + var translators = new Array(); + translators.push(Zotero.Translators.get(this.translator[0])); + if(!translators[0]) throw new Error('Translate: could not get translator ' + this.translator[0]); + + this._getTranslatorsTranslatorsReceived(translators); +} + /** * Bind sandbox to document being translated */