Skip to content

Commit

Permalink
Utils: add isSafeFrameWindow(), canAccessWindowTop()
Browse files Browse the repository at this point in the history
  • Loading branch information
osazos committed May 14, 2024
1 parent 1604431 commit 802ef77
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const internal = {
createTrackPixelIframeHtml,
getWindowSelf,
getWindowTop,
canAccessWindowTop,
getWindowLocation,
insertUserSyncIframe,
insertElement,
Expand Down Expand Up @@ -180,6 +181,16 @@ export function getWindowLocation() {
return window.location;
}

export function canAccessWindowTop() {
try {
if (internal.getWindowTop().location.href) {
return true;
}
} catch (e) {
return false;
}
}

/**
* Wrappers to console.(log | info | warn | error). Takes N arguments, the same as the native methods
*/
Expand Down Expand Up @@ -620,6 +631,18 @@ export function inIframe() {
}
}

/**
* https://iabtechlab.com/wp-content/uploads/2016/03/SafeFrames_v1.1_final.pdf
*/
export function isSafeFrameWindow() {
if (!inIframe()) {
return false;
}

const ws = internal.getWindowSelf();
return !!(ws.$sf && ws.$sf.ext);
}

export function isSafariBrowser() {
return /^((?!chrome|android|crios|fxios).)*safari/i.test(navigator.userAgent);
}
Expand Down
40 changes: 40 additions & 0 deletions test/spec/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,46 @@ describe('Utils', function () {
type_array = 'Array',
type_function = 'Function';

describe('canAccessWindowTop', function () {
it('should return true if window.top is accessible', function () {
assert.equal(utils.canAccessWindowTop(), true);
});

it('should return false if window.top is not accessible', function () {
const stubGetWindowTop = sinon.stub(utils.internal, 'getWindowTop').throws();
assert.equal(utils.canAccessWindowTop(), false);
stubGetWindowTop.restore();
});
});

describe('isSafeFrameWindow', function () {
// SafeFrames implementation
// https://iabtechlab.com/wp-content/uploads/2016/03/SafeFrames_v1.1_final.pdf
const $sf = {
ext: {
geom: function() {}
}
};

afterEach(function() {
delete window.$sf;
})

it('should return true if window.$sf is accessible', function () {
window.$sf = $sf;
assert.equal(utils.isSafeFrameWindow(), true);
});

it('should return false if window.$sf is missimplemented', function () {
window.$sf = {};
assert.equal(utils.isSafeFrameWindow(), false);
});

it('should return false if window.$sf is missing', function () {
assert.equal(utils.isSafeFrameWindow(), false);
});
});

describe('getBidIdParameter', function () {
it('should return value of the key in input object', function () {
var obj = {
Expand Down

0 comments on commit 802ef77

Please sign in to comment.