Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow writing into current document if prebid is loaded inside an iframe #1066

Merged
merged 4 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ $$PREBID_GLOBAL$$.renderAd = function (doc, id) {
var url = adObject.adUrl;
var ad = adObject.ad;

if (doc === document || adObject.mediaType === 'video') {
if ((doc === document && !utils.inIframe()) || adObject.mediaType === 'video') {
utils.logError(`Error trying to write ad. Ad render call ad id ${id} was prevented from writing to the main document.`);
} else if (ad) {
if (isSrcdocSupported(doc)) {
Expand Down
11 changes: 10 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,18 @@ export function adUnitsFilter(filter, bid) {
*/
export function isSrcdocSupported(doc) {
//Firefox is excluded due to https://bugzilla.mozilla.org/show_bug.cgi?id=1265961
return !!doc.defaultView && 'srcdoc' in doc.defaultView.frameElement && !/firefox/i.test(navigator.userAgent);
return doc.defaultView && doc.defaultView.frameElement &&
'srcdoc' in doc.defaultView.frameElement && !/firefox/i.test(navigator.userAgent);
}

export function cloneJson(obj) {
return JSON.parse(JSON.stringify(obj));
}

export function inIframe() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem here is we need to check a passed in window param, which we don't have. I think we have to change the renderAd API to accept the window instead of doc :(

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm; I would hate to break backwards compatibility. Suren and I will brainstorm some techniques to see if we can come up with a backwards compatible solution that still solves this problem, and if so we'll update the pull-request. Thanks!

try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
8 changes: 7 additions & 1 deletion test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ describe('Unit: Prebid Module', function () {
var adResponse = {};
var spyLogError = null;
var spyLogMessage = null;
var inIframe = true;

beforeEach(function () {
doc = {
Expand All @@ -535,13 +536,17 @@ describe('Unit: Prebid Module', function () {

spyLogError = sinon.spy(utils, 'logError');
spyLogMessage = sinon.spy(utils, 'logMessage');

inIframe = true;
sinon.stub(utils, "inIframe", () => inIframe);
});

afterEach(function () {
$$PREBID_GLOBAL$$._bidsReceived.splice($$PREBID_GLOBAL$$._bidsReceived.indexOf(adResponse), 1);
$$PREBID_GLOBAL$$._winningBids = [];
utils.logError.restore();
utils.logMessage.restore();
utils.inIframe.restore();
});

it('should require doc and id params', function () {
Expand Down Expand Up @@ -576,7 +581,8 @@ describe('Unit: Prebid Module', function () {
assert.ok(spyLogError.calledWith(error), 'expected error was logged');
});

it('should log an error when doc is document', () => {
it('should log an error when not in an iFrame', () => {
inIframe = false;
$$PREBID_GLOBAL$$.renderAd(document, bidId);
const error = 'Error trying to write ad. Ad render call ad id ' + bidId + ' was prevented from writing to the main document.';
assert.ok(spyLogError.calledWith(error), 'expected error was logged');
Expand Down