From a17e8fa9982293789e09ed4f64f124a31b0fbf7f Mon Sep 17 00:00:00 2001 From: Bogdan Abaev Date: Fri, 27 Sep 2024 12:18:29 -0700 Subject: [PATCH] trim openUrl custom input, match against resolvers - trim openURL input as it is being updated - on input, load resolvers if needed - when resolvers are loaded, check if the input matches any of them. If so - update the dropdown with resolver's label --- .../zotero/preferences/preferences_general.js | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/chrome/content/zotero/preferences/preferences_general.js b/chrome/content/zotero/preferences/preferences_general.js index 1af48f465d..7392d8ecde 100644 --- a/chrome/content/zotero/preferences/preferences_general.js +++ b/chrome/content/zotero/preferences/preferences_general.js @@ -345,7 +345,7 @@ Zotero_Preferences.General = { } var openURLMenu = document.getElementById('openurl-menu'); let openURLMenuFirstItem = openURLMenu.menupopup.firstChild; - if (!this._openURLResolvers) { + if (!this._openURLResolvers?.length) { openURLMenuFirstItem.setAttribute('label', Zotero.getString('general.loading')); try { this._openURLResolvers = await Zotero.Utilities.Internal.OpenURL.getResolvers(); @@ -493,10 +493,33 @@ Zotero_Preferences.General = { }, onOpenURLCustomized: function () { - // Change resolver preference to "custom" - let firstItem = document.getElementById('openurl-menu').menupopup.firstChild; - firstItem.setAttribute('label', Zotero.getString('general.custom')); - firstItem.setAttribute('value', 'custom'); + let opeUrlInput = document.getElementById("openURLServerField"); + let openUrlMenu = document.getElementById('openurl-menu').menupopup.firstChild; + // Remove trailing whitespaces + opeUrlInput.value = opeUrlInput.value.trim(); + // If resolvers have been fetched, see if there is one that matches the input + if (this._openURLResolvers?.length) { + let inputMatchedResolved = this._openURLResolvers.find(resolver => resolver.url == opeUrlInput.value); + if (inputMatchedResolved) { + // User typed the url of an existing resolver, so update the name and dropdown label + Zotero.Prefs.set('openURL.name', inputMatchedResolved.name); + openUrlMenu.setAttribute('label', inputMatchedResolved.name); + return; + } + } + // If the resolvers have not been loaded, fetch them + else if (!this._openURLResolvers?.promise) { + // Save the promise so we don't request resolvers multiple times as the user types + this._openURLResolvers = Zotero.Promise.defer(); + Zotero.Utilities.Internal.OpenURL.getResolvers().then((res) => { + this._openURLResolvers = res; + // Rerun so that if the user pastes a url, we still check if it matches loaded resolvers above + this.onOpenURLCustomized(); + }); + } + // Change resolver preference to "custom" if we did not find a matching resolver above + openUrlMenu.setAttribute('label', Zotero.getString('general.custom')); + openUrlMenu.setAttribute('value', 'custom'); Zotero.Prefs.clear('openURL.name'); },