设为首页收藏本站

简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย Français

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 608|回复: 54

【开源】在 CF Workers 搭建自己的 t.cn 短网址入口

[复制链接]

46

主题

533

回帖

1274

积分

金牌会员

积分
1274
发表于 2020-4-19 18:01:48 | 显示全部楼层 |阅读模式
本帖最后由 Mr. 于 2020-4-19 18:39 编辑


@ljm4216
大佬的帖子 https://www.52.ht/thread-676833-1-1.html 得到启发,
才知道原来 t.cn 的接口这么简单,于是撸了个基于 Cloudflare Workers 的在线版页面。

演示地址:https://tcn.sino.workers.dev/

食用方法:创建个 workers 把以下代码粘贴进去就可以了,快把今天撸的免费域名用起来吧!

[ol]
  • addEventListener('fetch', (event) => {
  •   return event.respondWith(handleRequest(event.request));
  • })
  • const handleRequest = async (request) => {
  •   const render = (body) => {
  •     return new Response(`
  •       
  •       
  •       
  •         
  •         
  •         T.CN 短网址
  •       
  •       ${body}
  •       `.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(`
  •         缩短结果:[url=]${short[0]}[/url]
  •         原始网址:[url=]${refer[1]}[/url]
  •         [url=]返回[/url]
  •       `);
  •     }
  •     return render(`请求失败`);
  •   }
  •   return render(`
  •    
  •    
  •     压缩
  •    
  •   `);
  • }[/ol]复制代码
  • 回复

    使用道具 举报

    0

    主题

    34

    回帖

    86

    积分

    注册会员

    积分
    86
    发表于 2020-4-20 09:11:56 | 显示全部楼层
    本帖最后由 xJogger 于 2020-4-20 09:23 编辑

    加了一点样式,看起来更舒服了。

    预览:







    代码:

    [ol]
  •      addEventListener('fetch', (event) => {
  •       return event.respondWith(handleRequest(event.request));
  •     })
  •     const handleRequest = async (request) => {
  •       const render = (body) => {
  •         return new Response(`
  •   
  •    
  •    
  •     T.CN 短网址
  •    
  •       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; }
  •       }
  •    
  •   
  •   
  • ${body}
  • `.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(`
  •          
  •          
  •             缩短结果:[url=]${short[0]}[/url]
  •             
  •             原始网址:[url=]${refer[1]}[/url]
  •             
  •             [url=]返回[/url]
  •             
  •           `);
  •         }
  •         return render(`
  •         
  •         请求失败
  •         
  •         `);
  •       }
  •       return render(`
  •       
  •       T.CN 短网址
  •    
  •         
  •         
  •         压缩
  •         
  •         
  •       `);
  •     }[/ol]复制代码
  • 回复

    使用道具 举报

    90

    主题

    2691

    回帖

    5764

    积分

    论坛元老

    积分
    5764
    发表于 2020-4-19 18:20:32 | 显示全部楼层
    本帖最后由 qytang 于 2020-4-19 18:22 编辑

    牛逼!不管是啥,到大佬们手里都能玩出花来
    回复

    使用道具 举报

    9

    主题

    481

    回帖

    1071

    积分

    金牌会员

    积分
    1071
    发表于 2020-4-19 18:04:52 | 显示全部楼层
    感谢分享,一会儿试试
    回复

    使用道具 举报

    11

    主题

    189

    回帖

    445

    积分

    中级会员

    积分
    445
    发表于 2020-4-19 18:02:15 | 显示全部楼层
    支持大佬!
    回复

    使用道具 举报

    142

    主题

    3133

    回帖

    6818

    积分

    论坛元老

    积分
    6818
    发表于 2020-4-19 18:04:07 | 显示全部楼层
    好东东。紫薯补丁
    回复

    使用道具 举报

    283

    主题

    1687

    回帖

    4301

    积分

    论坛元老

    积分
    4301
    发表于 2020-4-19 18:05:38 | 显示全部楼层
    牛批牛批
    回复

    使用道具 举报

    22

    主题

    764

    回帖

    1720

    积分

    金牌会员

    积分
    1720
    发表于 2020-4-19 18:07:40 | 显示全部楼层
    收藏
    回复

    使用道具 举报

    4

    主题

    74

    回帖

    200

    积分

    中级会员

    积分
    200
    发表于 2020-4-19 18:12:15 | 显示全部楼层

    shenke 发表于 2020-4-19 18:12

    https://blog.csdn.net/sculpta/article/details/104142531

    你怕是理解错了谢谢分享
    回复

    使用道具 举报

    46

    主题

    533

    回帖

    1274

    积分

    金牌会员

    积分
    1274
     楼主| 发表于 2020-4-19 18:12:53 | 显示全部楼层

    qytang 发表于 2020-4-19 18:20

    牛逼!不管是啥,到大佬们手里都能玩出花来

    不是啥新花样,就为了自用方便点
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|Discuz! X

    GMT+8, 2026-3-24 18:17 , Processed in 0.015968 second(s), 4 queries , Gzip On, Redis On.

    Powered by Discuz! X3.5

    © 2001-2025 Discuz! Team.

    快速回复 返回顶部 返回列表