fix search translation and get rid of unnecessary instanceof calls

This commit is contained in:
Simon Kornblith 2010-11-06 19:42:28 +00:00
parent 219cc49988
commit 379888eec4

View file

@ -199,16 +199,10 @@ Zotero.Translate.Sandbox = {
*/ */
"loadTranslator":function(translate, type) { "loadTranslator":function(translate, type) {
const setDefaultHandlers = function(translate, translation) { const setDefaultHandlers = function(translate, translation) {
var noHandlers = true; if(Zotero.Utilities.isEmpty(translation._handlers)) {
for(var i in translation._handlers) { if(type === "export") {
noHandlers = false;
break;
}
if(noHandlers) {
if(!(translation instanceof Zotero.Translate.Export)) {
translation.setHandler("itemDone", function(obj, item) { item.complete() }); translation.setHandler("itemDone", function(obj, item) { item.complete() });
} } else if(type === "web") {
if(translation instanceof Zotero.Translate.Web) {
translation.setHandler("selectItems", translate._handlers["selectItems"]); translation.setHandler("selectItems", translate._handlers["selectItems"]);
} }
} }
@ -302,7 +296,7 @@ Zotero.Translate.Sandbox = {
* @param {Zotero.Translate} translate * @param {Zotero.Translate} translate
*/ */
"wait":function(translate) { "wait":function(translate) {
if(translate._currentState == "translate" || translate instanceof Zotero.Translate.Web) { if(translate._currentState == "translate") {
translate._waitForCompletion = true; translate._waitForCompletion = true;
} else { } else {
throw "Translate: cannot call Zotero.wait() in detectCode of non-web translators"; throw "Translate: cannot call Zotero.wait() in detectCode of non-web translators";
@ -420,6 +414,14 @@ Zotero.Translate.Sandbox = {
// call super // call super
Zotero.Translate.Sandbox.Base._itemDone(translate, item); Zotero.Translate.Sandbox.Base._itemDone(translate, item);
},
/**
* Overloads {@link Zotero.Sandbox.Base.wait} to allow asynchronous detect
* @param {Zotero.Translate} translate
*/
"wait":function(translate) {
translate._waitForCompletion = true;
} }
}, },
@ -474,7 +476,7 @@ Zotero.Translate.Sandbox = {
* @borrows Zotero.Translate.Sandbox.Web._itemDone as this._itemDone * @borrows Zotero.Translate.Sandbox.Web._itemDone as this._itemDone
*/ */
"_itemDone":function(translate, item) { "_itemDone":function(translate, item) {
Zotero.Translate.Sandbox.Web.itemDone(translate, item); Zotero.Translate.Sandbox.Web._itemDone(translate, item);
} }
} }
} }
@ -677,7 +679,7 @@ Zotero.Translate.Base.prototype = {
// translate // translate
try { try {
this._sandboxManager.sandbox["do"+this._entryFunctionSuffix](this.document, this.location); this._sandboxManager.sandbox["do"+this._entryFunctionSuffix].apply(this.null, this._getParameters());
} catch(e) { } catch(e) {
if(this._parentTranslator) { if(this._parentTranslator) {
throw(e); throw(e);
@ -762,7 +764,7 @@ Zotero.Translate.Base.prototype = {
this._prepareDetection(); this._prepareDetection();
try { try {
var returnValue = this._sandboxManager.sandbox["detect"+this._entryFunctionSuffix](this.document, this.location); var returnValue = this._sandboxManager.sandbox["detect"+this._entryFunctionSuffix].apply(null, this._getParameters());
} catch(e) { } catch(e) {
this.complete(false, e); this.complete(false, e);
return; return;
@ -775,7 +777,11 @@ Zotero.Translate.Base.prototype = {
* Loads the translator into its sandbox * Loads the translator into its sandbox
*/ */
"_loadTranslator":function(translator) { "_loadTranslator":function(translator) {
if(!this._sandboxManager) this._generateSandbox(); var sandboxLocation = this._getSandboxLocation();
if(!this._sandboxLocation || sandboxLocation != this._sandboxLocation) {
this._sandboxLocation = sandboxLocation;
this._generateSandbox();
}
this._waitForCompletion = false; this._waitForCompletion = false;
Zotero.debug("Translate: Parsing code for "+translator.label, 4); Zotero.debug("Translate: Parsing code for "+translator.label, 4);
@ -801,27 +807,8 @@ Zotero.Translate.Base.prototype = {
* Generates a sandbox for scraping/scraper detection * Generates a sandbox for scraping/scraper detection
*/ */
"_generateSandbox":function() { "_generateSandbox":function() {
var sandboxLocation = "http://www.example.com/"; Zotero.debug("Translate: Binding sandbox to "+(typeof this._sandboxLocation == "object" ? this._sandboxLocation.document.location : this._sandboxLocation), 4);
this._sandboxManager = new Zotero.Translate.SandboxManager(this, this._sandboxLocation);
if(this instanceof Zotero.Translate.Web) {
// use real URL, not proxied version, to create sandbox
sandboxLocation = this.document.defaultView;
} else if(this instanceof Zotero.Translate.Search) {
// generate sandbox for search by extracting domain from translator target
if(this.translator && this.translator[0] && this.translator[0].target) {
// so that web translators work too
const searchSandboxRe = /^http:\/\/[\w.]+\//;
var tempURL = this.translator[0].target.replace(/\\/g, "").replace(/\^/g, "");
var m = searchSandboxRe.exec(tempURL);
if(m) sandboxLocation = m[0];
}
} else if(this._parentTranslator) {
sandboxLocation = this._parentTranslator._sandboxLocation;
}
Zotero.debug("Translate: Binding sandbox to "+(typeof sandboxLocation == "object" ? sandboxLocation.document.location : sandboxLocation), 4);
this._sandboxLocation = sandboxLocation;
this._sandboxManager = new Zotero.Translate.SandboxManager(this, sandboxLocation);
this._sandboxManager.eval("var Zotero = {};"+ this._sandboxManager.eval("var Zotero = {};"+
"Zotero.Item = function (itemType) {"+ "Zotero.Item = function (itemType) {"+
"this.itemType = itemType;"+ "this.itemType = itemType;"+
@ -882,6 +869,18 @@ Zotero.Translate.Base.prototype = {
return errorString.substr(1); return errorString.substr(1);
}, },
/**
* Determines the location where the sandbox should be bound
*/
"_getSandboxLocation":function() {
return (this._parentTranslator ? this._parentTranslator._sandboxLocation : "http://www.example.com/");
},
/**
* Gets parameters to be passed to detect* and do* functions
*/
"_getParameters":function() { return []; },
/** /**
* No-op for preparing detection * No-op for preparing detection
*/ */
@ -961,6 +960,18 @@ Zotero.Translate.Web.prototype._getPotentialTranslators = function() {
return potentialTranslators; return potentialTranslators;
} }
/**
* Bind sandbox to document being translated
*/
Zotero.Translate.Web.prototype._getSandboxLocation = function() {
return this.document.defaultView;
}
/**
* Pass document and location to detect* and do* functions
*/
Zotero.Translate.Web.prototype._getParameters = function() { return [this.document, this.location]; }
/** /**
* Prepare translation * Prepare translation
*/ */
@ -1232,7 +1243,7 @@ Zotero.Translate.Search.prototype.setCookieManager = Zotero.Translate.Web.protot
* @param {Object} item An item, with as many fields as desired, in the format returned by * @param {Object} item An item, with as many fields as desired, in the format returned by
* {@link Zotero.Item#serialize} * {@link Zotero.Item#serialize}
*/ */
Zotero.Translate.Search.prototype.setSearch = function(item) { Zotero.Translate.Search.prototype.setSearch = function(search) {
this.search = search; this.search = search;
} }
@ -1280,12 +1291,23 @@ Zotero.Translate.Search.prototype.complete = function(returnValue, error) {
} }
/** /**
* Loads the translator into its sandbox. We overload this to make sure that the sandbox is * Pass search item to detect* and do* functions
* bound to the correct URI.
*/ */
Zotero.Translate.Search.prototype._loadTranslator = function() { Zotero.Translate.Search.prototype._getParameters = function() { return [this.search]; };
this._generateSandbox();
Zotero.Translate.Base.prototype._loadTranslator.apply(this); /**
* Extract sandbox location from translator target
*/
Zotero.Translate.Search.prototype._getSandboxLocation = function() {
// generate sandbox for search by extracting domain from translator target
if(this.translator && this.translator[0] && this.translator[0].target) {
// so that web translators work too
const searchSandboxRe = /^http:\/\/[\w.]+\//;
var tempURL = this.translator[0].target.replace(/\\/g, "").replace(/\^/g, "");
var m = searchSandboxRe.exec(tempURL);
if(m) return m[0];
}
return Zotero.Translate.Base.prototype._getSandboxLocation.call(this);
} }
Zotero.Translate.Search.prototype._prepareTranslation = Zotero.Translate.Web.prototype._prepareTranslation; Zotero.Translate.Search.prototype._prepareTranslation = Zotero.Translate.Web.prototype._prepareTranslation;