今天借助AI的代码编写,在CloudFlare实现了一个简单的随机访问博客页面;
网址数据存储在CF的KV空间中,从前端随机调用并跳转,实现我收藏的博客的随机访问功能。

部署流程

1.在cf创建Worker,将index.js的代码,部署到创建的woker里

2.创建并绑定KV空间,KV 变量名为:blogurl

3.在KV空间里添加网址,密钥随意,值为网址

4.访问你的worker网址

演示站

https://goblog.cn.sv/

代码

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

const html = (urlCount) => `

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机访问博客 - 阿祖导航</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f2f2f2;
            font-family: Arial, sans-serif;
        }
        button {
            position: absolute;
            top: 30%;
            padding: 20px 40px;
            font-size: 20px;
            cursor: pointer;
            background-color: #4CAF50; /* Green */
            border: none;
            color: white;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            transition-duration: 0.4s;
            border-radius: 12px;
        }
        button:hover {
            background-color: white;
            color: black;
        }
        #urlCount {
            position: absolute;
            top: 45%;
            font-size: 14px;
            color: gray;
        }
    </style>
    <script>
        async function fetchAndRedirect() {
            const response = await fetch('/getRandomUrl', { method: 'POST' });
            const url = await response.text();
            window.location.href = url;
        }
    </script>
</head>
<body>
    <button onclick="fetchAndRedirect()">随机访问博客</button>
    <p id="urlCount">当前系统博客数量:${urlCount}</p>
</body>
</html>
`;

/**

* Respond to the request
* @param {Request} request
  */
  async function handleRequest(request) {
  if (request.method === 'POST' && request.url.endsWith('/getRandomUrl')) {
  const keys = await blogurl.list();
  const randomKey = keys.keys[Math.floor(Math.random() * keys.keys.length)];
  const url = await blogurl.get(randomKey.name);
  return new Response(url, { status: 200 });
  }
  
  const keys = await blogurl.list();
  return new Response(html(keys.keys.length), {
  headers: { 'content-type': 'text/html;charset=UTF-8' },
  });
  }

页面截图

随机访问博客的简单实现