diff --git a/CHANGELOG.md b/CHANGELOG.md index c02ba7f3..4de48bcd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- issue with overwriting `google.ima` value if it was already set + [#331](https://github.com/AdguardTeam/Scriptlets/issues/331) - issue with printing unnecessary logs to the console in `log-addEventListener` scriptlet [#335](https://github.com/AdguardTeam/Scriptlets/issues/335) - error throwing in `prevent-fetch` and `prevent-xhr` scriptlets when a request is blocked diff --git a/src/redirects/google-ima3.js b/src/redirects/google-ima3.js index d97f2607..ef847274 100644 --- a/src/redirects/google-ima3.js +++ b/src/redirects/google-ima3.js @@ -479,6 +479,15 @@ export function GoogleIma3(source) { window.google = {}; } + // Workaround for https://github.com/AdguardTeam/Scriptlets/issues/331 + // To avoid conflicts with the DAI SDK, we need to make sure that the + // google.ima.dai namespace is not overwritten. + // TODO: Later we should create a mock for the DAI SDK as well. + // See https://github.com/AdguardTeam/Scriptlets/issues/239 + if (window.google.ima?.dai) { + ima.dai = window.google.ima.dai; + } + window.google.ima = ima; hit(source); diff --git a/tests/redirects/google-ima3.test.js b/tests/redirects/google-ima3.test.js index 23f7a8a3..0e7d8815 100644 --- a/tests/redirects/google-ima3.test.js +++ b/tests/redirects/google-ima3.test.js @@ -87,3 +87,30 @@ test('Ima - run requestAds function twice', (assert) => { done(); }); }); + +// https://github.com/AdguardTeam/Scriptlets/issues/331 +test('Ima - check if DAI API is not discarded by the redirect', (assert) => { + // Define some dummy DAI API which cannot be discarded by the redirect + window.google = { + ima: { + // should be kept + dai: { + api: { + foo: 'bar', + }, + }, + // should be discarded + bar: 'foo', + }, + }; + + runRedirect(name); + + // check if DAI API is not discarded by the redirect + assert.ok(window.google.ima.dai, 'window.google.ima.dai exists'); + assert.ok(window.google.ima.dai.api, 'window.google.ima.dai.api exists'); + assert.strictEqual(window.google.ima.dai.api.foo, 'bar', 'window.google.ima.dai.api.foo is equal to "bar"'); + + // other properties should be discarded + assert.strictEqual(window.google.ima.bar, undefined, 'window.google.ima.bar is undefined'); +});