惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

空白Koobai

唠叨 - 2026-05-10 10:45 唠叨 - 2026-05-08 23:34 唠叨 - 2026-05-08 15:55 唠叨 - 2026-05-07 14:38 唠叨 - 2026-05-06 11:47 唠叨 - 2026-04-30 14:40 唠叨 - 2026-04-30 12:51 唠叨 - 2026-04-22 12:46 唠叨 - 2026-04-18 09:35 唠叨 - 2026-04-16 12:24 唠叨 - 2026-04-12 10:47 唠叨 - 2026-04-06 12:25 唠叨 - 2026-04-03 19:57 唠叨 - 2026-04-02 22:38 唠叨 - 2026-04-01 23:23 唠叨 - 2026-03-31 14:08 唠叨 - 2026-03-28 16:43 唠叨 - 2026-03-28 09:39 唠叨 - 2026-03-27 11:10 唠叨 - 2026-03-25 11:48 唠叨 - 2026-03-24 13:12 唠叨 - 2026-03-21 16:55 唠叨 - 2026-03-20 11:48 罢免小区业委会之后续 反角度之博客用户体验不能做的 N 件事 唠叨 - 2026-03-16 13:10 唠叨 - 2026-03-13 12:46 唠叨 - 2026-03-11 19:25 唠叨 - 2026-03-07 20:21 唠叨 - 2026-03-04 13:45 唠叨 - 2026-02-25 23:14 唠叨 - 2026-02-25 18:16 唠叨 - 2026-02-23 16:11 唠叨 - 2026-02-21 16:21 唠叨 - 2026-02-05 09:19 唠叨 - 2026-02-04 20:18 唠叨 - 2026-01-29 13:03 唠叨 - 2026-01-27 15:40 Hugo 通过 Github 自动推送到宝塔 (2026 年版备忘) 唠叨 - 2026-01-24 15:55 唠叨 - 2026-01-24 15:38 2025 关键词 唠叨 - 2025-06-22 22:07 唠叨 - 2025-06-10 17:31 唠叨 - 2025-05-14 22:04 唠叨 - 2025-04-16 22:06 唠叨 - 2025-04-09 10:28 唠叨 - 2025-04-07 22:09 唠叨 - 2025-03-22 11:30 唠叨 - 2025-01-29 23:14 唠叨 - 2025-01-21 19:30 唠叨 - 2025-01-10 19:39 唠叨 - 2025-01-09 13:57 罢免小区业委会 唠叨 - 2024-12-20 09:48 唠叨 - 2024-12-19 14:46 唠叨 - 2024-12-19 09:37 唠叨 - 2024-12-18 22:04 唠叨 - 2024-12-17 19:01 唠叨 - 2024-12-11 10:32 唠叨 - 2024-12-07 15:47 唠叨 - 2024-11-27 18:15 唠叨 - 2024-11-16 12:45 唠叨 - 2024-11-13 16:14 唠叨 - 2024-09-29 11:51 唠叨 - 2024-09-26 20:24 唠叨 - 2024-09-20 18:13 从 iOS 更换到 Android 会有不适吗? 唠叨 - 2024-09-17 11:21 唠叨 - 2024-09-14 20:10 唠叨 - 2024-09-04 20:51 唠叨 - 2024-09-02 20:17 唠叨 - 2024-08-08 14:19 唠叨 - 2024-08-07 16:45 唠叨 - 2024-07-27 23:21 唠叨 - 2024-07-09 23:37 唠叨 - 2024-06-15 10:43 唠叨 - 2024-06-01 23:03 唠叨 - 2024-05-31 10:06 五一假期之登高望远 以后如果小米做汽车了,也可以买小米汽车 唠叨 - 2024-03-25 14:05 唠叨 - 2024-03-23 15:26 唠叨 - 2024-03-20 21:06 自行车给自己带来了什么情绪价值爽点 唠叨 - 2024-02-28 15:44 HUGO 折腾随记之热力图 / 段落导航 唠叨 - 2024-02-21 21:00 唠叨 - 2024-02-18 19:38 唠叨 - 2024-02-17 16:24 这个春节有什么不一样 唠叨 - 2024-02-16 11:35 唠叨 - 2024-02-15 14:56 唠叨 - 2024-02-14 18:30 唠叨 - 2024-02-03 20:23 唠叨 - 2024-01-31 20:20 折腾博客更像是自己的精神鸦片 唠叨 - 2024-01-29 16:21 唠叨 - 2024-01-27 10:54 唠叨 - 2024-01-24 16:16
HUGO 外链跳转到中间页
2024-02-01 · via 空白Koobai

前天在老麦博客中看到" 给Twikoo添加链接跳转中间页“博文,因为外部链接你无法确定未来某一天可能就会变成不安全链接,从而影响到自己博客。同时又见到 大大的小蜗牛也是因为安全的原因,把博客评论换成了GitHub Discussions。网上也有不少的案例,因为某个链接的原因,而被请喝茶或电话让自己排查。今天在 不亦乐乎那见链接也是跳转到中间页,于是请教了下怎么实现的,感谢。之后经过与 AI 的博弈,搞定。这次使用 ChatGPT 跟 Claude 都不行,说了一堆,完全不懂,最后使用 coze 搞定基本框架,然后再来回沟通逐步完善功能。估计跟它使用的 ChatGPT 4.0 接口有关。想充值 ChatGPT,奈何月费太贵了。

新建 js 文件,如:tiaozhuan.js。里面的网址为排除跳转中间页。

function checkParent(element, classNames) {
while (element) {
if (element.classList && classNames.some(cn => element.classList.contains(cn))) {
return true;
}
element = element.parentElement;
}
return false;
}
var excludedClasses = ['talks_comments','tiaozhuan-button']; // 添加需要排除的a标签类名class
window.addEventListener('load', (event) => {
document.body.addEventListener('click', function(e) {
let target = e.target;
while (target && target.nodeName !== 'A') {
target = target.parentNode;
}
if (target && target.nodeName === 'A' &&
!checkParent(target, excludedClasses) &&
!target.href.includes('koobai.com') &&
!target.href.includes('douban.com') &&
!target.href.includes('bilibili.com') &&
!target.href.includes('github.com') &&
target.hostname !== window.location.hostname) {
e.preventDefault();
let encodedUrl = btoa(target.href); // Base64 encode the URL
let url = '/tiaozhuan?target=' + encodedUrl;
window.open(url, '_blank');
}
});
});

在模板_default目录下,新建一个 html,如tiaozhuan.html。

{{ define "body_classes" }}page-tiaozhuan{{ end }}
{{ define "main" }}
<div class="tiaozhuan-all">
  <div class="tiaozhuan-nrong">
    <div class="tiaozhuan-title">即将离开 {{ .Site.Title }},跳转到以下外部链接</div>
    <div id="tiaozhuan-link"></div> 
    <div class="tiaozhuan-info">请自行识别该链接是否安全,并保管好个人信息。</div>
      <div class="tiaozhuan-button"><a href='' id='direct-link'>继续访问</a></div>
  </div>
</div>
<script>
  const params = new URLSearchParams(window.location.search);
  const encodedTarget = params.get('target');
  const target = atob(encodedTarget); // 使用 atob 进行 Base64 解码
  
  if (target) {
    document.getElementById('direct-link').href = target;
    document.getElementById('tiaozhuan-link').textContent = '' + target; // 直接显示目标地址    
  } else {
    console.error('未指定重定向目标。');
  }
</script>
{{ end }}

相关 css,根据主题风格调整。其中 background 为明暗模式下不同的背景图。

.tiaozhuan-all{
  position: relative;
  box-shadow: rgba(0, 0, 0, 0.25) 0px 25px 50px -12px;
  border-radius: 10px;
  background: #fff url(/tiaozhuanico.webp) no-repeat center center / cover;
  color: #666;
  word-break: break-all;
  max-width: 700px;
  height: 350px;
  text-align: center;
  font-size: 0.85rem;
  overflow: hidden;
  margin: 100px auto 0; 
  @include breakpoint('small') {
    aspect-ratio: 2 / 1;
    height: auto;
  }
}
.tiaozhuan-nrong{
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20px 20px 30px 20px;
}
.tiaozhuan-title{
  font-size: 1.3rem;
  font-family: var(--font-family-title);
  color: #222;
  line-height: 1.4;
  margin-bottom: 4px;
}
.tiaozhuan-info{
  margin-top: 6px;
}
.tiaozhuan-button{
  margin-top: 20px;
}

.tiaozhuan-button a{
  color: var(--text-highlight-color);
  border-radius: 4px;
  padding: 10px 30px;
  font-family: var(--font-family-title);
  font-size: 0.85rem;
  border: 0.5px solid var(--text-highlight-color);
  display: inline-block;
}

@media (prefers-color-scheme: dark) {
  .tiaozhuan-all{
    background: var(--background-card-dark-color) url(/tiaozhuandark.webp) no-repeat center center / cover;
    color: #acacac;
  }
  .tiaozhuan-title{
    color: #ddd;
  }
}

最后在页尾引用js文件即可,如

<script src="/js/tiaozhuan.js"></script>

搞定,整体思路是:js文件识别链接的a标签,并把链接用 base64 编码,同时排除一些不需要跳转中间页的class或网站。然后跳转到tiaozhuan.html文件中(链接格式为/tiaozhuan?target=base64编码),html文件承担了中间页具体信息,及跳转动作。如使用base64解码跳转的链接。

以上为全局引用JS方法,判断 a 标签。下面利用 Hugo 内置的 render-link.html 实现。在模板_default/_makup目录下新建 render-link.html文件。思路是直接给链接添加 “/tiaozhuan?target=base64编码”。下面代码中的 “/” 为排除内链;网址为排除特定地址。感谢 大大的小蜗牛提示的 render-link 方法。

{{ $url := .Destination }}
<a 
    {{ if or (in $url "koobai.com") (strings.HasPrefix $url "/") }}
        href="{{ $url }}"
    {{ else }}
        href="/tiaozhuan?target={{ $url | base64Encode }}"
    {{ end }}
    {{ if strings.HasPrefix .Destination "http" }} target="_blank"{{ end }}
>
{{ .Text | safeHTML }}</a>

针对无法给链接添加 “/tiaozhuan?target=base64编码” 的,譬如 Artalk 评论,可以特定使用 js 判断。里面的.atk-comment-wrap a 就是 Artalk 评论的 a 标签;网址为排除特定地址。

document.body.addEventListener('click', function(e) {
  let target = e.target.closest('.atk-comment-wrap a, .datacont a');
  if (target && !target.href.includes('koobai.com')) {
      e.preventDefault();
      let encodedUrl = btoa(target.href);
      let url = '/tiaozhuan?target=' + encodedUrl;
      window.open(url, '_blank');
  }
});

对于浏览体验来说,多了一步跳转,总归是不好,但为了愉快的玩耍博客,稳妥一点更佳。现在把各个页面的评论都去掉了,只留下了唠叨跟博文评论,并且把博文的评论默认设为隐藏。倒也不是因为所谓的安全,主要是觉得评论的样式有待提高。

如果觉得跳转中间页很烦人,浏览器可以装个 Skip Redirect插件,告别所有网站的中间页跳转,直接抵达目标地址。