跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 【开源】在 CF Workers 搭建自己的 t.cn 短网址入口
未分類
19 4 月 2020

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

Mr. 大佬有话说 :

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

本帖最后由 Mr. 于 2020-4-19 18:04 编辑

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

演示地址:http://t.cn/A6wl4Dqs

食用方法:创建个 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(/$refers+: "(.+?)"/i);
    if (short && refer) {
      return render(`
      缩短结果:<a href="${short}">${short}</a><br />
      原始网址:<a href="${refer}">${refer}</a><br />
      <a href="/">返回</a>
      `);
    }
    return render(`请求失败`);
}
return render(`
    <form method="GET">
    <input name="url" placeholder="URL" />
    <button type="submit">压缩</button>
    </form>
`);
}

future 大佬有话说 :

支持大佬!

robot 大佬有话说 :

好东东。紫薯补丁

Reves 大佬有话说 :

感谢分享,一会儿试试

嗷嗷 大佬有话说 :

牛批牛批

tssz 大佬有话说 :

收藏

shenke 大佬有话说 :

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

Mr. 大佬有话说 :

shenke 大佬有话说 : 2020-4-19 18:12
https://blog.csdn.net/sculpta/article/details/104142531

你怕是理解错了

qytang 大佬有话说 :

牛逼!

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具