projectdiscovery/katana · error

failed to inject page-init.js

Error message

failed to inject page-init.js

What it means

Same call path as the utils.js injection error, but for pageInitJavascriptBundle (page-init.js). page.EvalOnNewDocument failed when registering this second bundle. Since utils.js is injected first, reaching this error confirms the page was alive at that point and the failure is specific to the page-init bundle evaluation or a page that died between the two injections.

Source

Thrown at pkg/engine/headless/js/js.go:24

	"github.com/go-rod/rod"
	"github.com/pkg/errors"
)

var (
	//go:embed utils.js
	utilsJavascriptBundle string

	//go:embed page-init.js
	pageInitJavascriptBundle string
)

// InitJavascriptEnv injects the necessary javascript code into the browser
func InitJavascriptEnv(page *rod.Page) error {
	if _, err := page.EvalOnNewDocument(utilsJavascriptBundle); err != nil {
		return errors.Wrap(err, "failed to inject utils.js")
	}
	if _, err := page.EvalOnNewDocument(pageInitJavascriptBundle); err != nil {
		return errors.Wrap(err, "failed to inject page-init.js")
	}
	return nil
}

View on GitHub (pinned to e3e742739c)

Solutions

  1. Rebuild/re-embed the page-init.js bundle and verify it parses as valid JavaScript.
  2. Call InitJavascriptEnv immediately after page creation so the two injections happen back-to-back.
  3. Check that the page/browser are still alive; recreate the page and retry the full initialization.
  4. Inspect the wrapped CDP error to distinguish an evaluation (syntax) error from a connection loss.

Example fix

// before
if _, err := page.EvalOnNewDocument(pageInitJavascriptBundle); err != nil {
    return errors.Wrap(err, "failed to inject page-init.js")
}
// after
if err := js.InitJavascriptEnv(page); err != nil {
    return fmt.Errorf("init js env: %w (page alive: %v)", err, page != nil)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// verify the bundle content is non-empty and was built correctly
if len(pageInitJavascriptBundle) == 0 {
    return errors.New("page-init.js bundle is empty; check embedded assets")
}

Try / catch

// Go: single init function with one retry on fresh page
if err := js.InitJavascriptEnv(page); err != nil {
    if strings.Contains(err.Error(), "page-init.js") {
        page = browser.MustPage("")
        return js.InitJavascriptEnv(page)
    }
    return err
}

Prevention

When it happens

Trigger: Calling InitJavascriptEnv where the page-init.js bundle content is malformed (embedded/incorrectly built bundle), or the page's CDP connection is lost between the utils.js and page-init.js injections.

Common situations: Corrupted embedded JS assets from a bad build; page crashed/closed right after the first injection; browser upgrade changing CDP behavior mid-run.

Related errors


AI-assisted analysis of projectdiscovery/katana@e3e742739c (2026-09-03). Data as JSON: /api/errors/da4348389c2e79ac. Report an issue: GitHub.