Skip to content

Commit 8de47f2

Browse files
committed
Improve google-ima shim
Related issue: uBlockOrigin/uAssets#30134
1 parent 5127e17 commit 8de47f2

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/web_accessible_resources/google-ima.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*
1919
* Related issue:
2020
* - https://github.com/uBlockOrigin/uBlock-issues/issues/2158
21+
* - https://github.com/uBlockOrigin/uAssets/issues/30134
2122
*
2223
**/
2324

@@ -388,21 +389,23 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
388389
getVersion() {
389390
return VERSION;
390391
}
391-
requestAds(/*r, c*/) {
392+
requestAds(_r, _c) {
392393
// If autoplay is disabled and the page is trying to autoplay a tracking
393394
// ad, then IMA fails with an error, and the page is expected to request
394395
// ads again later when the user clicks to play.
395396
CheckCanAutoplay().then(
396397
() => {
397398
const { ADS_MANAGER_LOADED } = AdsManagerLoadedEvent.Type;
398-
this._dispatch(new ima.AdsManagerLoadedEvent(ADS_MANAGER_LOADED));
399+
this._dispatch(new ima.AdsManagerLoadedEvent(ADS_MANAGER_LOADED, _r, _c));
399400
},
400401
() => {
401402
const e = new ima.AdError(
402403
"adPlayError",
403404
1205,
404405
1205,
405-
"The browser prevented playback initiated without user interaction."
406+
"The browser prevented playback initiated without user interaction.",
407+
_r,
408+
_c
406409
);
407410
this._dispatch(new ima.AdErrorEvent(e));
408411
}
@@ -638,10 +641,12 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
638641
}
639642

640643
class AdError {
641-
constructor(type, code, vast, message) {
644+
constructor(type, code, vast, message, request, context) {
642645
this.errorCode = code;
643646
this.message = message;
644647
this.type = type;
648+
this.adsRequest = request;
649+
this.userRequestContext = context;
645650
this.vastErrorCode = vast;
646651
}
647652
getErrorCode() {
@@ -738,7 +743,7 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
738743
return this.error;
739744
}
740745
getUserRequestContext() {
741-
return {};
746+
return this.error?.userRequestContext || {};
742747
}
743748
}
744749
AdErrorEvent.Type = {
@@ -748,8 +753,10 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
748753
const manager = new AdsManager();
749754

750755
class AdsManagerLoadedEvent {
751-
constructor(type) {
756+
constructor(type, request, context) {
752757
this.type = type;
758+
this.adsRequest = request;
759+
this.userRequestContext = context;
753760
}
754761
getAdsManager(c, settings) {
755762
if (settings && settings.enablePreloading) {
@@ -758,7 +765,7 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
758765
return manager;
759766
}
760767
getUserRequestContext() {
761-
return {};
768+
return this.userRequestContext || {};
762769
}
763770
}
764771
AdsManagerLoadedEvent.Type = {

0 commit comments

Comments
 (0)