{"id":45366025,"url":"https://github.com/febalist/element-detector","last_synced_at":"2026-02-21T14:16:07.328Z","repository":{"id":320943879,"uuid":"616514356","full_name":"febalist/element-detector","owner":"febalist","description":null,"archived":false,"fork":false,"pushed_at":"2025-10-26T22:26:34.000Z","size":195,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-26T22:27:23.549Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/element-detector","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/febalist.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"febalist"}},"created_at":"2023-03-20T14:35:06.000Z","updated_at":"2025-10-26T21:46:55.000Z","dependencies_parsed_at":"2025-10-26T22:27:31.395Z","dependency_job_id":null,"html_url":"https://github.com/febalist/element-detector","commit_stats":null,"previous_names":["febalist/element-detector"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/febalist/element-detector","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/febalist%2Felement-detector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/febalist%2Felement-detector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/febalist%2Felement-detector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/febalist%2Felement-detector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/febalist","download_url":"https://codeload.github.com/febalist/element-detector/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/febalist%2Felement-detector/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29682981,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-21T13:29:26.630Z","status":"ssl_error","status_checked_at":"2026-02-21T13:26:50.125Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-02-21T14:16:06.642Z","updated_at":"2026-02-21T14:16:07.321Z","avatar_url":"https://github.com/febalist.png","language":"TypeScript","funding_links":["https://github.com/sponsors/febalist"],"categories":[],"sub_categories":[],"readme":"# element-detector\n\nA library for detecting elements appearing in the DOM. Executes callbacks when elements matching a selector are added to the page.\n\nUses [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) to efficiently track DOM changes and notify about new elements. Works at any stage of page lifecycle - during initial load, after DOMContentLoaded, or during dynamic content updates.\n\n## Installation\n\n### For Bundlers\n\n```bash\nnpm install element-detector\n```\n\n### For Userscripts\n\nAdd the library via `@require` in your userscript metadata:\n\n```javascript\n// ==UserScript==\n// @name         My Userscript\n// @namespace    http://tampermonkey.net/\n// @version      0.1\n// @description  Example userscript using element-detector\n// @require      https://cdn.jsdelivr.net/npm/element-detector/dist/index.global.min.js\n// @grant        none\n// ==/UserScript==\n\n(function () {\n  'use strict';\n\n  // Library is available as window.ElementDetector\n  const {detect} = window.ElementDetector;\n\n  // Now you can use detect() in your script\n  detect('.some-element', (element) =\u003e {\n    console.log('Element found:', element);\n  });\n})();\n```\n\nAlternative CDN links:\n\n- jsDelivr (minified): `https://cdn.jsdelivr.net/npm/element-detector/dist/index.global.min.js`\n- jsDelivr (unminified): `https://cdn.jsdelivr.net/npm/element-detector/dist/index.global.js`\n- unpkg (minified): `https://unpkg.com/element-detector/dist/index.global.min.js`\n- unpkg (unminified): `https://unpkg.com/element-detector/dist/index.global.js`\n\n## Usage\n\n### With callback\n\n```typescript\nimport {detect} from 'element-detector';\n\n// Called for each new element matching the selector\ndetect('.notification', (element) =\u003e {\n  console.log('New notification:', element);\n});\n```\n\n### Without callback (Promise)\n\n```typescript\nimport {detect} from 'element-detector';\n\n// Returns a promise that resolves with the first matching element\nconst modal = await detect('.modal');\nconsole.log('Modal appeared:', modal);\n```\n\n## API\n\n### `detect(selector, callback?, options?)`\n\n#### Parameters\n\n**selector**: `string`\n\n- CSS selector to match elements\n\n**callback**: `(element: T) =\u003e void` (optional)\n\n- Function called for each matching element\n- If omitted, returns a Promise\n\n**options**: `DetectOptions\u003cT\u003e` (optional)\n\n- Configuration object\n\n#### Returns\n\n- `Detector` - when callback is provided\n- `Promise\u003cT\u003e` - when callback is omitted (defaults to `{ once: true }`)\n\n### Options\n\n**existing**: `boolean` (default: `false`)\n\n- When `true`, processes elements that already exist in the DOM at the time `detect` is called\n- When `false`, only processes elements added after the call\n\n```typescript\ndetect('.item', callback, {existing: true});\n```\n\n**once**: `boolean` (default: `false`)\n\n- When `true`, stops watching after the first matching element\n- Automatically calls `stop()` on the detector\n\n```typescript\ndetect('.dialog', callback, {once: true});\n```\n\n**filter**: `(element: T) =\u003e boolean`\n\n- Additional filtering function applied to matched elements\n- Only elements for which the function returns `true` will trigger the callback\n\n```typescript\ndetect('a', callback, {\n  filter: (link) =\u003e link.hostname !== window.location.hostname\n});\n```\n\n**timeout**: `number`\n\n- Time in milliseconds after which watching automatically stops\n- Creates an internal AbortController that triggers after the specified time\n\n```typescript\ndetect('.widget', callback, {timeout: 5000});\n```\n\n**signal**: `AbortSignal`\n\n- External AbortSignal for manual control\n- When the signal is aborted, watching stops\n\n```typescript\nconst controller = new AbortController();\ndetect('.element', callback, {signal: controller.signal});\n\n// Later\ncontroller.abort();\n```\n\n### TypeScript Generics\n\nSpecify element type for proper typing:\n\n```typescript\ndetect\u003cHTMLButtonElement\u003e('.submit', (button) =\u003e {\n  button.disabled = false;\n});\n\ndetect\u003cHTMLAnchorElement\u003e('a', (link) =\u003e {\n  console.log(link.href);\n});\n\nconst img = await detect\u003cHTMLImageElement\u003e('img.hero');\n```\n\n### Interfaces\n\n```typescript\ninterface DetectOptions\u003cT extends Element = Element\u003e {\n  existing?: boolean;\n  filter?: (element: T) =\u003e boolean;\n  once?: boolean;\n  timeout?: number;\n  signal?: AbortSignal;\n}\n\ninterface Detector {\n  signal: AbortSignal;  // Fires when watching stops\n  stop: () =\u003e void;     // Manually stop watching\n}\n```\n\n## Examples\n\n### Initializing widgets\n\n```typescript\ndetect('.date-picker', (element) =\u003e {\n  new DatePicker(element);\n}, {existing: true});\n```\n\n### Waiting for dynamic content\n\n```typescript\nconst item = await detect('.product[data-id=\"12345\"]', {\n  timeout: 10000\n});\n\nitem.scrollIntoView();\n```\n\n### Modal dialogs\n\n```typescript\ndetect('.modal.confirmation', (modal) =\u003e {\n  const confirmBtn = modal.querySelector('.confirm');\n  confirmBtn?.addEventListener('click', handleConfirm);\n});\n```\n\n### Processing external links\n\n```typescript\ndetect('a', (link) =\u003e {\n  link.setAttribute('target', '_blank');\n  link.setAttribute('rel', 'noopener noreferrer');\n}, {\n  filter: (link) =\u003e link.hostname !== window.location.hostname,\n  existing: true\n});\n```\n\n### Timeout with fallback\n\n```typescript\nconst detector = detect('.slow-widget', (widget) =\u003e {\n  initialize(widget);\n}, {timeout: 5000});\n\ndetector.signal.addEventListener('abort', () =\u003e {\n  showFallback();\n});\n```\n\n### Manual control with AbortController\n\n```typescript\nconst controller = new AbortController();\n\ndetect('.live-update', (update) =\u003e {\n  processUpdate(update);\n}, {signal: controller.signal});\n\n// Stop watching when user leaves page section\ndocument.addEventListener('navigate', () =\u003e {\n  controller.abort();\n});\n```\n\n### Combining timeout and signal\n\n```typescript\nconst controller = new AbortController();\n\nconst detector = detect('.element', callback, {\n  signal: controller.signal,\n  timeout: 10000\n});\n\n// Stops when either timeout is reached OR controller.abort() is called\n// detector.signal combines both signals\n```\n\n### Waiting for third-party scripts\n\n```typescript\nconst widget = await detect('.third-party-widget', {\n  timeout: 5000,\n  existing: true\n});\n\ninitializeIntegration(widget);\n```\n\n## Browser Support\n\n[Check browser compatibility](https://caniuse.com/mutationobserver,abortcontroller,mdn-api_abortsignal_any_static,promises)\n\nRequires:\n\n- [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)\n- [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)\n- [AbortSignal.any()](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static)\n- [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffebalist%2Felement-detector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffebalist%2Felement-detector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffebalist%2Felement-detector/lists"}