From cb6ae9cec72ac0b6d1086fd46a8a0f0b1dba2a89 Mon Sep 17 00:00:00 2001 From: aurimasv Date: Wed, 4 Apr 2012 23:08:36 -0500 Subject: [PATCH 1/6] Perform detect web before calling Google Scholar translator --- chrome/content/zotero/recognizePDF.js | 43 ++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/recognizePDF.js b/chrome/content/zotero/recognizePDF.js index 72b6cb3ca7..741210326c 100644 --- a/chrome/content/zotero/recognizePDF.js +++ b/chrome/content/zotero/recognizePDF.js @@ -363,7 +363,7 @@ Zotero_RecognizePDF.Recognizer.prototype._queryGoogle = function() { var me = this; if(this._DOI) { // use CrossRef to look for DOI - var translate = new Zotero.Translate("search"); + var translate = new Zotero.Translate.Search(); translate.setTranslator("11645bd1-0420-45c1-badb-53fb41eeb753"); var item = {"itemType":"journalArticle", "DOI":this._DOI}; translate.setSearch(item); @@ -411,7 +411,7 @@ Zotero_RecognizePDF.Recognizer.prototype._queryGoogle = function() { this._hiddenBrowser.docShell.allowImages = false; } - var translate = new Zotero.Translate("web"); + var translate = new Zotero.Translate.Web(); var savedItem = false; translate.setTranslator("57a00950-f0d1-4b41-b6ba-44ff0fc30289"); translate.setHandler("itemDone", function(translate, item) { @@ -459,12 +459,47 @@ Zotero_RecognizePDF.Recognizer.prototype._scrape = function(/**Zotero.Translate* this._callback(false, "recognizePDF.limit"); return; } - + this._hiddenBrowser.removeEventListener("pageshow", this._scrape.caller, true); translate.setDocument(this._hiddenBrowser.contentDocument); - translate.translate(this._libraryID, false); + + //closure? + (function(me) { + me._detectWeb(translate, function(translate, itemType) { + if(itemType) { + translate.translate(me._libraryID, false); + } else { + me._queryGoogle(); + } + }); + })(this); } +/** + * 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 From 0f684fee6e9d13e4d450d328ef48b46def7882ca Mon Sep 17 00:00:00 2001 From: aurimasv Date: Thu, 5 Apr 2012 02:44:33 -0500 Subject: [PATCH 2/6] 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 */ From 99a2ac7967777811e8c7c7f19f36e4ca92d34edd Mon Sep 17 00:00:00 2001 From: aurimasv Date: Fri, 6 Apr 2012 04:41:25 -0500 Subject: [PATCH 3/6] Use getTranslators to perform detectWeb with a set translator. --- chrome/content/zotero/recognizePDF.js | 2 +- .../zotero/xpcom/translation/translate.js | 41 +++++++++---------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/chrome/content/zotero/recognizePDF.js b/chrome/content/zotero/recognizePDF.js index 8cf3f81848..265d62358a 100644 --- a/chrome/content/zotero/recognizePDF.js +++ b/chrome/content/zotero/recognizePDF.js @@ -473,7 +473,7 @@ Zotero_RecognizePDF.Recognizer.prototype._scrape = function(/**Zotero.Translate* } }); - translate._detectWeb(); + translate.getTranslators(false, true); } /** diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js index 5b10f424f4..ffd0fc1c8b 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 or when _detectWeb is called and completes. + * complete * passed: an array of appropriate translators * returns: N/A * @param {Function} handler Callback function. All handlers will be passed the current @@ -889,15 +889,30 @@ Zotero.Translate.Base.prototype = { * * @param {Boolean} [getAllTranslators] Whether all applicable translators should be returned, * rather than just the first available. + * @param {Boolean} [checkSetTranslator] If true, the appropriate detect function is run on the + * set document/text/etc. using the translator set by setTranslator. + * getAllTranslators parameter is meaningless in this context. * @return {Zotero.Translator[]} An array of {@link Zotero.Translator} objects */ - "getTranslators":function(getAllTranslators) { + "getTranslators":function(getAllTranslators, checkSetTranslator) { // do not allow simultaneous instances of getTranslators if(this._currentState === "detect") throw new Error("getTranslators: detection is already running"); this._currentState = "detect"; - this._getAllTranslators = getAllTranslators; - this._getTranslatorsGetPotentialTranslators(); - + + if(checkSetTranslator) { + // setTranslator must be called beforehand if checkSetTranslator is set + if( !this.translator || !this.translator[0] ) { + throw new Error("getTranslators: translator must be set via setTranslator before calling" + + " getTranslators with the checkSetTranslator flag"); + } + var translator = Zotero.Translators.get(this.translator[0]); + if(!translator) throw new Error("getTranslators: could not retrieve translator '" + this.translator[0] + "'"); + this._getTranslatorsTranslatorsReceived([translator]); + } else { + this._getAllTranslators = getAllTranslators; + this._getTranslatorsGetPotentialTranslators(); + } + // if detection returns immediately, return found translators if(!this._currentState) return this._foundTranslators; }, @@ -1426,22 +1441,6 @@ 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 */ From f3bae4b00052a3cbf946ed2a9525e75c6e4ebb52 Mon Sep 17 00:00:00 2001 From: aurimasv Date: Fri, 6 Apr 2012 20:31:28 -0500 Subject: [PATCH 4/6] Allow running detect with multiple translators set. --- .../content/zotero/xpcom/translation/translate.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js index ffd0fc1c8b..c91bc7d3a2 100644 --- a/chrome/content/zotero/xpcom/translation/translate.js +++ b/chrome/content/zotero/xpcom/translation/translate.js @@ -898,6 +898,7 @@ Zotero.Translate.Base.prototype = { // do not allow simultaneous instances of getTranslators if(this._currentState === "detect") throw new Error("getTranslators: detection is already running"); this._currentState = "detect"; + this._getAllTranslators = getAllTranslators; if(checkSetTranslator) { // setTranslator must be called beforehand if checkSetTranslator is set @@ -905,11 +906,17 @@ Zotero.Translate.Base.prototype = { throw new Error("getTranslators: translator must be set via setTranslator before calling" + " getTranslators with the checkSetTranslator flag"); } - var translator = Zotero.Translators.get(this.translator[0]); - if(!translator) throw new Error("getTranslators: could not retrieve translator '" + this.translator[0] + "'"); - this._getTranslatorsTranslatorsReceived([translator]); + var translators = new Array(); + var t; + for(var i=0, n=this.translator.length; i Date: Sun, 8 Apr 2012 23:48:10 -0500 Subject: [PATCH 5/6] this.translator may contain Zotero.Translate objects or translatorID's --- chrome/content/zotero/xpcom/translation/translate.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js index c91bc7d3a2..ec4ed11ee0 100644 --- a/chrome/content/zotero/xpcom/translation/translate.js +++ b/chrome/content/zotero/xpcom/translation/translate.js @@ -909,10 +909,14 @@ Zotero.Translate.Base.prototype = { var translators = new Array(); var t; for(var i=0, n=this.translator.length; i Date: Sun, 8 Apr 2012 23:48:49 -0500 Subject: [PATCH 6/6] Moving setHandler "translator" next to other setHandler calls. --- chrome/content/zotero/recognizePDF.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/chrome/content/zotero/recognizePDF.js b/chrome/content/zotero/recognizePDF.js index 265d62358a..ae3df74941 100644 --- a/chrome/content/zotero/recognizePDF.js +++ b/chrome/content/zotero/recognizePDF.js @@ -425,6 +425,13 @@ Zotero_RecognizePDF.Recognizer.prototype._queryGoogle = function() { translate.setHandler("done", function(translate, success) { if(!success || !savedItem) me._queryGoogle(); }); + translate.setHandler("translators", function(translate, detected) { + if(detected.length) { + translate.translate(me._libraryID, false); + } else { + me._queryGoogle(); + } + }); this._hiddenBrowser.addEventListener("pageshow", function() { me._scrape(translate) }, true); @@ -463,16 +470,6 @@ Zotero_RecognizePDF.Recognizer.prototype._scrape = function(/**Zotero.Translate* this._hiddenBrowser.removeEventListener("pageshow", this._scrape.caller, true); translate.setDocument(this._hiddenBrowser.contentDocument); - var me = this; - - translate.setHandler("translators", function(translate, detected) { - if(detected.length) { - translate.translate(me._libraryID, false); - } else { - me._queryGoogle(); - } - }); - translate.getTranslators(false, true); }