improve js rewriting functionality by not relying on js to get proxy and proxified URLs; direct injection from golang
This commit is contained in:
@@ -145,7 +145,11 @@ func (r *HTMLResourceURLRewriter) Read(p []byte) (int, error) {
|
||||
// inject <script> right after <head>
|
||||
isHeadToken := (r.currentToken.Type == html.StartTagToken || r.currentToken.Type == html.SelfClosingTagToken) && r.currentToken.Data == "head"
|
||||
if isHeadToken {
|
||||
injectScript(r.tokenBuffer, rewriteJSResourceUrlsScript)
|
||||
params := map[string]string{
|
||||
"PROXY_ORIGIN_INJECT_FROM_GOLANG": r.proxyURL,
|
||||
"ORIGIN_INJECT_FROM_GOLANG": fmt.Sprintf("%s://%s", r.baseURL.Scheme, r.baseURL.Host),
|
||||
}
|
||||
injectScriptWithParams(r.tokenBuffer, rewriteJSResourceUrlsScript, params)
|
||||
}
|
||||
|
||||
r.currentTokenProcessed = false
|
||||
@@ -171,6 +175,19 @@ func injectScript(tokenBuffer *bytes.Buffer, script string) {
|
||||
)
|
||||
}
|
||||
|
||||
func injectScriptWithParams(tokenBuffer *bytes.Buffer, script string, params map[string]string) {
|
||||
for old, new := range params {
|
||||
script = strings.ReplaceAll(
|
||||
script,
|
||||
fmt.Sprintf("`${%s}`", old),
|
||||
fmt.Sprintf("`${%s}`", new),
|
||||
)
|
||||
}
|
||||
tokenBuffer.WriteString(
|
||||
fmt.Sprintf("\n<script>\n%s\n</script>\n", script),
|
||||
)
|
||||
}
|
||||
|
||||
// possible ad-blocking / bypassing opportunity here
|
||||
func modifyInlineScript(scriptContentBuffer *bytes.Buffer) string {
|
||||
return html.UnescapeString(scriptContentBuffer.String())
|
||||
|
||||
Reference in New Issue
Block a user