Skip to content

Commit

Permalink
Improve 'google-ima3'
Browse files Browse the repository at this point in the history
Move window.google.ima.dai check at the end of GoogleIma3 function
  • Loading branch information
AdamWr committed Jul 18, 2023
1 parent d9e49ed commit ece2529
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/redirects/google-ima3.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import {
export function GoogleIma3(source) {
const VERSION = '3.453.0';

// Do not overwrite window.google.ima if it was already set
// https://github.com/AdguardTeam/Scriptlets/issues/331
const ima = typeof window.google?.ima === 'object' ? window.google.ima : {};
const ima = {};
const AdDisplayContainer = function () { };
AdDisplayContainer.prototype.destroy = noopFunc;
AdDisplayContainer.prototype.initialize = noopFunc;
Expand Down Expand Up @@ -480,6 +478,14 @@ export function GoogleIma3(source) {
window.google = {};
}

// To avoid conflicts with the DAI SDK, we need to make sure that the
// google.ima.dai namespace is not overwritten.
// https://github.com/AdguardTeam/Scriptlets/issues/331
if (window.google.ima?.dai) {
// TODO: Later we should create a mock for the DAI SDK as well.
ima.dai = window.google.ima.dai;
}

window.google.ima = ima;

hit(source);
Expand Down
8 changes: 8 additions & 0 deletions tests/redirects/google-ima3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ test('Ima - check if google.ima.dai was NOT overwritten', (assert) => {
StreamManager() { },
},
},
shouldBeDiscarded: true,
},
};

Expand All @@ -116,6 +117,13 @@ test('Ima - check if google.ima.dai was NOT overwritten', (assert) => {
typeof window.google.ima.dai.api.StreamManager === 'function',
'window.google.ima.dai.api.StreamManager was not overwritten',
);

// Only google.ima.dai should not be overwritten
assert.strictEqual(
window.google.ima.shouldBeDiscarded, undefined,
'google.ima.shouldBeDiscarded is undefined',
);

AdsLoader.requestAds();
requestAnimationFrame(() => {
assert.strictEqual(number, 1, 'number is equal to 1');
Expand Down

0 comments on commit ece2529

Please sign in to comment.