From c0b6712923cc14799c113154cdfcc15c334724f5 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 11 Aug 2020 03:40:30 -0400 Subject: [PATCH] Utilities.Translate.resolveURL() tweaks - Use `new URL()`, available in all modern environments, instead of various other methods. In addition to being consistent and simple, this allows setting the base URL explicitly, regardless of the environment. - Default protocol-relative URLs to 'https' if no document location (though I'm not sure if that ever happens) --- .../zotero/xpcom/translation/translate.js | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js index 8b46c0f28a..71db9dfea8 100644 --- a/chrome/content/zotero/xpcom/translation/translate.js +++ b/chrome/content/zotero/xpcom/translation/translate.js @@ -1395,22 +1395,14 @@ Zotero.Translate.Base.prototype = { var m = url.match(hostPortRe), resolved; if (!m) { - // Convert relative URLs to absolute - if(Zotero.isFx && this.location) { - resolved = Components.classes["@mozilla.org/network/io-service;1"]. - getService(Components.interfaces.nsIIOService). - newURI(this.location, "", null).resolve(url); - } else if(Zotero.isNode && this.location) { - resolved = require('url').resolve(this.location, url); - } else if (this.document) { - var a = this.document.createElement('a'); - a.href = url; - resolved = a.href; - } else if (url.indexOf('//') == 0) { - // Protocol-relative URL with no associated web page - // Use HTTP by default - resolved = 'http:' + url; - } else { + if (this.location) { + resolved = new URL(url, this.location).toString(); + } + else if (url.startsWith('//')) { + // Use HTTPS by default for protocol-relative URL with no associated web page + resolved = 'https:' + url; + } + else { throw new Error('Cannot resolve relative URL without an associated web page: ' + url); } } else if (allowedSchemes.indexOf(m[1].toLowerCase()) == -1) {