From bd3ba77eb8dead3c79c8356c7bb77b9e1418a2f7 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Tue, 29 Jul 2025 16:02:09 -0700 Subject: [PATCH] Search automatically on URL query parameter Automatically start searching when ?q= parameter is given in the URL, selecting and visiting the first match if available. If not available, the query will be filled into the search box but we won't navigate away. --- lib/rdoc/generator/template/darkfish/js/darkfish.js | 10 ++++++++++ lib/rdoc/generator/template/darkfish/js/search.js | 12 +++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/rdoc/generator/template/darkfish/js/darkfish.js b/lib/rdoc/generator/template/darkfish/js/darkfish.js index 4c15efde66..15b5d706be 100644 --- a/lib/rdoc/generator/template/darkfish/js/darkfish.js +++ b/lib/rdoc/generator/template/darkfish/js/darkfish.js @@ -76,6 +76,16 @@ function hookSearch() { } search.scrollIntoView = search.scrollInWindow; + + // Check for ?q= URL parameter and trigger search automatically + if (typeof URLSearchParams !== 'undefined') { + var urlParams = new URLSearchParams(window.location.search); + var queryParam = urlParams.get('q'); + if (queryParam) { + input.value = queryParam; + search.search(queryParam, true); + } + } }; function hookFocus() { diff --git a/lib/rdoc/generator/template/darkfish/js/search.js b/lib/rdoc/generator/template/darkfish/js/search.js index d3cded1d57..68e1f77ff8 100644 --- a/lib/rdoc/generator/template/darkfish/js/search.js +++ b/lib/rdoc/generator/template/darkfish/js/search.js @@ -34,6 +34,8 @@ Search.prototype = Object.assign({}, Navigation, new function() { } this.search = function(value, selectFirstMatch) { + this.selectFirstMatch = selectFirstMatch; + value = value.trim().toLowerCase(); if (value) { this.setNavigationActive(true); @@ -76,7 +78,15 @@ Search.prototype = Object.assign({}, Navigation, new function() { //TODO: ECMAScript //if (jQuery.browser.msie) this.$element[0].className += ''; - if (isLast) this.result.setAttribute('aria-busy', 'false'); + if (this.selectFirstMatch && this.current) { + this.selectFirstMatch = false; + this.select(this.current); + } + + if (isLast) { + this.selectFirstMatch = false; + this.result.setAttribute('aria-busy', 'false'); + } } this.move = function(isDown) {