11---
2- import DefaultPageTitle from " @astrojs/starlight/components/PageTitle.astro" ;
32import HitsInfo from " @components/react/HitsInfo" ;
3+ import { Aside } from " @astrojs/starlight/components" ;
4+ import DefaultPageTitle from " @astrojs/starlight/components/PageTitle.astro" ;
45
56let tag = Astro .url .href ;
67if (! tag .endsWith (" /" ) && ! tag .endsWith (" .html" )) {
78 tag += " .html" ;
89}
910
10- const { contributors } = Astro .locals .starlightRoute .entry .data ;
11+ const { autoRedirect, contributors } = Astro .locals .starlightRoute .entry .data ;
1112const showContributors = Array .isArray (contributors ) && contributors .length > 0 ;
13+
14+ const clientVars = autoRedirect ?? { delay: 0 , href: " " };
1215---
1316
17+ {
18+ autoRedirect && (
19+ <div id = " auto-redirect" >
20+ <Aside type = " tip" >
21+ { " 页面将在 " }
22+ <span id = " auto-redirect-countdown" >{ autoRedirect .delay } </span >
23+ { " 秒后自动跳转到" }
24+ <a href = { autoRedirect .href } >{ autoRedirect .text } </a >
25+ { " ,您也可以手动" }
26+ <a href = " javascript:;" id = " auto-redirect-cancel" >
27+ { " 取消跳转" }
28+ </a >
29+ 。
30+ </Aside >
31+ </div >
32+ )
33+ }
34+
1435<DefaultPageTitle />
1536
1637<div class =" info" >
@@ -29,3 +50,25 @@ const showContributors = Array.isArray(contributors) && contributors.length > 0;
2950 client:only =" react"
3051 />
3152</div >
53+
54+ <script is:inline define:vars ={ clientVars } >
55+ const autoRedirect = document.getElementById("auto-redirect");
56+ if (!autoRedirect) return;
57+ const autoRedirectCountdown = document.getElementById("auto-redirect-countdown");
58+ if (autoRedirectCountdown && delay > 0) {
59+ let countdown = delay;
60+ const autoRedirectCancel = document.getElementById("auto-redirect-cancel");
61+ const interval = setInterval(() => {
62+ if (countdown > 0) {
63+ autoRedirectCountdown.textContent = String(--countdown);
64+ } else {
65+ clearInterval(interval);
66+ location.href = href;
67+ }
68+ }, 1000);
69+ autoRedirectCancel.addEventListener("click", () => {
70+ clearInterval(interval);
71+ autoRedirect.style.display = "none";
72+ });
73+ }
74+ </script >
0 commit comments