
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
tiny-event-intercept
Advanced tools
Lightweight (~1.2KB gzip) TypeScript library for conditional event interception with browser-standard API. Zero dependencies, browser-first, SSR-safe.
A lightweight (~1.2KB gzip), zero-dependency TypeScript library for conditional event interception with browser-standard API. Designed for modern web apps and safe to call in SSR or other non-browser environments.
Languages: English | 简体中文
AbortSignal supportAddEventListenerOptions APInpm install tiny-event-intercept
import { interceptEvents } from 'tiny-event-intercept'
// Prevent clicks when feature is disabled
let isFeatureEnabled = false
const cleanup = interceptEvents(document, {
events: 'click',
when: () => !isFeatureEnabled, // Only intercept when feature is disabled
listener: (event) => {
console.log('Feature is currently disabled')
event.preventDefault()
event.stopPropagation()
},
})
// Enable feature later
isFeatureEnabled = true
// Clean up when done (removes all event listeners)
cleanup()
interceptEvents(target, options): CleanupFunctionCreates conditional event interceptors with a browser-standard API.
function interceptEvents(target: TargetElement, options: InterceptOptions): CleanupFunction
// Types
type TargetElement = EventTarget | (() => EventTarget | null) | null
type EventTypes<K extends keyof GlobalEventHandlersEventMap = keyof GlobalEventHandlersEventMap> =
| K
| readonly K[]
interface InterceptOptions<K extends keyof GlobalEventHandlersEventMap = keyof GlobalEventHandlersEventMap>
extends AddEventListenerOptions {
events: EventTypes<K> // Event types to intercept
when: () => boolean // Condition function
listener: (event: GlobalEventHandlersEventMap[K]) => void // Event handler
// Inherits: capture?, once?, passive?, signal?
}
type EventTarget = Element | Document | Window
type CleanupFunction = () => void
Parameters:
target - Target element, function returning element, or null (defaults to document)options - Intercept options including events, condition, and listenerReturns:
CleanupFunction - Function to remove all event listenersNotes:
target === null defaults to documentinterceptEvents() is callednull or throws, no listeners are attachedas const for event arrays if you want the narrowest union type in listenerconst submitButton = document.querySelector('#submit-btn')
let isFormValid = false
const cleanup = interceptEvents(submitButton, {
events: 'click',
when: () => !isFormValid,
listener: (event) => {
event.preventDefault()
showValidationErrors()
console.log('Form submission blocked - validation failed')
},
})
let isLoading = false
const cleanup = interceptEvents(document, {
events: ['click', 'keydown', 'submit'],
when: () => isLoading,
listener: (event) => {
event.preventDefault()
event.stopPropagation()
showLoadingMessage('Please wait...')
},
capture: true, // Intercept in capture phase for better control
})
let isModalOpen = false
const cleanup = interceptEvents(document, {
events: 'keydown',
when: () => isModalOpen,
listener: (event) => {
if (event.key === 'Escape') {
closeModal()
event.preventDefault()
}
},
})
const featureButton = document.querySelector('#new-feature-btn')
const cleanup = interceptEvents(featureButton, {
events: 'click',
when: () => !window.featureFlags?.newFeatureEnabled,
listener: (event) => {
event.preventDefault()
showFeatureNotAvailable()
},
})
// Resolve the active tab once when registering listeners
const cleanup = interceptEvents(() => document.querySelector('.tab.active'), {
events: 'click',
when: () => isTabSwitchingDisabled,
listener: (event) => {
event.preventDefault()
showMessage('Tab switching is temporarily disabled')
},
})
const controller = new AbortController()
const cleanup = interceptEvents(document.body, {
events: ['mousedown', 'touchstart'],
when: () => isDragModeActive,
listener: (event) => {
startDragOperation(event)
},
capture: true, // Capture phase for early interception
passive: false, // Allow preventDefault()
signal: controller.signal, // AbortController support
})
// Later: abort all listeners
controller.abort()
import { useEffect, useState } from 'react'
import { interceptEvents } from 'tiny-event-intercept'
function FeatureToggle() {
const [isEnabled, setIsEnabled] = useState(false)
useEffect(() => {
const cleanup = interceptEvents(document, {
events: 'click',
when: () => !isEnabled,
listener: (event) => {
console.log('Feature disabled')
event.preventDefault()
}
})
return cleanup // Cleanup on unmount
}, [isEnabled])
return (
<button onClick={() => setIsEnabled(!isEnabled)}>
{isEnabled ? 'Disable' : 'Enable'} Feature
</button>
)
}
import { onMounted, onUnmounted, ref } from 'vue'
import { interceptEvents } from 'tiny-event-intercept'
export default {
setup() {
const isEnabled = ref(false)
let cleanup: (() => void) | null = null
onMounted(() => {
cleanup = interceptEvents(document, {
events: 'click',
when: () => !isEnabled.value,
listener: (event) => event.preventDefault(),
})
})
onUnmounted(() => {
cleanup?.()
})
return { isEnabled }
},
}
The library provides predictable cleanup mechanisms:
signalconst cleanup = interceptEvents(document, {
events: 'click',
when: () => true,
listener: () => {},
})
// Safe to call multiple times
cleanup()
cleanup() // No errors
MIT
FAQs
Lightweight (~1.2KB gzip) TypeScript library for conditional event interception with browser-standard API. Zero dependencies, browser-first, SSR-safe.
We found that tiny-event-intercept demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.