更新 proxy/rewriter.go

This commit is contained in:
XOF
2025-12-15 17:33:57 +08:00
parent 0cb3d165bc
commit 7dbc7e3d4a

View File

@@ -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 {