diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index 4626eeadb9..4db6548716 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -3039,9 +3039,18 @@ Zotero.Utilities.Internal.OpenURL = { ); if (co) { let base = Zotero.Prefs.get("openURL.resolver"); - // Add & if there's already a ? - let splice = base.indexOf("?") == -1 ? "?" : "&"; - return base + splice + co; + if (!base.endsWith('?')) { + if (base.includes('login?url=')) { + base += '?'; + } + else if (base.includes('?')) { + base += '&'; + } + else { + base += '?'; + } + } + return base + co; } return false; }, diff --git a/test/tests/utilities_internalTest.js b/test/tests/utilities_internalTest.js index 3f3c413c84..e4b4b0bc0a 100644 --- a/test/tests/utilities_internalTest.js +++ b/test/tests/utilities_internalTest.js @@ -738,4 +738,42 @@ describe("Zotero.Utilities.Internal", function () { assert.equal(html, 'yes1yes2 yes3'); }); }); + + describe("OpenURL", function () { + var item; + + before(async function () { + item = await createDataObject('item', { title: 'Foo Bar', date: '2024-12-19' }); + }) + + after(function () { + Zotero.Prefs.clear('openURL.resolver'); + }); + + describe("#resolve()", function () { + it("should add trailing '?' if no query string", async function () { + Zotero.Prefs.set("openURL.resolver", "https://resolver.ebsco.com/c/abcdef/result"); + var url = Zotero.Utilities.Internal.OpenURL.resolve(item); + assert.include(url, 'result?url_ver=Z39.88-2004'); + }); + + it("should add trailing '&' if already a query string", async function () { + Zotero.Prefs.set("openURL.resolver", "https://resolver.ebscohost.com/openurl?custid=abcdef&groupid=main&profile=ftf&authtype=ip,uid"); + var url = Zotero.Utilities.Internal.OpenURL.resolve(item); + assert.include(url, 'authtype=ip,uid&url_ver=Z39.88-2004'); + }); + + it("should add trailing '?' after /login?url=", async function () { + Zotero.Prefs.set("openURL.resolver", "https://proxy.school.edu/login?url=https://resolver.ebscohost.com/openurl"); + var url = Zotero.Utilities.Internal.OpenURL.resolve(item); + assert.include(url, 'openurl?url_ver=Z39.88-2004'); + }); + + it("shouldn't add trailing '?' after /login?url= if URL already ends in '?'", async function () { + Zotero.Prefs.set("openURL.resolver", "https://proxy.school.edu/login?url=https://resolver.ebscohost.com/openurl?"); + var url = Zotero.Utilities.Internal.OpenURL.resolve(item); + assert.include(url, 'openurl?url_ver=Z39.88-2004'); + }); + }); + }); });