Commit 6f34f79

HPCesia <me@hpcesia.com>
2025-01-30 06:13:10
feat: random post
use rss.xml to get random post link.
1 parent ee2b66a
Changed files (1)
src/config.ts
@@ -56,7 +56,32 @@ export const navbarConfig: NavbarConfig = {
       {
         icon: 'material-symbols:casino',
         text: I18nKey.randomPost,
-        onclick: '',
+        onclick: {
+          id: 'random-post-btn',
+          function: async () => {
+            // Use rss.xml to get random post
+            // 使用 rss.xml 获取随机文章
+            const site = import.meta.env.SITE;
+            try {
+              const response = await fetch('/rss.xml');
+              const text = await response.text();
+              const parser = new DOMParser();
+              const xml = parser.parseFromString(text, 'text/xml');
+              const items = xml.querySelectorAll('item link');
+              const links = Array.from(items).map((item) => item.textContent || '');
+
+              if (links.length > 0) {
+                const randomLink = links[Math.floor(Math.random() * links.length)].replace(
+                  site,
+                  '/'
+                );
+                window.location.href = randomLink;
+              }
+            } catch (error) {
+              console.error('Failed to get random post:', error);
+            }
+          },
+        },
       },
       {
         icon: 'material-symbols:search-rounded',