commit d3bc2b4046188506089e5c049b68886c2551b683
parent 6fa4f8d02b517d1296edc7229b0f35d886affaee
Author: Simon Kornblith <simon@simonster.com>
Date: Sun, 8 Apr 2012 21:51:29 -0700
Merge pull request #93 from aurimasv/recognizePDF
[PDF Metadata Retrieval] Call detectWeb before trying to call doWeb for Google Scholar
Diffstat:
2 files changed, 41 insertions(+), 7 deletions(-)
diff --git 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) {
@@ -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);
@@ -459,10 +466,11 @@ 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);
+
+ translate.getTranslators(false, true);
}
/**
diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js
@@ -892,15 +892,41 @@ 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 translators = new Array();
+ var t;
+ for(var i=0, n=this.translator.length; i<n; i++) {
+ if(typeof(this.translator[i]) == 'string') {
+ t = Zotero.Translators.get(this.translator[i]);
+ if(!t) Zotero.debug("getTranslators: could not retrieve translator '" + this.translator[i] + "'");
+ } else {
+ t = this.translator[i];
+ }
+ /**TODO: check that the translator is of appropriate type?*/
+ if(t) translators.push(t);
+ }
+ if(!translators.length) throw new Error("getTranslators: no valid translators were set.");
+ this._getTranslatorsTranslatorsReceived(translators);
+ } else {
+ this._getTranslatorsGetPotentialTranslators();
+ }
+
// if detection returns immediately, return found translators
if(!this._currentState) return this._foundTranslators;
},