链接卡片图片修复:img 加 referrerpolicy=no-referrer 绕过哔哩哔哩等 CDN 防盗链;抓取 UA 换浏览器形态;识别并丢弃 X 的全站占位图

This commit is contained in:
Sakurasan
2026-09-28 21:50:41 +08:00
parent a234afe448
commit 1db1698174
3 changed files with 10 additions and 1 deletions
+3 -1
View File
@@ -26,7 +26,9 @@ const (
maxBody = 512 << 10
// maxRedirects 跳转链上限。
maxRedirects = 3
userAgent = "Mozilla/5.0 (compatible; ONEBot/1.0; +link-preview)"
// 浏览器形态的 UA:不少站点(X、微博、部分门户)对未知爬虫 UA 给的
// og 元数据不全或直接拒绝,对浏览器 UA 则照常输出 <head> 里的标签。
userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
)
// Card 是抓取结果的数据形状;定义在 model 里(要落库、要给前端),
+6
View File
@@ -53,6 +53,12 @@ func parse(doc string, base *url.URL) *Card {
if img := first(meta["og:image"], meta["twitter:image"], meta["twitter:image:src"]); img != "" {
c.Image = resolve(img, base)
}
// X(Twitter)对非白名单爬虫只回一张全站通用灰图占位符,
// 拿它当封面只会显示一张无意义的底图——当作没有图处理。
// 真推文图只发给 Twitterbot 等白名单 UA,服务端拿不到,不硬来。
if strings.Contains(c.Image, "abs.twimg.com/rweb/ssr/default") {
c.Image = ""
}
return c
}
+1
View File
@@ -39,6 +39,7 @@ function open() {
alt=""
loading="lazy"
decoding="async"
referrerpolicy="no-referrer"
@error="imgBroken = true"
/>
</div>