縮網址是很重要又方便的工具,但打開網頁、貼網址、縮網址整個流程太繁瑣
以前使用 Tiny URL Shortener、現在是用 X.gd 官方提供的縮網址插件一鍵完成
但也許有更簡單的方式?試試用書籤吧!
本來是用 Tiny 來做,但 Tiny 現在使用上不太流暢,所以改用 X.gd 來做。
首先到官網申請 API 金鑰,用金鑰替換掉下方程式碼的 YOUR_API_KEY 位置
javascript:(function(){const apiUrl='https://xgd.io/V1/shorten?url='+encodeURIComponent(location.href)+'&key=YOUR_API_KEY';fetch(apiUrl).then(r=>r.json()).then(res=>{if(res.status===200&&res.shorturl){navigator.clipboard.writeText(res.shorturl);let d=document.createElement('div');d.innerText='✨ 已複製 X.gd 短網址:'+res.shorturl;d.style.cssText='position:fixed;bottom:20px;right:20px;background:#323232;color:#fff;padding:10px 16px;border-radius:8px;z-index:999999;font-size:14px;box-shadow:0 4px 12px rgba(0,0,0,0.3);transition:opacity 0.3s;';document.body.appendChild(d);setTimeout(()=>{d.style.opacity='0';setTimeout(()=>d.remove(),300);},1500);}}).catch(()=>{});})();
然後在書籤列上新建一個空白書籤,右鍵編輯、把替換好金鑰的程式碼整組貼進網址後儲存就大功告成啦,以後不管是手機還是電腦都可以輕鬆使用縮網址!
部份瀏覽器可能會跳出提示,問是否允許讀寫剪貼簿或執行腳本,記得要按允許、程式碼才能正常工作喔。
下面是原本用 Tiny 製作的程式碼,不需要申請 api 金鑰就能用,但就是打開縮網址會跑出轉址畫面感覺有點卡,操作方式一樣是編輯空白書籤並在網址貼上這段程式碼
javascript:(function(){const targetUrl='https://tinyurl.com/api-create.php?url=%27+encodeURIComponent(location.href);fetch(targetUrl).then(r=>r.text()).then(res=>{if(res.startsWith(%27http%27)){navigator.clipboard.writeText(res);alert(%27已自動複製短網址:%27+res);}else{alert(%27縮網址失敗%27);}}).catch(()=>alert(%27發生錯誤%27));})();
就可以一鍵產生縮網址並複製囉
要是連這個確定都不想按的話,把程式碼換成下面這樣
javascript:(function(){const targetUrl='https://tinyurl.com/api-create.php?url='+encodeURIComponent(location.href);fetch(targetUrl).then(r=>r.text()).then(res=>{if(res.startsWith('http')){navigator.clipboard.writeText(res);let d=document.createElement('div');d.innerText='✨ 已複製短網址:'+res;d.style.cssText='position:fixed;bottom:20px;right:20px;background:#323232;color:#fff;padding:10px 16px;border-radius:8px;z-index:999999;font-size:14px;box-shadow:0 4px 12px rgba(0,0,0,0.3);transition:opacity 0.3s;';document.body.appendChild(d);setTimeout(()=>{d.style.opacity='0';setTimeout(()=>d.remove(),300);},1500);}}).catch(()=>{});})();
這樣按下縮網址書籤後,畫面右下角就會跳出能自動消失的懸浮提示囉
如果遇到失敗,通常代表著 Tiny 暫時無法使用,這時候推薦使用 X.gd