短網址搭建:利用 cloudflare wordkers 搭建自用短網址 t.cn 教程
阿新 • • 發佈:2021-01-13
直接複製貼上下面的程式碼就好了,沒什麼可說的。這裡再提供一份美化的版本,自行下載。(美化版)利用 cloudflare wordkers 搭建 t.cn 短網址.txt
addEventListener('fetch',(event)=> { 返回event.respondWith(handleRequest(event.request)); }) const handleRequest =非同步(請求)=> { const render =(body)=> { 返回新的Response(` <!doctype html> <html> <頭> <meta charset =“ utf-8” /> <meta name =“ viewport” content =“ width = device-width,initial-scale = 1” /> <title>t.cn短網址</ title> </ head> <body> $ {body} </ body> </ html>`.trim(),{ 狀態:200, 標頭:{ 'Content-Type':'text / html; charset = utf-8' } }); } request = new URL(request.url); if(request.pathname!=='/')返回新的Response(null,{status:404}); 如果(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 =等待response.text(); const short = html.match(/ http:\ / \ / t.cn \ / \ w + / i); const refer = html.match(/ \ $ refer \ s +:“(。+?)” / i); 如果(簡稱&&參考){ 返回render(` 縮短結果:<a href="${short[0]}"> $ {short [0]} </a> <br /> 原始網址:<a href="${refer[1]}"> $ {refer [1]} </a> <br /> <a href="/">返回</a> `); } 返回render(`請求失敗`); } 返回render(` <form method =“ GET”> <input name =“ url” placeholder =“ URL” /> <button type =“ submit”>壓縮</ button> </ form> `); }