Skip to content

Commit

Permalink
Improve google-ima3
Browse files Browse the repository at this point in the history
Do not overwrite google.ima if it was already set
  • Loading branch information
AdamWr committed Jul 17, 2023
1 parent 541473c commit 6f3dce4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/redirects/google-ima3.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import {
export function GoogleIma3(source) {
const VERSION = '3.453.0';

const ima = {};

// 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 AdDisplayContainer = function () { };
AdDisplayContainer.prototype.destroy = noopFunc;
AdDisplayContainer.prototype.initialize = noopFunc;
Expand Down
33 changes: 33 additions & 0 deletions tests/redirects/google-ima3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,36 @@ test('Ima - run requestAds function twice', (assert) => {
done();
});
});

test('Ima - check if google.ima.dai was NOT overwritten', (assert) => {
// Test for https://github.com/AdguardTeam/Scriptlets/issues/331
const done = assert.async();

window.google = {
ima: {
dai: {
api: {
StreamManager() { },
},
},
},
};

runRedirect(name);

let number = 0;
const test = () => {
number += 1;
};
const { ima } = window.google;
const AdsLoader = new ima.AdsLoader();
AdsLoader.addEventListener(ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, test);

assert.ok(typeof window.google.ima.dai.api.StreamManager === 'function',
'window.google.ima.dai.api.StreamManager was not overwritten');
AdsLoader.requestAds();
requestAnimationFrame(() => {
assert.strictEqual(number, 1, 'number is equal to 1');
done();
});
});

0 comments on commit 6f3dce4

Please sign in to comment.