更新 proxy/rewriter.go

This commit is contained in:
XOF
2025-12-15 19:44:12 +08:00
parent ff3a8722f9
commit 6267509fa4

View File

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