无服务器 自建短链服务 Url-Shorten-Worker 支持自定义短链

源码GitHub: 

https://github.com/crazypeace/Url-Shorten-Worker

搭建方法:

https://zelikk.blogspot.com/2022/07/url-shorten-worker-hide-tutorial.html


支持自定义短链的修改思路:

gh-pages分支下

index.html增加了一个可以输入自定义短链的文本框

<input type="text" class="form-customs-shorturl" placeholder="customs shorturl" id="customShortURL">

main.js准备给POST的JSON里面添加这个短链的文本

body: JSON.stringify({ url: document.querySelector("#text").value, customShortURL: document.querySelector("#customShortURL").value })

准备放到worker的脚本

获取POST过来的数据

let req_customShortURL=req["customShortURL"]

增加自定义短链的处理

  if (config.custom_link && (req_customShortURL != "")){
    let is_exist=await LINKS.get(req_customShortURL)
    if (is_exist != null) {
      return new Response(`{"status":500,"key":": Error: Custom shortURL existed."}`, {
        headers: response_header,
      })
    }else{
      random_key = req_customShortURL
      stat, await LINKS.put(req_customShortURL, req_url)
    }
  } 

======

后记

在index.html里使用jsdelivr的url去调用main.js,结果repo里修改了jsdelivr上面没动,调试了半天怎么都不通。浪费了好几个小时。

https://gcore.jsdelivr.net/gh/crazypeace/Url-Shorten-Worker@gh-pages/main.js

最终修改为调用github.io的形式,调试成功。

https://crazypeace.github.io/Url-Shorten-Worker/main.js

当然,这样的调用方式墙内用不了。

我在gh-pages分支下面放了一个内容完全一样,只是调用jsdelivr的index-cdn.html

如果等到jsdelivr的资源同步了,在部署到worker的脚本时,157行改一下就好。

  const html= await fetch("https://crazypeace.github.io/Url-Shorten-Worker/"+config.theme+"/index-cdn.html")


评论

The Hot3 in Last 30 Days

无服务器 自建短链服务 Url-Shorten-Worker 完整的部署教程

ClouDNS .asia免费域名 托管到CloudFlare开CDN白嫖Websocket WS通道翻墙 / desec.io