From 35f6dd8660d49dc591e51241fc41808078ad4f66 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 21 Mar 2026 12:26:26 -0400 Subject: [PATCH 1/2] fix: search error fixes --- doxyconfig-readthedocs-search.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/doxyconfig-readthedocs-search.js b/doxyconfig-readthedocs-search.js index 7d7f4d4..f1c2c7e 100644 --- a/doxyconfig-readthedocs-search.js +++ b/doxyconfig-readthedocs-search.js @@ -22,19 +22,23 @@ class ReadtheDocsSearch { } static init() { - const originalSearchBox = globalThis.SearchBox; + const realSearchBox = globalThis.SearchBox; globalThis.SearchBox = function(name, resultsPath, extension) { - originalSearchBox.call(this, name, resultsPath, extension); + if (realSearchBox) { + realSearchBox.call(this, name, resultsPath, extension); + } this.OnSearchFieldFocus = function() {}; - const originalCloseResultsWindow = this.CloseResultsWindow.bind(this); + const originalClose = this.CloseResultsWindow; this.CloseResultsWindow = function() { - const field = this.DOMSearchField(); + if (originalClose) { + originalClose.call(this); + } + const field = this.DOMSearchField ? this.DOMSearchField() : null; if (field?.id === document.activeElement?.id) { return; } - originalCloseResultsWindow(); }; }; @@ -47,7 +51,14 @@ class ReadtheDocsSearch { field.focus(); } }); - observer.observe(document.body, { childList: true, subtree: true }); + + if (document.body) { + observer.observe(document.body, { childList: true, subtree: true }); + } else { + document.addEventListener('DOMContentLoaded', function() { + observer.observe(document.body, { childList: true, subtree: true }); + }); + } document.addEventListener('focusin', function(e) { if (e.target?.id === 'MSearchField') { From f48d9b6696181b1f92cb62f3f589eda63a53c715 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 21 Mar 2026 12:42:46 -0400 Subject: [PATCH 2/2] Resolve ReadTheDocs default version before search When a numeric (PR) version is detected, the code now fetches the project's default ReadTheDocs version and waits for it before building the search URL. getReadTheDocsDefaultVersion was refactored to return a Promise that resolves to data.default_version or 'latest' on error, and the search routine uses the resolved version when calling fetchResults. This fixes incorrect searches for pull-request builds and provides a safe fallback. --- doxyconfig-readthedocs-search.js | 51 +++++++++++++++++--------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/doxyconfig-readthedocs-search.js b/doxyconfig-readthedocs-search.js index f1c2c7e..75df7bb 100644 --- a/doxyconfig-readthedocs-search.js +++ b/doxyconfig-readthedocs-search.js @@ -105,20 +105,20 @@ class ReadtheDocsSearch { let projectSlug = ReadtheDocsSearch.getMetaValue("readthedocs-project-slug") || "doxyconfig"; let projectVersion = ReadtheDocsSearch.getMetaValue("readthedocs-version-slug") || "latest"; - // pull requests are not indexed, so use the default version - if (/^\d+$/.test(projectVersion)) { - console.log('Pull request detected, getting default version from ReadTheDocs API'); - ReadtheDocsSearch.getReadTheDocsDefaultVersion(projectSlug); - } - - let url = `${ReadtheDocsSearch.serverUrl}search/?q=project:${projectSlug}/${projectVersion}+${query}&page=${page + 1}&page_size=${count}`; - console.log(url); - const ctx = { query, resultSummary, resultList, titleInterval, pageTitle, originalTitle, firstUrl: true }; - ReadtheDocsSearch.fetchResults(url, ctx); + // pull requests are not indexed, so use the default version + const versionReady = /^\d+$/.test(projectVersion) + ? ReadtheDocsSearch.getReadTheDocsDefaultVersion(projectSlug) + : Promise.resolve(projectVersion); + + versionReady.then(function(resolvedVersion) { + const url = `${ReadtheDocsSearch.serverUrl}search/?q=project:${projectSlug}/${resolvedVersion}+${query}&page=${page + 1}&page_size=${count}`; + console.log(url); + ReadtheDocsSearch.fetchResults(url, ctx); + }); } } @@ -203,19 +203,22 @@ class ReadtheDocsSearch { } static getReadTheDocsDefaultVersion(project) { - let url = `${ReadtheDocsSearch.serverUrl}projects/${project}/`; - $.ajax({ - url: url, - dataType: 'json', - success: function(data) { - console.log(data); - return data.default_version; - }, - error: function(jqXHR, textStatus, errorThrown) { - console.error('Error:', textStatus, errorThrown); - console.log(`Cannot determine default version for ${project}, assuming "latest"`); - return "latest"; - } - }) + console.log('Pull request detected, getting default version from ReadTheDocs API'); + const url = `${ReadtheDocsSearch.serverUrl}projects/${project}/`; + return new Promise(function(resolve) { + $.ajax({ + url: url, + dataType: 'json', + success: function(data) { + console.log(data); + resolve(data.default_version || 'latest'); + }, + error: function(jqXHR, textStatus, errorThrown) { + console.error('Error:', textStatus, errorThrown); + console.log(`Cannot determine default version for ${project}, assuming "latest"`); + resolve('latest'); + } + }); + }); } }