{"record":{"id":"da4348389c2e79ac","repo":"projectdiscovery/katana","slug":"failed-to-inject-page-init-js","errorCode":null,"errorMessage":"failed to inject page-init.js","messagePattern":"failed to inject page-init\\.js","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/engine/headless/js/js.go","lineNumber":24,"sourceCode":"\t\"github.com/go-rod/rod\"\n\t\"github.com/pkg/errors\"\n)\n\nvar (\n\t//go:embed utils.js\n\tutilsJavascriptBundle string\n\n\t//go:embed page-init.js\n\tpageInitJavascriptBundle string\n)\n\n// InitJavascriptEnv injects the necessary javascript code into the browser\nfunc InitJavascriptEnv(page *rod.Page) error {\n\tif _, err := page.EvalOnNewDocument(utilsJavascriptBundle); err != nil {\n\t\treturn errors.Wrap(err, \"failed to inject utils.js\")\n\t}\n\tif _, err := page.EvalOnNewDocument(pageInitJavascriptBundle); err != nil {\n\t\treturn errors.Wrap(err, \"failed to inject page-init.js\")\n\t}\n\treturn nil\n}\n","sourceCodeStart":6,"sourceCodeEnd":28,"githubUrl":"https://github.com/projectdiscovery/katana/blob/e3e742739c3746f085943ce918fb4e2b8daf6fe6/pkg/engine/headless/js/js.go#L6-L28","documentation":"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.","triggerScenarios":"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.","commonSituations":"Corrupted embedded JS assets from a bad build; page crashed/closed right after the first injection; browser upgrade changing CDP behavior mid-run.","solutions":["Rebuild/re-embed the page-init.js bundle and verify it parses as valid JavaScript.","Call InitJavascriptEnv immediately after page creation so the two injections happen back-to-back.","Check that the page/browser are still alive; recreate the page and retry the full initialization.","Inspect the wrapped CDP error to distinguish an evaluation (syntax) error from a connection loss."],"exampleFix":"// before\nif _, err := page.EvalOnNewDocument(pageInitJavascriptBundle); err != nil {\n    return errors.Wrap(err, \"failed to inject page-init.js\")\n}\n// after\nif err := js.InitJavascriptEnv(page); err != nil {\n    return fmt.Errorf(\"init js env: %w (page alive: %v)\", err, page != nil)\n}","handlingStrategy":"try-catch","validationCode":"// verify the bundle content is non-empty and was built correctly\nif len(pageInitJavascriptBundle) == 0 {\n    return errors.New(\"page-init.js bundle is empty; check embedded assets\")\n}","typeGuard":null,"tryCatchPattern":"// Go: single init function with one retry on fresh page\nif err := js.InitJavascriptEnv(page); err != nil {\n    if strings.Contains(err.Error(), \"page-init.js\") {\n        page = browser.MustPage(\"\")\n        return js.InitJavascriptEnv(page)\n    }\n    return err\n}","preventionTips":["Validate embedded JS bundles at build time (syntax check page-init.js).","Initialize the JS environment as the first action on every new page.","Log the wrapped CDP error to distinguish evaluation errors from dead pages."],"tags":["browser-automation","javascript","cdp"],"backgroundTag":"script-injection-failed","analyzedSha":"e3e742739c3746f085943ce918fb4e2b8daf6fe6","analyzedAt":"2026-09-03T14:55:13.248Z","contentChangedAt":"2026-09-03T14:55:13.248Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}