Merge pull request #93 from aurimasv/recognizePDF

[PDF Metadata Retrieval] Call detectWeb before trying to call doWeb for Google Scholar
This commit is contained in:
Simon Kornblith 2012-04-08 21:51:29 -07:00
commit d3bc2b4046
2 changed files with 41 additions and 7 deletions

View file

@ -363,7 +363,7 @@ Zotero_RecognizePDF.Recognizer.prototype._queryGoogle = function() {
var me = this; var me = this;
if(this._DOI) { if(this._DOI) {
// use CrossRef to look for 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"); translate.setTranslator("11645bd1-0420-45c1-badb-53fb41eeb753");
var item = {"itemType":"journalArticle", "DOI":this._DOI}; var item = {"itemType":"journalArticle", "DOI":this._DOI};
translate.setSearch(item); translate.setSearch(item);
@ -411,7 +411,7 @@ Zotero_RecognizePDF.Recognizer.prototype._queryGoogle = function() {
this._hiddenBrowser.docShell.allowImages = false; this._hiddenBrowser.docShell.allowImages = false;
} }
var translate = new Zotero.Translate("web"); var translate = new Zotero.Translate.Web();
var savedItem = false; var savedItem = false;
translate.setTranslator("57a00950-f0d1-4b41-b6ba-44ff0fc30289"); translate.setTranslator("57a00950-f0d1-4b41-b6ba-44ff0fc30289");
translate.setHandler("itemDone", function(translate, item) { translate.setHandler("itemDone", function(translate, item) {
@ -425,6 +425,13 @@ Zotero_RecognizePDF.Recognizer.prototype._queryGoogle = function() {
translate.setHandler("done", function(translate, success) { translate.setHandler("done", function(translate, success) {
if(!success || !savedItem) me._queryGoogle(); 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); 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"); this._callback(false, "recognizePDF.limit");
return; return;
} }
this._hiddenBrowser.removeEventListener("pageshow", this._scrape.caller, true); this._hiddenBrowser.removeEventListener("pageshow", this._scrape.caller, true);
translate.setDocument(this._hiddenBrowser.contentDocument); translate.setDocument(this._hiddenBrowser.contentDocument);
translate.translate(this._libraryID, false);
translate.getTranslators(false, true);
} }
/** /**

View file

@ -892,15 +892,41 @@ Zotero.Translate.Base.prototype = {
* *
* @param {Boolean} [getAllTranslators] Whether all applicable translators should be returned, * @param {Boolean} [getAllTranslators] Whether all applicable translators should be returned,
* rather than just the first available. * 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 * @return {Zotero.Translator[]} An array of {@link Zotero.Translator} objects
*/ */
"getTranslators":function(getAllTranslators) { "getTranslators":function(getAllTranslators, checkSetTranslator) {
// do not allow simultaneous instances of getTranslators // do not allow simultaneous instances of getTranslators
if(this._currentState === "detect") throw new Error("getTranslators: detection is already running"); if(this._currentState === "detect") throw new Error("getTranslators: detection is already running");
this._currentState = "detect"; this._currentState = "detect";
this._getAllTranslators = getAllTranslators; 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 detection returns immediately, return found translators
if(!this._currentState) return this._foundTranslators; if(!this._currentState) return this._foundTranslators;
}, },