Skip to content

Commit

Permalink
improve ga redirect push function #184 AG-12372
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/scriptlets from fix/AG-12372_01 to release/v1.6

Squashed commit of the following:

commit 4d6e667
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Wed Feb 9 19:45:50 2022 +0300

    revert .length assertion

commit 7fa416a
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Wed Feb 9 18:29:13 2022 +0300

    improve ga redirect
  • Loading branch information
stanislav-atr committed Feb 9, 2022
1 parent 02b5c45 commit 9af886f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/redirects/googlesyndication-adsbygoogle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@ export function GoogleSyndicationAdsByGoogle(source) {
// length: 0,
loaded: true,
// https://github.com/AdguardTeam/Scriptlets/issues/184
push(arg) { // eslint-disable-line no-unused-vars
push(arg) {
if (typeof this.length === 'undefined') {
this.length = 0;
this.length += 1;
}
if (arg !== null && arg instanceof Object && arg.constructor.name === 'Object') {
// eslint-disable-next-line no-restricted-syntax
for (const key of Object.keys(arg)) {
if (typeof arg[key] === 'function') {
try {
arg[key].call();
} catch {
/* empty */
}
}
}
}
},
};
const adElems = document.querySelectorAll('.adsbygoogle');
Expand Down
12 changes: 8 additions & 4 deletions tests/redirects/googlesyndication-adsbygoogle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ test('Checking if alias name works', (assert) => {

test('Redirect testing', (assert) => {
const ad = createAdElement();

runRedirect(name);

// check if iframes were created by scriptlet
Expand All @@ -70,9 +69,14 @@ test('Redirect testing', (assert) => {

assert.strictEqual(window.adsbygoogle.length, undefined, 'adsbygoogle.length check');
assert.strictEqual(window.adsbygoogle.push.length, 1, 'push.length check');

// check if API was mocked
window.adsbygoogle.push('somedata');
const pushCallback = () => {
assert.ok(true, 'callback was called');
};
const pushArg = {
test: 'test',
callback: pushCallback,
};
window.adsbygoogle.push(pushArg);
assert.strictEqual(window.adsbygoogle.length, 1, 'API was mocked');

removeBodyElement(ad);
Expand Down

0 comments on commit 9af886f

Please sign in to comment.