tscanlin / tocbot

Build a table of contents from headings in an HTML document.
MIT License
1.37k stars 114 forks source link

Make tocbot work inside iframes #307

Closed felipemarcos closed 11 months ago

felipemarcos commented 11 months ago

Problem

Screenshot 2023-07-31 at 16 47 34

The instanceof window.HTMLElement statement returns false when inside an iframe element in very specific situations, this causes the script to stop working thus rendering a blank table of contents.

Solution

I've added instanceof window.parent.HTMLElement to check if an element was created by the parent document. This check only works in iframes in the same domain, so I wrapped it in a try-catch block to prevent errors.

tscanlin commented 11 months ago

Thanks for the fix / PR!!

tscanlin commented 11 months ago

This should be published now. Thanks again! 📦 tocbot@4.21.1