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
This commit is contained in:
Dave Vandyke 2023-11-16 14:41:07 +00:00 committed by GitHub
parent 8df6c3243c
commit c1d8f5908d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 3 deletions

View File

@ -13,6 +13,8 @@
* - Added missing event dispatcher functionality * - Added missing event dispatcher functionality
* - Corrected return type of `Ad.getUniversalAdIds()` * - Corrected return type of `Ad.getUniversalAdIds()`
* - Corrected typo in `UniversalAdIdInfo.getAdIdValue()` method name * - 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: * Related issue:
* - https://github.com/uBlockOrigin/uBlock-issues/issues/2158 * - https://github.com/uBlockOrigin/uBlock-issues/issues/2158
@ -412,6 +414,7 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
constructor() { constructor() {
super(); super();
this.volume = 1; this.volume = 1;
this._enablePreloading = false;
} }
collapse() {} collapse() {}
configureAdsManager() {} configureAdsManager() {}
@ -437,7 +440,11 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
getVolume() { getVolume() {
return this.volume; 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() { isCustomClickTrackingUsed() {
return false; return false;
} }
@ -457,13 +464,14 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
for (const type of [ for (const type of [
AdEvent.Type.LOADED, AdEvent.Type.LOADED,
AdEvent.Type.STARTED, AdEvent.Type.STARTED,
AdEvent.Type.CONTENT_RESUME_REQUESTED, AdEvent.Type.CONTENT_PAUSE_REQUESTED,
AdEvent.Type.AD_BUFFERING, AdEvent.Type.AD_BUFFERING,
AdEvent.Type.FIRST_QUARTILE, AdEvent.Type.FIRST_QUARTILE,
AdEvent.Type.MIDPOINT, AdEvent.Type.MIDPOINT,
AdEvent.Type.THIRD_QUARTILE, AdEvent.Type.THIRD_QUARTILE,
AdEvent.Type.COMPLETE, AdEvent.Type.COMPLETE,
AdEvent.Type.ALL_ADS_COMPLETED, AdEvent.Type.ALL_ADS_COMPLETED,
AdEvent.Type.CONTENT_RESUME_REQUESTED,
]) { ]) {
try { try {
this._dispatch(new ima.AdEvent(type)); this._dispatch(new ima.AdEvent(type));
@ -743,7 +751,10 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
constructor(type) { constructor(type) {
this.type = type; this.type = type;
} }
getAdsManager() { getAdsManager(c, settings) {
if (settings && settings.enablePreloading) {
manager._enablePreloading = true;
}
return manager; return manager;
} }
getUserRequestContext() { getUserRequestContext() {