From 5d17ff3f5324ef74a1937a113995c6bf202645ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Tue, 27 Sep 2016 17:43:56 +0300 Subject: [PATCH] Implements #1021 on Z4fx extension --- .../zotero/xpcom/connector/translator.js | 5 +- .../zotero/xpcom/translation/translate.js | 9 +- .../zotero/xpcom/translation/translator.js | 5 +- .../zotero/xpcom/translation/translators.js | 139 ++++++++++-------- test/content/support.js | 12 +- test/tests/translateTest.js | 2 +- test/tests/translatorsTest.js | 89 +++++++++++ 7 files changed, 189 insertions(+), 72 deletions(-) create mode 100644 test/tests/translatorsTest.js diff --git a/chrome/content/zotero/xpcom/connector/translator.js b/chrome/content/zotero/xpcom/connector/translator.js index c9cd682106..56acb305fb 100644 --- a/chrome/content/zotero/xpcom/connector/translator.js +++ b/chrome/content/zotero/xpcom/connector/translator.js @@ -425,7 +425,10 @@ Zotero.Translator.prototype.init = function(info) { if(this.translatorType & TRANSLATOR_TYPES["web"]) { // compile web regexp - this.webRegexp = this.target ? new RegExp(this.target, "i") : null; + this.webRegexp = { + root: this.target ? new RegExp(this.target, "i") : null, + all: this.targetAll ? new RegExp(this.targetAll, "i") : null + }; } else if(this.hasOwnProperty("webRegexp")) { delete this.webRegexp; } diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js index eb1be0c14d..0766677a51 100644 --- a/chrome/content/zotero/xpcom/translation/translate.js +++ b/chrome/content/zotero/xpcom/translation/translate.js @@ -1883,7 +1883,8 @@ Zotero.Translate.Web.prototype.Sandbox = Zotero.Translate.Sandbox._inheritFromBa */ Zotero.Translate.Web.prototype.setDocument = function(doc) { this.document = doc; - this.setLocation(doc.location.href); + this.rootDocument = doc.defaultView.top.document || doc; + this.setLocation(doc.location.href, this.rootDocument.location.href); } /** @@ -1900,9 +1901,11 @@ Zotero.Translate.Web.prototype.setCookieSandbox = function(cookieSandbox) { * Sets the location to operate upon * * @param {String} location The URL of the page to translate + * @param {String} rootLocation The URL of the root page, within which `location` is embedded */ -Zotero.Translate.Web.prototype.setLocation = function(location) { +Zotero.Translate.Web.prototype.setLocation = function(location, rootLocation) { this.location = location; + this.rootLocation = rootLocation || location; this.path = this.location; } @@ -1910,7 +1913,7 @@ Zotero.Translate.Web.prototype.setLocation = function(location) { * Get potential web translators */ Zotero.Translate.Web.prototype._getTranslatorsGetPotentialTranslators = function() { - return Zotero.Translators.getWebTranslatorsForLocation(this.location); + return Zotero.Translators.getWebTranslatorsForLocation(this.location, this.rootLocation); } /** diff --git a/chrome/content/zotero/xpcom/translation/translator.js b/chrome/content/zotero/xpcom/translation/translator.js index 5bddc14a06..1b6a754812 100644 --- a/chrome/content/zotero/xpcom/translation/translator.js +++ b/chrome/content/zotero/xpcom/translation/translator.js @@ -115,7 +115,10 @@ Zotero.Translator.prototype.init = function(info) { if(this.translatorType & TRANSLATOR_TYPES["web"]) { // compile web regexp this.cacheCode |= !this.target; - this.webRegexp = this.target ? new RegExp(this.target, "i") : null; + this.webRegexp = { + root: this.target ? new RegExp(this.target, "i") : null, + all: this.targetAll ? new RegExp(this.targetAll, "i") : null + }; } else if(this.hasOwnProperty("webRegexp")) { delete this.webRegexp; } diff --git a/chrome/content/zotero/xpcom/translation/translators.js b/chrome/content/zotero/xpcom/translation/translators.js index 04c94c9d2a..f063a20278 100644 --- a/chrome/content/zotero/xpcom/translation/translators.js +++ b/chrome/content/zotero/xpcom/translation/translators.js @@ -46,7 +46,7 @@ Zotero.Translators = new function() { Zotero.debug("Initializing translators"); var start = new Date; - _cache = {"import":[], "export":[], "web":[], "search":[]}; + _cache = {"import":[], "export":[], "web":[], "webWithTargetAll":[], "search":[]}; _translators = {}; var sql = "SELECT fileName, metadataJSON, lastModifiedTime FROM translatorCache"; @@ -152,6 +152,9 @@ Zotero.Translators = new function() { for (let type in TRANSLATOR_TYPES) { if (translator.translatorType & TRANSLATOR_TYPES[type]) { _cache[type].push(translator); + if ((translator.translatorType & TRANSLATOR_TYPES.web) && translator.targetAll) { + _cache.webWithTargetAll.push(translator); + } } } @@ -267,76 +270,92 @@ Zotero.Translators = new function() { /** * Gets web translators for a specific location * @param {String} uri The URI for which to look for translators + * @param {String} rootUri The root URI of the page, different from `uri` if running in an iframe */ - this.getWebTranslatorsForLocation = function(uri) { - return this.getAllForType("web").then(function(allTranslators) { + this.getWebTranslatorsForLocation = function(URI, rootURI) { + var isFrame = URI !== rootURI; + var type = isFrame ? "webWithTargetAll" : "web"; + + return this.getAllForType(type).then(function(allTranslators) { var potentialTranslators = []; + var translatorConverterFunctions = []; - var properHosts = []; - var proxyHosts = []; + var rootSearchURIs = this.getSearchURIs(rootURI); + var frameSearchURIs = isFrame ? this.getSearchURIs(URI) : rootSearchURIs; - var properURI = Zotero.Proxies.proxyToProper(uri); - var knownProxy = properURI !== uri; - if(knownProxy) { - // if we know this proxy, just use the proper URI for detection - var searchURIs = [properURI]; - } else { - var searchURIs = [uri]; - - // if there is a subdomain that is also a TLD, also test against URI with the domain - // dropped after the TLD - // (i.e., www.nature.com.mutex.gmu.edu => www.nature.com) - var m = /^(https?:\/\/)([^\/]+)/i.exec(uri); - if(m) { - // First, drop the 0- if it exists (this is an III invention) - var host = m[2]; - if(host.substr(0, 2) === "0-") host = host.substr(2); - var hostnames = host.split("."); - for(var i=1; i www.nature.com) + var m = /^(https?:\/\/)([^\/]+)/i.exec(URI); + if (m) { + // First, drop the 0- if it exists (this is an III invention) + var host = m[2]; + if(host.substr(0, 2) === "0-") host = host.substr(2); + var hostnames = host.split("."); + for (var i=1; i