Skip to content

Commit

Permalink
More improvements to the google-ima shim script (#3908)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kzar authored Nov 16, 2023
1 parent 8df6c32 commit c1d8f59
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/web_accessible_resources/google-ima.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -412,6 +414,7 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
constructor() {
super();
this.volume = 1;
this._enablePreloading = false;
}
collapse() {}
configureAdsManager() {}
Expand All @@ -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;
}
Expand All @@ -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));
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit c1d8f59

Please sign in to comment.