-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Enhance location detection within utils #2167
Changes from 16 commits
c0c28d7
d7df108
0700e91
cf0ca21
f89135c
be091fd
af86376
c2f2350
4867c53
28f3e0e
497723b
f90b2e0
2e9e413
74ed7f9
1269a33
55284d5
3c2f8bc
fee1789
cd78968
68a8afa
35b3f4c
b2bc2a4
7da5620
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ import { config } from './config'; | |
import clone from 'just-clone'; | ||
import find from 'core-js/library/fn/array/find'; | ||
import includes from 'core-js/library/fn/array/includes'; | ||
import { parse } from './url'; | ||
|
||
var CONSTANTS = require('./constants'); | ||
|
||
var _loggingChecked = false; | ||
|
@@ -157,17 +159,47 @@ export function parseGPTSingleSizeArray(singleSize) { | |
} | ||
}; | ||
|
||
exports.getTopWindowLocation = function () { | ||
let location; | ||
export function getTopWindowLocation() { | ||
if (exports.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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the comment, not sure removing this is the correct thing to do, unless there's an equivalent fix in the new code i'm not seeing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I didn't realize that the exception wasn't thrown when trying to access window.top until I read that 1509 PR. Interesting. Thanks for pointing out. I added back in. |
||
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) { | ||
// force an exception in x-domain environments. #1509 | ||
window.top.location.toString(); | ||
loc = getNonWebKitIframeParentLoc(); | ||
} | ||
} | ||
loc = parse(loc); | ||
} catch (e) { | ||
location = window.location; | ||
this.logMessage('getTopParentLoc failure', e); | ||
} | ||
return loc; | ||
}; | ||
|
||
return location; | ||
const getNonWebKitIframeParentLoc = function() { | ||
let referrerLoc = ''; | ||
let currentWindow; | ||
do { | ||
currentWindow = currentWindow ? currentWindow.parent : window; | ||
if (currentWindow.document && currentWindow.document.referrer) { | ||
referrerLoc = currentWindow.document.referrer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. won't this return a string when an object is expected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returns a string to |
||
} | ||
} | ||
while (currentWindow !== window.top); | ||
return referrerLoc; | ||
}; | ||
|
||
exports.getTopWindowUrl = function () { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several modules rely on this function, changing it may introduce subtle differences. Any reason to not keep the existing
getTopWindowLocation
as is, and create a new function for this case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old function
getTopWindowLocation
either returns window.top.location or returns window.location.The new
getTopWindowLocation
returns window.location, window.top.location, window.document.referrer, window.document.location.ancestorOrigins[0], or an object that recreates the values within window.locationThe new version attempts to get the same info, just from additional objects.
I checked the modules, and I think this new version should fit all the current use cases.
I am not opposed to creating a new function if you’re worried about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks for the explanation. Changes to core require a second review, assigning that now