Skip to content

Commit

Permalink
Move scriptlet/JS rule bundle injections to the top <body>
Browse files Browse the repository at this point in the history
This change prioritizes parsing and initialization of the injections compared to first-party JS. In certain cases, like YT video ads, ad-blocking performance should get improved.
  • Loading branch information
anfragment committed Jan 6, 2025
1 parent c635ea9 commit 89015e0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
4 changes: 2 additions & 2 deletions internal/jsrule/injector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsrule

import (
"bytes"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -73,8 +74,7 @@ func (inj *Injector) Inject(req *http.Request, res *http.Response) error {
injection = append(injection, injectionEnd...)

htmlrewrite.ReplaceBodyContents(res, func(match []byte) []byte {
match = append(match, injection...)
return match
return bytes.Join([][]byte{injection, match}, nil)
})

return nil
Expand Down
4 changes: 1 addition & 3 deletions internal/scriptlet/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ func (inj *Injector) Inject(req *http.Request, res *http.Response) error {
ruleInjection.Write(scriptClosingTag)

htmlrewrite.ReplaceBodyContents(res, func(match []byte) []byte {
match = append(match, inj.bundle...)
match = append(match, ruleInjection.Bytes()...)
return match
return bytes.Join([][]byte{inj.bundle, ruleInjection.Bytes(), match}, nil)
})

return nil
Expand Down

0 comments on commit 89015e0

Please sign in to comment.