使用Cloudflare Workers搭建 t.cn 短网址网站
从 @ljm4216 大佬的帖子hostloc.com/thread-676833-1-1.html 得到启发,
才知道原来 t.cn 的接口这么简单,于是撸了个基于 Cloudflare Workers 的在线版页面。
演示地址:https://tcn.sino.workers.dev/
使用方法:
创建个 workers 把以下代码粘贴进去就可以了,快把今天撸的免费域名用起来吧!
addEventListener('fetch', (event) => { return event.respondWith(handleRequest(event.request)); }) const handleRequest = async (request) => { const render = (body) => { return new Response(` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>T.CN 短网址</title> <style media="screen"> body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; } #message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px; border-radius: 3px; } #message h2 { color: rgba(0,0,0,0.6); font-weight: bold; font-size: 14px; margin: 0 0 8px; } #message h1 { color: #ffa100; font-size: 30px; font-weight: 300; margin: 0 0 16px;} #message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; } #message a { display: block; text-align: center; background: #039be5; text-decoration: none; color: white; padding: 16px; border-radius: 4px; } #message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); } #load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; } @media (max-width: 600px) { body, #message { margin-top: 0; background: white; box-shadow: none; } body { border-top: 16px solid #ffa100; } } </style> </head> <body> ${body} </body> </html>`.trim(), { status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' } }); } request = new URL(request.url); if (request.pathname !== '/') return new Response(null, { status: 404 }); if (request.searchParams.has('url')) { const url = request.searchParams.get('url'); const response = await fetch(`http://service.weibo.com/share/share.php?url=${encodeURIComponent(url)}&title=1`); const html = await response.text(); const short = html.match(/http:\/\/t.cn\/\w+/i); const refer = html.match(/\$refer\s+: "(.+?)"/i); if (short && refer) { return render(` <div id="message" align="center"> <center><h1>缩短结果:</h1><a href="${short[0]}">${short[0]}</a></center> <p></p> <center><h1>原始网址:</h1><a href="${refer[1]}">${refer[1]}</a></center> <p></p> <a href="/">返回</a> </div> `); } return render(` <div id="message" align="center"> <center><h1>请求失败</h1></center> </div> `); } return render(` <div id="message" align="center"> <center><h1>T.CN 短网址</h1></center> <p></p> <form method="GET"> <input name="url" placeholder="URL" /> <button type="submit">压缩</button> </form> </div> `); }
相关推荐:
使用CloudFlare Workers搭建Google Drive在线下载网站 – GoIndex
Cloudflare Worker版网址导航程序 – 无需域名主机,免费搭建
GoogleDrive(含转码) + lanzou + 19 直链工具 CloudFlare worker 版
作者:xJogger
查看原帖:hostloc.com/thread-677080-1-1.html
暮城
2020年8月4日 上午11:02
大佬,演示站挂了
MJJ
2020年9月2日 下午7:28
可以自己搭建一个啊,不过现在t.cn接口应该不能用了