Skip to content

Commit be0b5f9

Browse files
authored
Dispatch Click to Load ready event again when necessary (#457)
The "ddg-ctp-ready" event is dispatched by the Click to Load content-scope-script, to let the surrogate scripts know when the feature is ready. Very occasionally that event is dispatched too early, and so the surrogate script misses it. Workaround that by dispatching the event again, if we see that a surrogate script finished loading _after_ page load has finished.
1 parent ec67e16 commit be0b5f9

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/features/click-to-load.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,16 @@ export default class ClickToLoad extends ContentFeature {
17021702
}
17031703
await afterPageLoad
17041704

1705+
// On some websites, the "ddg-ctp-ready" event is occasionally
1706+
// dispatched too early, before the listener is ready to receive it.
1707+
// To counter that, catch "ddg-ctp-surrogate-load" events dispatched
1708+
// _after_ page, so the "ddg-ctp-ready" event can be dispatched again.
1709+
window.addEventListener(
1710+
'ddg-ctp-surrogate-load', () => {
1711+
originalWindowDispatchEvent(createCustomEvent('ddg-ctp-ready'))
1712+
}
1713+
)
1714+
17051715
// Then wait for any in-progress element replacements, before letting
17061716
// the surrogate scripts know to start.
17071717
window.setTimeout(() => {

0 commit comments

Comments
 (0)