From 6267509fa49168e0ad6b6c7b2aef42f3bad059ec Mon Sep 17 00:00:00 2001 From: XOF Date: Mon, 15 Dec 2025 19:44:12 +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 | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/proxy/rewriter.go b/proxy/rewriter.go index 4817cd2..e024f39 100644 --- a/proxy/rewriter.go +++ b/proxy/rewriter.go @@ -54,10 +54,15 @@ func (r *ContentRewriter) rewriteNode(n *html.Node) { Type: html.TextNode, Data: `(function(){var t="/p/` + r.token + `";var b="` + r.baseURL.String() + `";function r(u){if(!u||typeof u!=="string")return u;if(u.startsWith(t))return u;if(u.startsWith("http://")||u.startsWith("https://")||u.startsWith("data:")||u.startsWith("blob:")||u.startsWith("javascript:"))return u;if(u.startsWith("/")){return t+u}try{var a=new URL(u,b);if(a.origin===new URL(b).origin){return t+a.pathname+a.search+a.hash}}catch(e){}return u}var o=XMLHttpRequest.prototype.open;XMLHttpRequest.prototype.open=function(m,u){arguments[1]=r(u);return o.apply(this,arguments)};var f=window.fetch;window.fetch=function(u,opt){return f.call(this,r(u),opt)};var oa=Element.prototype.setAttribute;Element.prototype.setAttribute=function(n,v){if((n==="href"||n==="src")&&typeof v==="string"){v=r(v)}return oa.call(this,n,v)};var oi=Image;window.Image=function(){var i=new oi();var d=Object.getOwnPropertyDescriptor(HTMLImageElement.prototype,"src");if(d&&d.set){var os=d.set;Object.defineProperty(i,"src",{set:function(v){os.call(this,r(v))},get:d.get})}return i};document.addEventListener("submit",function(e){var a=e.target.action;if(a){e.target.action=r(a)}},true)})();`, }) - script.NextSibling = n.FirstChild - n.FirstChild.PrevSibling = script - script.Parent = n - n.FirstChild = script + // 插入到 head 最前面 + if n.FirstChild != nil { + script.NextSibling = n.FirstChild + n.FirstChild.PrevSibling = script + script.Parent = n + n.FirstChild = script + } else { + n.AppendChild(script) + } } attrs := map[string]bool{"href": true, "src": true, "action": true, "data": true}