Skip to content

Commit

Permalink
Enhance location detection within util function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachel Joyce committed Feb 20, 2018
1 parent af86376 commit 02dc482
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
5 changes: 3 additions & 2 deletions modules/sovrnBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const spec = {
* @return object of parameters for Prebid AJAX request
*/
buildRequests: function(bidReqs) {
const loc = utils.getTopWindowLocation();
let sovrnImps = [];
let iv;
utils._each(bidReqs, function (bid) {
Expand All @@ -37,8 +38,8 @@ export const spec = {
id: utils.getUniqueIdentifierStr(),
imp: sovrnImps,
site: {
domain: window.location.host,
page: window.location.host + window.location.pathname + location.search + location.hash
domain: loc.host,
page: loc.host + loc.pathname + loc.search + loc.hash
}
};
if (iv) sovrnBidReq.iv = iv;
Expand Down
47 changes: 40 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,50 @@ export function parseGPTSingleSizeArray(singleSize) {
}
};

exports.getTopWindowLocation = function () {
let location;
export function getTopWindowLocation() {
if (this.inIframe()) {
return getIframeParentLoc();
} else {
return window.location;
}
}

const getIframeParentLoc = function() {
let loc = window.location;
try {
// force an exception in x-domain enviornments. #1509
window.top.location.toString();
location = window.top.location;
if (window.$sf) {
loc = window.document.referrer;
} else {
if (window.document.location && window.document.location.ancestorOrigins &&
window.document.location.ancestorOrigins.length >= 1) {
loc = window.document.location.ancestorOrigins[window.document.location.ancestorOrigins.length - 1];
} else if (window.document.location) {
loc = getNonWebKitIframeParentLoc();
}
}
loc = parseFullUrl(loc);
} catch (e) {
location = window.location;
this.logMessage('getTopParentLoc failure', e);
}
return loc;
};

const getNonWebKitIframeParentLoc = function() {
let referrerLoc = '';
let currentWindow;
do {
currentWindow = currentWindow ? currentWindow.parent : window;
if (currentWindow.document && currentWindow.document.referrer) {
referrerLoc = currentWindow.document.referrer;
}
}
while (currentWindow !== window.top);
return referrerLoc;
};

return location;
const parseFullUrl = function(locString) {
var match = locString.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
return match && {protocol: match[1], host: match[2], hostname: match[3], port: match[4], pathname: match[5], search: match[6], hash: match[7], href: locString};
};

exports.getTopWindowUrl = function () {
Expand Down

0 comments on commit 02dc482

Please sign in to comment.