Move _detectWeb to Zotero.Translate.Web.prototype

This commit is contained in:
aurimasv 2012-04-05 02:44:33 -05:00
parent cb6ae9cec7
commit 0f684fee6e
2 changed files with 28 additions and 36 deletions

View file

@ -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

View file

@ -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
*/