Skip to content

Commit fa9f44c

Browse files
authored
Autofill Cmd+K on 404 pages (cloudflare#24754)
1 parent a204f12 commit fa9f44c

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

src/components/404.astro

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,37 @@
1010

1111
<script>
1212
import { track } from "~/util/zaraz";
13+
import { openGlobalSearch } from "~/util/search";
1314

1415
const { pathname } = window.location;
1516

1617
const anchor = document.getElementById("404-search-link");
18+
const pretty = decodeURIComponent(
19+
pathname.replaceAll("/", " ").replaceAll("-", " ").trim(),
20+
);
1721

1822
if (anchor) {
19-
const pretty = pathname.replaceAll("/", " ").replaceAll("-", " ").trim();
20-
2123
anchor.setAttribute("href", `/search/?q=${encodeURIComponent(pretty)}`);
2224
anchor.addEventListener("click", () => {
2325
track("serp from location", { value: "404", query: pretty });
2426
});
2527
}
28+
29+
document.addEventListener(
30+
"keydown",
31+
(keyboardEvent) => {
32+
if (
33+
(keyboardEvent.metaKey || keyboardEvent.ctrlKey) &&
34+
keyboardEvent.key === "k"
35+
) {
36+
keyboardEvent.preventDefault();
37+
keyboardEvent.stopPropagation();
38+
39+
openGlobalSearch(pretty);
40+
}
41+
},
42+
{
43+
capture: true,
44+
},
45+
);
2646
</script>

src/components/overrides/Sidebar.astro

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
3838
<Default><slot /></Default>
3939

4040
<script>
41-
import { track } from "~/util/zaraz"
41+
import { track } from "~/util/zaraz";
42+
import { openGlobalSearch } from "~/util/search";
43+
4244
function initSidebarSearch() {
4345
const searchInput = document.getElementById('sidebar-search') as HTMLInputElement;
4446
const noResultsMessage = document.getElementById('sidebar-no-results') as HTMLElement;
@@ -231,29 +233,7 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
231233
globalSearchLink.addEventListener('click', () => {
232234
const currentQuery = searchInput.value.trim();
233235
if (currentQuery) {
234-
// Try multiple selectors for DocSearch
235-
const docSearchButton = document.querySelector('#docsearch button') as HTMLButtonElement ||
236-
document.querySelector('.DocSearch-Button') as HTMLButtonElement ||
237-
document.querySelector('[data-docsearch-button]') as HTMLButtonElement;
238-
239-
if (docSearchButton) {
240-
// Click the DocSearch button to open the modal
241-
docSearchButton.click();
242-
243-
// Wait for modal to open and set the search term
244-
setTimeout(() => {
245-
const searchInput = document.querySelector('.DocSearch-Input') as HTMLInputElement ||
246-
document.querySelector('#docsearch-input') as HTMLInputElement ||
247-
document.querySelector('[data-docsearch-input]') as HTMLInputElement;
248-
249-
if (searchInput) {
250-
searchInput.value = currentQuery;
251-
searchInput.focus();
252-
// Trigger search
253-
searchInput.dispatchEvent(new Event('input', { bubbles: true }));
254-
}
255-
}, 100);
256-
}
236+
openGlobalSearch(currentQuery);
257237
}
258238
});
259239

src/util/search.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const openGlobalSearch = (searchTerm?: string) => {
2+
// Try multiple selectors for DocSearch
3+
const docSearchButton =
4+
(document.querySelector("#docsearch button") as HTMLButtonElement) ||
5+
(document.querySelector(".DocSearch-Button") as HTMLButtonElement) ||
6+
(document.querySelector("[data-docsearch-button]") as HTMLButtonElement);
7+
8+
if (docSearchButton) {
9+
// Click the DocSearch button to open the modal
10+
docSearchButton.click();
11+
12+
if (searchTerm) {
13+
// Wait for modal to open and set the search term
14+
setTimeout(() => {
15+
const searchInput =
16+
(document.querySelector(".DocSearch-Input") as HTMLInputElement) ||
17+
(document.querySelector("#docsearch-input") as HTMLInputElement) ||
18+
(document.querySelector(
19+
"[data-docsearch-input]",
20+
) as HTMLInputElement);
21+
22+
if (searchInput) {
23+
searchInput.value = searchTerm;
24+
searchInput.focus();
25+
// Trigger search
26+
searchInput.dispatchEvent(new Event("input", { bubbles: true }));
27+
}
28+
}, 100);
29+
}
30+
}
31+
};

0 commit comments

Comments
 (0)