diff --git a/chrome/content/zotero/xpcom/connector/translator.js b/chrome/content/zotero/xpcom/connector/translator.js index 92d4837752..72684b93c3 100644 --- a/chrome/content/zotero/xpcom/connector/translator.js +++ b/chrome/content/zotero/xpcom/connector/translator.js @@ -295,10 +295,13 @@ Zotero.Translator = function(info) { } if(info.code) { - this.code = info.code; + this.code = preprocessCode(info.code); } } +/** + * Retrieves code for this translator + */ Zotero.Translator.prototype.getCode = function(callback) { if(this.code) { callback(true); @@ -309,7 +312,7 @@ Zotero.Translator.prototype.getCode = function(callback) { if(!code) { callback(false); } else { - me.code = code; + me.code = me.preprocessCode(code); callback(true); } } @@ -317,6 +320,20 @@ Zotero.Translator.prototype.getCode = function(callback) { } } +/** + * Preprocesses code for this translator + */ +Zotero.Translator.prototype.preprocessCode = function(code) { + if(!Zotero.isFx) { + const foreach = /^(\s*)for each\s*\((var )?([^ ]+) in (.*?)\)(\s*){/m; + code = code.replace(foreach, "$1var $3_zForEachSubject = $4; "+ + "for(var $3_zForEachIndex in $4_zForEachSubject)$5{ "+ + "$2$3 = $3_zForEachSubject[$3_zForEachIndex];", code); + Zotero.debug(code); + } + return code; +} + Zotero.Translator.prototype.__defineGetter__("displayOptions", function() { return Zotero.Utilities.deepCopy(this._displayOptions); }); diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js index 17e5ea6f52..73ef64b1db 100644 --- a/chrome/content/zotero/xpcom/translation/translate.js +++ b/chrome/content/zotero/xpcom/translation/translate.js @@ -744,8 +744,7 @@ Zotero.Translate.Base.prototype = { } } - // TODO maybe this should only be in the web translator - if(this._waitingForRPC) { + if(this._waitingForRPC && this instanceof Zotero.Translate.Web) { var me = this; Zotero.Connector.callMethod("detect", {"uri":this.location.toString(), "cookie":this.document.cookie, @@ -956,7 +955,8 @@ Zotero.Translate.Base.prototype = { Zotero.debug("Translate: Parsing code for "+translator.label, 4); try { - this._sandboxManager.eval("var translatorInfo = "+translator.code, this._sandbox); + this._sandboxManager.eval("var translatorInfo = "+translator.code, + ["detect"+this._entryFunctionSuffix, "do"+this._entryFunctionSuffix]); } catch(e) { if(translator.logError) { translator.logError(e.toString()); @@ -1030,7 +1030,10 @@ Zotero.Translate.Base.prototype = { * @param {Integer} level Log level (1-5, higher numbers are higher priority) */ "_debug":function(string, level) { - if(typeof string === "object") string = new XPCSafeJSObjectWrapper(string); + if(typeof string === "object" && Zotero.isFx36) { + string = new XPCSafeJSObjectWrapper(string); + } + if(level !== undefined && typeof level !== "number") { Zotero.debug("debug: level must be an integer"); return;