From c1d8f5908de1155c411a19f5809096f629344e29 Mon Sep 17 00:00:00 2001 From: Dave Vandyke Date: Thu, 16 Nov 2023 14:41:07 +0000 Subject: [PATCH] More improvements to the google-ima shim script (#3908) We have enabled the google-ima shim script again in the DuckDuckGo Privacy Essentials browser extension, and found a couple more issues: - Some websites set the enablePreloading[1] option, which should cause[2] the AdsManager.init() method to trigger the LOADED AdEvent to fire. If the event doesn't fire, those websites can get stuck waiting for the event forever. - When AdsManager.start() method is called, a bunch of events are dispatched in order, to simulate ads loading, playing and finishing. There was a mistake in that logic though. The CONTENT_PAUSE_REQUESTED and CONTENT_RESUME_REQUESTED events[3] should fire as the ads start and finish respectively. By firing the latter early, and skipping the former, some websites got confused and tried to display ad overlays at the same time as playing their content, or didn't display they content at all. 1 - https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/reference/js/google.ima.AdsRenderingSettings#enablePreloading 2 - https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/preload#timing 3 - https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/reference/js/google.ima.AdEvent --- src/web_accessible_resources/google-ima.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/web_accessible_resources/google-ima.js b/src/web_accessible_resources/google-ima.js index 4f7b49d65e9fe..4556516422de2 100644 --- a/src/web_accessible_resources/google-ima.js +++ b/src/web_accessible_resources/google-ima.js @@ -13,6 +13,8 @@ * - Added missing event dispatcher functionality * - Corrected return type of `Ad.getUniversalAdIds()` * - Corrected typo in `UniversalAdIdInfo.getAdIdValue()` method name + * - Corrected dispatch of LOAD event when preloading is enabled + * - Corrected dispatch of CONTENT_PAUSE/RESUME_REQUESTED events * * Related issue: * - https://github.com/uBlockOrigin/uBlock-issues/issues/2158 @@ -412,6 +414,7 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) { constructor() { super(); this.volume = 1; + this._enablePreloading = false; } collapse() {} configureAdsManager() {} @@ -437,7 +440,11 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) { getVolume() { return this.volume; } - init(/*w, h, m, e*/) {} + init(/*w, h, m, e*/) { + if (this._enablePreloading) { + this._dispatch(new ima.AdEvent(AdEvent.Type.LOADED)); + } + } isCustomClickTrackingUsed() { return false; } @@ -457,13 +464,14 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) { for (const type of [ AdEvent.Type.LOADED, AdEvent.Type.STARTED, - AdEvent.Type.CONTENT_RESUME_REQUESTED, + AdEvent.Type.CONTENT_PAUSE_REQUESTED, AdEvent.Type.AD_BUFFERING, AdEvent.Type.FIRST_QUARTILE, AdEvent.Type.MIDPOINT, AdEvent.Type.THIRD_QUARTILE, AdEvent.Type.COMPLETE, AdEvent.Type.ALL_ADS_COMPLETED, + AdEvent.Type.CONTENT_RESUME_REQUESTED, ]) { try { this._dispatch(new ima.AdEvent(type)); @@ -743,7 +751,10 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) { constructor(type) { this.type = type; } - getAdsManager() { + getAdsManager(c, settings) { + if (settings && settings.enablePreloading) { + manager._enablePreloading = true; + } return manager; } getUserRequestContext() {