全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
楼主: Mr.

【开源】在 CF Workers 搭建自己的 t.cn 短网址入口

  [复制链接]
发表于 2020-4-19 23:45:19 | 显示全部楼层
优秀,哈哈,终于有自己的啦
发表于 2020-4-20 02:27:02 来自手机 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2020-4-20 09:11:56 | 显示全部楼层
本帖最后由 xJogger 于 2020-4-20 09:23 编辑

加了一点样式,看起来更舒服了。

预览:




代码:

  1.      addEventListener('fetch', (event) => {
  2.       return event.respondWith(handleRequest(event.request));
  3.     })

  4.     const handleRequest = async (request) => {
  5.       const render = (body) => {
  6.         return new Response(`
  7. <!DOCTYPE html>
  8. <html>
  9.   <head>
  10.     <meta charset="utf-8">
  11.     <meta name="viewport" content="width=device-width, initial-scale=1">
  12.     <title>T.CN 短网址</title>

  13.     <style media="screen">
  14.       body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
  15.       #message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px; border-radius: 3px; }
  16.       #message h2 {  color: rgba(0,0,0,0.6); font-weight: bold; font-size: 14px; margin: 0 0 8px; }
  17.       #message h1 { color: #ffa100;  font-size: 30px; font-weight: 300;  margin: 0 0 16px;}
  18.       #message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
  19.       #message a { display: block; text-align: center; background: #039be5; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
  20.       #message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
  21.       #load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
  22.       @media (max-width: 600px) {
  23.         body, #message { margin-top: 0; background: white; box-shadow: none; }
  24.         body { border-top: 16px solid #ffa100; }
  25.       }
  26.     </style>
  27.   </head>
  28.   <body>
  29. ${body}

  30. </body>
  31. </html>`.trim(), {
  32.           status: 200,
  33.           headers: {
  34.             'Content-Type': 'text/html; charset=utf-8'
  35.           }
  36.         });
  37.       }
  38.       request = new URL(request.url);
  39.       if (request.pathname !== '/') return new Response(null, { status: 404 });
  40.       if (request.searchParams.has('url')) {
  41.         const url = request.searchParams.get('url');
  42.         const response = await fetch(`http://service.weibo.com/share/share.php?url=${encodeURIComponent(url)}&title=1`);
  43.         const html = await response.text();
  44.         const short = html.match(/http:\/\/t.cn\/\w+/i);
  45.         const refer = html.match(/\$refer\s+: "(.+?)"/i);
  46.         if (short && refer) {
  47.           return render(`
  48.           <div id="message" align="center">
  49.          
  50.             <center><h1>缩短结果:</h1><a href="${short[0]}">${short[0]}</a></center>
  51.             <p></p>
  52.             <center><h1>原始网址:</h1><a href="${refer[1]}">${refer[1]}</a></center>
  53.             <p></p>
  54.             <a href="/">返回</a>
  55.             </div>
  56.           `);
  57.         }
  58.         return render(`
  59.         <div id="message" align="center">
  60.         <center><h1>请求失败</h1></center>
  61.         </div>
  62.         `);
  63.       }
  64.       return render(`
  65.       <div id="message" align="center">
  66.       <center><h1>T.CN 短网址</h1></center>
  67.    <p></p>
  68.         <form method="GET">
  69.         <input name="url" placeholder="URL" />
  70.         <button type="submit">压缩</button>
  71.         </form>
  72.         </div>
  73.       `);
  74.     }
复制代码
发表于 2020-4-20 17:48:14 | 显示全部楼层
可以自定义网址的notion.clost.net
发表于 2020-4-20 20:08:47 | 显示全部楼层
优秀学习了!
发表于 2020-4-21 13:29:30 | 显示全部楼层
t.cn 短网址api接口怎么制作?
发表于 2020-4-21 13:35:58 | 显示全部楼层
哈  都是鬼才   
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-30 00:26 , Processed in 0.080611 second(s), 7 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表