From 7dbc7e3d4ab282130b8b393ece9124e9b49536d2 Mon Sep 17 00:00:00 2001 From: XOF Date: Mon, 15 Dec 2025 17:33:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20proxy/rewriter.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxy/rewriter.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/proxy/rewriter.go b/proxy/rewriter.go index 2891519..138136a 100644 --- a/proxy/rewriter.go +++ b/proxy/rewriter.go @@ -43,6 +43,23 @@ func (r *ContentRewriter) RewriteHTML(body []byte) ([]byte, error) { func (r *ContentRewriter) rewriteNode(n *html.Node) { if n.Type == html.ElementNode { + + // 在 head 标签中注入请求拦截脚本 + if n.Data == "head" && n.FirstChild != nil { + script := &html.Node{ + Type: html.ElementNode, + Data: "script", + } + script.AppendChild(&html.Node{ + Type: html.TextNode, + Data: `(function(){var o=XMLHttpRequest.prototype.open,s=XMLHttpRequest.prototype.send;XMLHttpRequest.prototype.open=function(m,u){if(typeof u==="string"&&u.startsWith("/")){u="/p/` + r.token + `"+u}return o.apply(this,arguments)};fetch=new Proxy(fetch,{apply:function(t,c,a){if(typeof a[0]==="string"&&a[0].startsWith("/")){a[0]="/p/` + r.token + `"+a[0]}return Reflect.apply(t,c,a)}})})();`, + }) + script.NextSibling = n.FirstChild + n.FirstChild.PrevSibling = script + script.Parent = n + n.FirstChild = script + } + attrs := map[string]bool{"href": true, "src": true, "action": true, "data": true} for i, attr := range n.Attr {