projectdiscovery/katana · error

could not get event listeners

Error message

could not get event listeners

What it means

Wrapper error from FindNavigations when b.GetEventListeners() fails. GetEventListeners evaluates window.__eventListeners (tolerated failure) and then window.getAllElementsWithEventListeners() (hard failure) plus JSON unmarshaling of the inline listener list; any error in the latter two is wrapped here. Event-listener based action discovery is aborted for the page.

Source

Thrown at pkg/engine/headless/browser/element.go:157

			continue
		}

		hash := link.Hash()
		link.MD5Hash = hash

		if _, found := unique[hash]; found {
			continue
		}
		unique[hash] = struct{}{}
		navigations = append(navigations, &types.Action{
			Type:    types.ActionTypeLeftClick,
			Element: link,
		})
	}

	eventListeners, err := b.GetEventListeners()
	if err != nil {
		return nil, errors.Wrap(err, "could not get event listeners")
	}
	for _, listener := range eventListeners {
		if _, found := relevantEventListeners[listener.Type]; !found {
			continue
		}
		if listener.Element == nil {
			continue
		}
		hash := listener.Element.Hash()
		listener.Element.MD5Hash = hash
		if _, found := unique[hash]; found {
			continue
		}
		unique[hash] = struct{}{}
		navigations = append(navigations, types.ActionFromEventListener(listener))
	}

	navLinks, err := b.GetNavigatedLinks()

View on GitHub (pinned to e3e742739c)

Solutions

  1. Confirm katana's injected helpers run on all frames/documents (update katana if using an old build)
  2. Wait for stable page load before discovery to avoid context races
  3. Retry the page once on transient eval errors
  4. Log-and-skip: treat a single page failure as non-fatal for the whole crawl

Example fix

// before
listeners, err := page.FindNavigations()
if err != nil { return err }
// after
listeners, err := page.FindNavigations()
if err != nil {
    return fmt.Errorf("navigation discovery skipped: %w", err) // or log+continue
}
Defensive patterns

Strategy: try-catch

Validate before calling

// probe that injected helpers exist in this document
if _, err := page.Eval("() => typeof window.getAllElementsWithEventListeners === 'function'"); err != nil {
    return // helpers missing; discovery will fail
}

Type guard

func helpersInjected(p *browser.BrowserPage) bool {
    out, err := p.Eval("() => typeof window.getAllElementsWithEventListeners")
    return err == nil && strings.Contains(out.Value.String(), "function")
}

Try / catch

navs, err := page.FindNavigations()
if err != nil {
    logger.Debug("event listener discovery failed", slog.String("error", err.Error()))
    return navsFallback // continue with forms/buttons/links only
}

Prevention

When it happens

Trigger: 1) window.getAllElementsWithEventListeners helper missing from the document (injection scripts not run on this page/iframe). 2) Execution context destroyed by navigation during discovery. 3) The returned listener JSON does not match the expected element/listeners shape, failing Unmarshal. 4) Tab closed/crashed.

Common situations: Crawling SPAs or iframes where init scripts don't cover every document; sites whose inline listeners produce odd serialization; rapid client-side navigation races.

Related errors


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