通过cloudflare wordkers自建t.cn短网址

没啥教程,复制粘贴即可

原始版本

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>
  `);
}

美化版本

     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>

    <style media =“ screen”>
      正文{背景:#ECEFF1; 颜色:rgba(0,0,0,0.87); 字体家族:Roboto,Helvetica,Arial,sans-serif;边距:0; 填充:0; }
      #message {背景:白色;最大宽度:360px;边距:100px自动16px; 内边距:32px 24px; border-radius:3px; }
      #message h2 {color:rgba(0,0,0,0.6); font-weight:粗体; font-size:14px; 边距:0 0 8px; }
      #message h1 {color:#ffa100; font-size:30px; 字体粗细:300;边距:0 0 16px;}
      #message p {行高:140%;边距:16px 0 24px; font-size:14px; }
      #message a {display:block; 文本对齐:居中;背景:#039be5;文字修饰:无;白颜色; 内边距: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); 文本对齐:居中;font-size:13px; }
      @media(最大宽度:600像素){
        正文,#message {margin-top:0; 背景:白色;盒子阴影:无;}
        body {border-top:16px solid#ffa100; }
      }
    </ style>
  </ head>
  <身体>
$ {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(`
          <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>
          `);
        }
        返回render(`
        <div id =“ message” align =“ center”>
        <center> <h1>请求失败</ h1> </ center>
        </ div>
        `);
      }
      返回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>
      `);
    }
© 版权声明
THE END
喜欢就支持以下吧
点赞1
分享
1 2
评论 抢沙发