利用CloudFlare Workers搭建T.Cn短網址入口
利用Mr.分享的CloudFlare Workers搭建T.Cn短網址入口代碼部署一個不需要服務器的T.Cn短網址入口程序。
官方網站:https://www.cloudflare.com
准備好CloudFlare賬號,沒有賬號就注冊一個,現在CloudFlare已經支持中文版本。
登陸:https://dash.cloudflare.com/login?lang=zh-cn
登陸之後點擊右邊的“Workers”
選擇您的Workers計劃
點擊“創建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> `); }
複制上面的代碼,刪除左側代碼框中的代碼,再粘貼己複制好的代碼至代碼框中。
點擊“保存並部署”,創建成功。
點擊“重命名”
修改子域名前綴,點擊“重命名並部署”。
自定義域名
域名管理面板,點擊“Workers”進入Workers管理界面,點擊“Add route”設置新的路由。Route可以輸入自己想使用的子域名,如果在根域名上使用直接把當前域名輸入即可。Route所使用的域名地址必須已經解析好A記錄,如果沒有能綁定的IP地址,可以輸入8.8.8.8占位。
備注:自定義域名容易出現問題,做好心裏准備。建議使用CloudFlare Workers的子域名。
演示:https://tcn.365t.workers.dev
未经允许不得转载:365t在線 » 利用CloudFlare Workers搭建T.Cn短網址入口
評論列表