From 8de47f250de3e7a1383443290f0fbf82a87595b9 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 22 Sep 2025 10:41:36 -0400 Subject: [PATCH] Improve `google-ima` shim Related issue: https://github.com/uBlockOrigin/uAssets/issues/30134 --- src/web_accessible_resources/google-ima.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/web_accessible_resources/google-ima.js b/src/web_accessible_resources/google-ima.js index 455651642..181b7451e 100644 --- a/src/web_accessible_resources/google-ima.js +++ b/src/web_accessible_resources/google-ima.js @@ -18,6 +18,7 @@ * * Related issue: * - https://github.com/uBlockOrigin/uBlock-issues/issues/2158 + * - https://github.com/uBlockOrigin/uAssets/issues/30134 * **/ @@ -388,21 +389,23 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) { getVersion() { return VERSION; } - requestAds(/*r, c*/) { + requestAds(_r, _c) { // If autoplay is disabled and the page is trying to autoplay a tracking // ad, then IMA fails with an error, and the page is expected to request // ads again later when the user clicks to play. CheckCanAutoplay().then( () => { const { ADS_MANAGER_LOADED } = AdsManagerLoadedEvent.Type; - this._dispatch(new ima.AdsManagerLoadedEvent(ADS_MANAGER_LOADED)); + this._dispatch(new ima.AdsManagerLoadedEvent(ADS_MANAGER_LOADED, _r, _c)); }, () => { const e = new ima.AdError( "adPlayError", 1205, 1205, - "The browser prevented playback initiated without user interaction." + "The browser prevented playback initiated without user interaction.", + _r, + _c ); this._dispatch(new ima.AdErrorEvent(e)); } @@ -638,10 +641,12 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) { } class AdError { - constructor(type, code, vast, message) { + constructor(type, code, vast, message, request, context) { this.errorCode = code; this.message = message; this.type = type; + this.adsRequest = request; + this.userRequestContext = context; this.vastErrorCode = vast; } getErrorCode() { @@ -738,7 +743,7 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) { return this.error; } getUserRequestContext() { - return {}; + return this.error?.userRequestContext || {}; } } AdErrorEvent.Type = { @@ -748,8 +753,10 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) { const manager = new AdsManager(); class AdsManagerLoadedEvent { - constructor(type) { + constructor(type, request, context) { this.type = type; + this.adsRequest = request; + this.userRequestContext = context; } getAdsManager(c, settings) { if (settings && settings.enablePreloading) { @@ -758,7 +765,7 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) { return manager; } getUserRequestContext() { - return {}; + return this.userRequestContext || {}; } } AdsManagerLoadedEvent.Type = {