Make Zotero.Translator shareable with connector

This commit is contained in:
Adomas Venčkauskas 2016-12-28 17:25:38 +02:00
parent eeee937310
commit b01487dccc

View file

@ -36,8 +36,6 @@ var TRANSLATOR_OPTIONAL_PROPERTIES = ["targetAll", "browserSupport", "minVersion
// Properties that are passed from background to inject page in connector
var TRANSLATOR_PASSING_PROPERTIES = TRANSLATOR_REQUIRED_PROPERTIES.
concat(["targetAll", "browserSupport", "code", "runMode", "itemType"]);
// Properties that are saved in connector if set but not required
var TRANSLATOR_SAVE_PROPERTIES = TRANSLATOR_REQUIRED_PROPERTIES.concat(["browserSupport"]);
/**
* @class Represents an individual translator
@ -77,8 +75,7 @@ Zotero.Translator = function(info) {
*/
Zotero.Translator.prototype.init = function(info) {
// make sure we have all the properties
for(var i=0; i<TRANSLATOR_REQUIRED_PROPERTIES.length; i++) {
var property = TRANSLATOR_REQUIRED_PROPERTIES[i];
for (let property of TRANSLATOR_REQUIRED_PROPERTIES) {
if (info[property] === undefined) {
this.logError(new Error('Missing property "'+property+'" in translator metadata JSON object in ' + info.label));
break;
@ -86,8 +83,7 @@ Zotero.Translator.prototype.init = function(info) {
this[property] = info[property];
}
}
for(var i=0; i<TRANSLATOR_OPTIONAL_PROPERTIES.length; i++) {
var property = TRANSLATOR_OPTIONAL_PROPERTIES[i];
for (let property of TRANSLATOR_OPTIONAL_PROPERTIES) {
if(info[property] !== undefined) {
this[property] = info[property];
}
@ -95,11 +91,9 @@ Zotero.Translator.prototype.init = function(info) {
this.browserSupport = info["browserSupport"] ? info["browserSupport"] : "g";
var supported = (
Zotero.isBookmarklet ?
(this.browserSupport.indexOf(Zotero.browser) !== -1 && this.browserSupport.indexOf("b") !== -1) ||
/(?:^|; ?)bookmarklet-debug-mode=1(?:$|; ?)/.test(document.cookie) :
this.browserSupport.indexOf(Zotero.browser) !== -1);
var supported = this.browserSupport.indexOf(Zotero.browser) !== -1 &&
(!Zotero.isBookmarklet || this.browserSupport.indexOf("b") !== -1 ||
/(?:^|; ?)bookmarklet-debug-mode=1(?:$|; ?)/.test(document.cookie));
if (supported) {
this.runMode = Zotero.Translator.RUN_MODE_IN_BROWSER;
@ -131,7 +125,7 @@ Zotero.Translator.prototype.init = function(info) {
this.fileName = OS.Path.basename(info.path);
}
if (info.code && this.cacheCode) {
this.code = info.code;
this.code = Zotero.Translator.replaceDeprecatedStatements(info.code);
} else if (this.hasOwnProperty("code")) {
delete this.code;
}
@ -209,6 +203,17 @@ Zotero.Translator.prototype.logError = function(message, type, line, lineNumber,
}
}
/**
* Replace deprecated ES5 statements
*/
Zotero.Translator.replaceDeprecatedStatements = function(code) {
const foreach = /^(\s*)for each\s*\((var )?([^ ]+) in (.*?)\)(\s*){/gm;
code = code.replace(foreach, "$1var $3_zForEachSubject = $4; "+
"for(var $3_zForEachIndex in $3_zForEachSubject)$5{ "+
"$2$3 = $3_zForEachSubject[$3_zForEachIndex];");
return code;
}
Zotero.Translator.RUN_MODE_IN_BROWSER = 1;
Zotero.Translator.RUN_MODE_ZOTERO_STANDALONE = 2;
Zotero.Translator.RUN_MODE_ZOTERO_SERVER = 4;