Skip to content

Wait longer for search result containers#1016

Open
PeterDaveHello wants to merge 1 commit into
ChatGPTBox-dev:masterfrom
PeterDaveHello:fix-site-adapter-wait-timeout
Open

Wait longer for search result containers#1016
PeterDaveHello wants to merge 1 commit into
ChatGPTBox-dev:masterfrom
PeterDaveHello:fix-site-adapter-wait-timeout

Conversation

@PeterDaveHello

@PeterDaveHello PeterDaveHello commented Jul 18, 2026

Copy link
Copy Markdown
Member

Summary

  • Give Brave and DuckDuckGo result containers five seconds to appear instead of five milliseconds.
  • Let Brave continue immediately when its main results container is already available as a sidebar fallback.
  • Centralize the site-adapter wait policy behind a fixed-timeout helper.
  • Clear the pending timeout after a mutation finds the element.
  • Add deterministic timing, fallback, and cleanup regression coverage.

Why

The adapters passed 5 directly to a helper that uses setTimeout, so their intended rendering wait expired after only 5 ms. This change narrows the fix to the initial container wait and does not change Bilibili's no-timeout behavior.

Validation

  • npm test (46 test files)
  • npm run test:coverage on Node.js 22 (36.26% lines)
  • npm run lint
  • npm run build
  • Required Chromium build artifacts verified
  • git diff --check
  • Manual browser smoke not run; it requires an interactive browser-extension session.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The element-waiting utility now performs immediate lookups, cleans up observers and timers, supports timed-out resolution, and exposes a five-second wrapper. Brave and DuckDuckGo use the wrapper, with unit tests covering lookup, mutation, timeout, and cleanup behavior.

Changes

Site adapter element waiting

Layer / File(s) Summary
Element wait utility and coverage
src/utils/wait-for-element-to-exist-and-select.mjs, tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs
The utility handles existing elements, mutation-based resolution, indefinite waits, timeout resolution, observer cleanup, timer cleanup, and the new five-second wrapper; tests cover these behaviors.
Site adapter integration
src/content-script/site-adapters/brave/index.mjs, src/content-script/site-adapters/duckduckgo/index.mjs
Brave and DuckDuckGo replace the direct helper call with waitForSiteAdapterElement while preserving their selector and initialization logic.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant BraveOrDuckDuckGo
  participant waitForSiteAdapterElement
  participant DOM
  BraveOrDuckDuckGo->>waitForSiteAdapterElement: wait for results container selector
  waitForSiteAdapterElement->>DOM: query selector and observe mutations
  DOM-->>waitForSiteAdapterElement: matching element or five-second timeout
  waitForSiteAdapterElement-->>BraveOrDuckDuckGo: element or null
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly reflects the main change: increasing the wait time for search result containers.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@qodo-code-review

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Fix site-adapter container wait by using a 5s timeout helper

🐞 Bug fix 🧪 Tests ✨ Enhancement 🕐 20-40 Minutes

Grey Divider

AI Description

• Fix Brave/DDG adapters waiting 5ms instead of intended 5s for result containers.
• Centralize adapter wait policy via a dedicated 5-second helper wrapper.
• Add unit tests for timeout behavior and proper timer cleanup on early resolve.
Diagram

graph TD
  A["Brave adapter"] --> C["waitForSiteAdapterElement"] --> D["waitForElementToExistAndSelect"] --> E{{"document.querySelector"}}
  B["DuckDuckGo adapter"] --> C
  D --> F{{"MutationObserver"}}
  D --> G{{"setTimeout/clearTimeout"}}
  T["Unit tests"] --> D
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Pass 5000ms explicitly from adapters
  • ➕ Smallest change surface (no new exported helper).
  • ➕ Keeps timeout policy close to each adapter.
  • ➖ Timeout policy becomes duplicated across adapters.
  • ➖ Easier to regress with a future magic-number mistake (e.g., 5 vs 5000).
2. Move timeout policy to config (per-site)
  • ➕ Allows tuning timeouts per adapter/site without code changes.
  • ➕ Makes the policy explicit and discoverable in one place.
  • ➖ More wiring and configuration complexity than needed for this bug.
  • ➖ Risk of over-generalizing when only a single fixed policy is desired.

Recommendation: The chosen approach (a dedicated waitForSiteAdapterElement wrapper) is a good balance: it fixes the ms-vs-s bug, centralizes the intended policy to avoid repetition, and keeps the core wait utility reusable (including the no-timeout behavior for other callers).

Files changed (4) +170 / -13

Enhancement (1) +17 / -9
wait-for-element-to-exist-and-select.mjsFix wait cleanup and add site-adapter 5s timeout wrapper +17/-9

Fix wait cleanup and add site-adapter 5s timeout wrapper

• Refactors the utility to reuse a single initial query, adds a finish() helper to consistently disconnect observers and clear pending timeouts, and introduces waitForSiteAdapterElement(selector) which applies a 5-second timeout policy for site adapters.

src/utils/wait-for-element-to-exist-and-select.mjs

Bug fix (2) +4 / -4
index.mjsUse fixed 5s adapter wait helper for Brave container +2/-2

Use fixed 5s adapter wait helper for Brave container

• Replaces the direct waitForElementToExistAndSelect(selector, 5) call with waitForSiteAdapterElement(selector), ensuring Brave waits 5 seconds rather than 5ms for the results/sidebar container to render.

src/content-script/site-adapters/brave/index.mjs

index.mjsUse fixed 5s adapter wait helper for DuckDuckGo results container +2/-2

Use fixed 5s adapter wait helper for DuckDuckGo results container

• Switches DuckDuckGo’s insert-at-top path to call waitForSiteAdapterElement instead of passing a 5ms timeout into the generic wait helper, preventing premature adapter initialization.

src/content-script/site-adapters/duckduckgo/index.mjs

Tests (1) +149 / -0
wait-for-element-to-exist-and-select.test.mjsAdd deterministic unit tests for DOM wait timing and cleanup +149/-0

Add deterministic unit tests for DOM wait timing and cleanup

• Introduces unit tests with a fake MutationObserver and mocked timers to validate immediate resolution, mutation-driven resolution, no-timeout behavior, timeout resolution to null, clearing a pending timeout on success, and the 5-second site-adapter policy.

tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs

@kilo-code-bot

kilo-code-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (4 files)
  • src/content-script/site-adapters/brave/index.mjs
  • src/content-script/site-adapters/duckduckgo/index.mjs
  • src/utils/wait-for-element-to-exist-and-select.mjs
  • tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs

Incremental review note: Re-verified against HEAD b8e84b9. The prior Codex concern about Brave blocking on a missing .sidebar for 5s is now resolved: the adapter waits on a compound selector .sidebar,#results, so it proceeds as soon as either container is present. The two remaining prior bot findings (missing .mjs extension on the brave util import; direct globalThis.setTimeout/clearTimeout mutation in tests) are carried forward from earlier reviewers and are not re-raised here.

Previous Review Summary (commit f982427)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit f982427)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (4 files)
  • src/content-script/site-adapters/brave/index.mjs
  • src/content-script/site-adapters/duckduckgo/index.mjs
  • src/utils/wait-for-element-to-exist-and-select.mjs
  • tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs

Reviewed by hy3:free · Input: 24.8K · Output: 3K · Cached: 232.4K

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the element selection logic by introducing a new helper function waitForSiteAdapterElement with a default 5-second timeout, replacing the direct usage of waitForElementToExistAndSelect in the Brave and DuckDuckGo site adapters. It also updates waitForElementToExistAndSelect to ensure pending timeouts are cleared and MutationObservers are disconnected correctly, and adds comprehensive unit tests. The review feedback suggests using explicit .mjs file extensions for imports in the Brave adapter to ensure ESM compatibility, and recommends using the built-in t.mock.method utility from node:test instead of directly mutating globalThis in the unit tests to prevent potential flakiness.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/content-script/site-adapters/brave/index.mjs
Comment thread tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs
Comment thread tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f9824274a7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/content-script/site-adapters/brave/index.mjs
@qodo-code-review

qodo-code-review Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Qodo Logo

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs (1)

54-55: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Mock clearTimeout alongside setTimeout to ensure proper timer cleanup.

Since the utility under test calls clearTimeout internally, it is best practice to mock it alongside setTimeout so that the mock timers are properly cleared from Node's mock timer registry. You can achieve this by simply omitting the apis argument, which defaults to mocking all timer functions (including setTimeout and clearTimeout).

Consider applying this change here and in the other tests (lines 83, 117, 135) that use t.mock.timers.enable({ apis: ['setTimeout'] }).

♻️ Proposed fix
-  t.mock.timers.enable({ apis: ['setTimeout'] })
+  t.mock.timers.enable()
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs` around lines
54 - 55, Update the mock timer setup in the waitForElementToExistAndSelect
tests, including the cases around lines 54, 83, 117, and 135, to enable mock
timers without restricting the apis argument. This must mock both setTimeout and
clearTimeout so the utility’s timer cleanup is tracked correctly.
src/content-script/site-adapters/brave/index.mjs (1)

1-1: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Ensure consistent import paths with explicit file extensions. These imports lack explicit .mjs extensions, which can cause module resolution issues in native ESM environments and creates inconsistency across the adapters.

  • src/content-script/site-adapters/brave/index.mjs#L1-L1: Append /index.mjs to the ../../../utils import path.
  • src/content-script/site-adapters/duckduckgo/index.mjs#L2-L2: Append .mjs to the ../index import path.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/content-script/site-adapters/brave/index.mjs` at line 1, Update the
import in src/content-script/site-adapters/brave/index.mjs at lines 1-1 to
reference ../../../utils/index.mjs, and update the import in
src/content-script/site-adapters/duckduckgo/index.mjs at lines 2-2 to append the
.mjs extension to ../index; preserve the imported symbols and behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/content-script/site-adapters/brave/index.mjs`:
- Line 1: Update the import in src/content-script/site-adapters/brave/index.mjs
at lines 1-1 to reference ../../../utils/index.mjs, and update the import in
src/content-script/site-adapters/duckduckgo/index.mjs at lines 2-2 to append the
.mjs extension to ../index; preserve the imported symbols and behavior.

In `@tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs`:
- Around line 54-55: Update the mock timer setup in the
waitForElementToExistAndSelect tests, including the cases around lines 54, 83,
117, and 135, to enable mock timers without restricting the apis argument. This
must mock both setTimeout and clearTimeout so the utility’s timer cleanup is
tracked correctly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 15407d6a-a7ea-4d41-86dd-5236fa8dbf47

📥 Commits

Reviewing files that changed from the base of the PR and between 2419486 and f982427.

📒 Files selected for processing (4)
  • src/content-script/site-adapters/brave/index.mjs
  • src/content-script/site-adapters/duckduckgo/index.mjs
  • src/utils/wait-for-element-to-exist-and-select.mjs
  • tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs

Give Brave and DuckDuckGo result containers five seconds to appear
instead of five milliseconds. Centralize that policy, clean up the
pending timer after a match, and cover observable timing behavior.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an unintended 5ms timeout in site-adapter container waits by introducing a centralized 5-second “site adapter wait” helper and updating Brave/DuckDuckGo adapters to use it. It also improves the underlying wait helper’s cleanup behavior and adds deterministic unit tests to prevent regressions.

Changes:

  • Refactor waitForElementToExistAndSelect to reuse the initially selected element, add a shared finish() path, and clear a pending timeout when a mutation match resolves.
  • Add waitForSiteAdapterElement() that standardizes the site-adapter container wait to 5 seconds.
  • Update Brave and DuckDuckGo adapters to use the new helper (including Brave’s compound-selector fallback behavior) and add unit tests covering timing, fallback selectors, and timeout cleanup.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
tests/unit/utils/wait-for-element-to-exist-and-select.test.mjs Adds deterministic unit coverage for immediate resolution, mutation-based resolution, timeout behavior, timeout cleanup, and the 5-second site-adapter policy.
src/utils/wait-for-element-to-exist-and-select.mjs Centralizes resolve/cleanup logic and introduces waitForSiteAdapterElement() with a fixed 5-second timeout.
src/content-script/site-adapters/duckduckgo/index.mjs Switches DuckDuckGo initialization wait to the standardized 5-second site-adapter helper.
src/content-script/site-adapters/brave/index.mjs Switches Brave initialization wait to the standardized helper and uses a compound selector to allow sidebar/results fallback behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit b8e84b9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants