Skip to content

Fixed initialization in iframes#116

Merged
tg666 merged 1 commit intomainfrom
fix/iframes
Aug 8, 2025
Merged

Fixed initialization in iframes#116
tg666 merged 1 commit intomainfrom
fix/iframes

Conversation

@tg666
Copy link
Contributor

@tg666 tg666 commented Aug 7, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Prevented the widget from displaying inside iframes that share the same origin as the main window, avoiding multiple instances.
    • Deferred widget initialization until the page is fully loaded if necessary, ensuring proper display and functionality.

- The widget is now not displayed inside iframes if the iframe is displayed on the same origin as the main window. This prevents the widget from being displayed multiple times.
- The widget will wait for the `DOMContentLoaded` event to occur if `document.body` does not yet exist.
@tg666 tg666 self-assigned this Aug 7, 2025
@tg666 tg666 added the Bugfix Fix for a knowing/reported bug. label Aug 7, 2025
@coderabbitai
Copy link

coderabbitai bot commented Aug 7, 2025

Walkthrough

The changes update the widget initialization logic to prevent it from running in same-origin iframes and to defer setup until the DOM is ready if document.body is not yet available. The changelog was updated to reflect these fixes. No public API signatures were altered.

Changes

Cohort / File(s) Change Summary
Changelog Update
CHANGELOG.md
Updated "Unreleased" section to document fixes: prevents widget in same-origin iframes; defers init until DOM ready.
Widget Initialization Logic
src/CookieConsentWrapper.mjs
Modified initialization to only proceed immediately if document.body exists, otherwise defers until DOM ready.
Iframe Detection & Initialization Control
src/CookieConsentWrapperFactory.mjs
Added private #shouldInit() to prevent widget initialization in same-origin iframes; initialization now conditional.

Sequence Diagram(s)

sequenceDiagram
    participant Browser
    participant CookieConsentWrapperFactory
    participant CookieConsentWrapper

    Browser->>CookieConsentWrapperFactory: Load script
    CookieConsentWrapperFactory->>CookieConsentWrapperFactory: #shouldInit()
    alt Should initialize
        CookieConsentWrapperFactory->>CookieConsentWrapper: init(window, document)
        CookieConsentWrapper->>CookieConsentWrapper: Check config & document.body
        alt document.body exists
            CookieConsentWrapper->>CookieConsentWrapper: doInitCookieConsent()
        else document.body missing
            CookieConsentWrapper->>Browser: Wait for DOMContentLoaded
            Browser->>CookieConsentWrapper: DOMContentLoaded event
            CookieConsentWrapper->>CookieConsentWrapper: doInitCookieConsent()
        end
    else Should NOT initialize
        CookieConsentWrapperFactory-->>Browser: Skip initialization
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A widget waits, it bides its time,
No longer lost in iframe rhyme.
Only when the coast is clear,
And the DOM is truly here,
It hops to life—just as planned,
A clever fix by rabbit hand!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3f5957d and 9af5554.

📒 Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • src/CookieConsentWrapper.mjs (1 hunks)
  • src/CookieConsentWrapperFactory.mjs (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: the cookie-consent plugin is designed to run in the main window only, so cross-realm javascript conc...
Learnt from: tg666
PR: 68publishers/cookie-consent#112
File: src/Ui/Utils.mjs:1-4
Timestamp: 2025-06-17T11:45:57.270Z
Learning: The cookie-consent plugin is designed to run in the main window only, so cross-realm JavaScript concerns (like iframe Element constructor differences) do not apply to this codebase.

Applied to files:

  • src/CookieConsentWrapper.mjs
  • src/CookieConsentWrapperFactory.mjs
📚 Learning: in the cookie-consent project, the checkvisibility utility function intentionally only checks for di...
Learnt from: tg666
PR: 68publishers/cookie-consent#112
File: src/Ui/Utils.mjs:8-19
Timestamp: 2025-06-17T11:45:26.315Z
Learning: In the cookie-consent project, the checkVisibility utility function intentionally only checks for display:none and hidden attribute, avoiding visibility and opacity checks to prevent interference with CSS animations. The behavior should match the standard HTMLElement.checkVisibility() function with no provided arguments.

Applied to files:

  • src/CookieConsentWrapper.mjs
  • src/CookieConsentWrapperFactory.mjs
🔇 Additional comments (4)
CHANGELOG.md (1)

9-11: Well-documented changes.

The changelog entries clearly describe both fixes implemented in this PR: preventing duplicate widgets in same-origin iframes and deferring initialization when document.body is unavailable. The documentation is concise and informative.

src/CookieConsentWrapper.mjs (1)

382-382: Proper DOM readiness check added.

The addition of && document.body ensures that immediate initialization only occurs when both the configuration allows it and the DOM body is available. This prevents potential initialization issues when the script runs before the document body exists.

src/CookieConsentWrapperFactory.mjs (2)

42-44: Conditional initialization properly implemented.

The wrapper initialization is now correctly guarded by the #shouldInit() check, ensuring the widget only initializes when appropriate based on the iframe context.


50-60: Robust iframe detection logic.

The #shouldInit() method effectively prevents widget initialization in same-origin iframes while handling cross-origin restrictions gracefully:

  • window.self === window.top correctly identifies the main window
  • The hostname check provides additional validation for same-origin contexts
  • Try-catch properly handles cross-origin access exceptions

This implementation successfully prevents duplicate widget instances in same-origin iframes as intended.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/iframes

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@jelen07 jelen07 self-requested a review August 8, 2025 06:29
@tg666 tg666 merged commit 07026c5 into main Aug 8, 2025
2 checks passed
@tg666 tg666 deleted the fix/iframes branch August 8, 2025 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bugfix Fix for a knowing/reported bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant