via:http://t.cn/A6wlcbEq
创建个 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> </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(` 缩短结果:<a href="${short[0]}">${short[0]}</a><br /> 原始网址:<a href="${refer[1]}">${refer[1]}</a><br /> <a href="/">返回</a> `); } return render(`请求失败`); } return render(` <form method="GET"> <input name="url" placeholder="URL" /> <button type="submit">压缩</button> </form> `); }
刚好之前论坛里也发过,有注册免费的域名的商家活动,也不用浪费了。
可以参考workerd的各种玩法:
https://zsxwz.com/2020/03/26/%e5%88%a9%e7%94%a8worker%e6%90%ad%e5%bb%bagithub%e5%8a%a0%e9%80%9f%e4%b8%8b%e8%bd%bd%e6%9c%8d%e5%8a%a1%e5%8f%8a%e5%85%b6%e4%bb%96%e4%b8%80%e4%ba%9b%e6%9c%89%e8%b6%a3%e7%9a%84%e9%a1%b9%e7%9b%ae/