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

Update window.js to support iframes #144

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
30 changes: 27 additions & 3 deletions lib/services/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,36 @@ windowService._setMessageSource(domEmitterSource);

module.exports = windowService;


_.extend(windowService, {
isTop: windowService_isTop
isTop: windowService_isTop,
getTop: windowService_getTop
});

/**
* Gets the top window containing a milo instance.
* It is not possible to use `window.top` if the application is contained
* in an iframe being served from a different domain (Trying to access
* properties of the top window in this case would breach the browser's
* cross-origin iframe policy)
*/
function windowService_getTop() {
var topWindow = window;

while (true) {
try {
var parentWindow = topWindow.parent;
// Trying to access this property outside of the same domain will throw an error
var milo = parentWindow.milo;
if (!milo) return topWindow; // No more milo

topWindow = parentWindow;
} catch (_e) {
return topWindow;
Copy link
Member

Choose a reason for hiding this comment

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

My favourite line

}
}
}

function windowService_isTop() {
return window.top == window.self || window.__karma__;
const topMiloWindow = windowService_getTop();
return topMiloWindow == window.self || window.__karma__;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"brfs": "^1.4.3",
"browserify": "^14.0.0",
"eslintify": "^3.1.0",
"grunt": "^1.0.1",
"grunt": "1.0.1",
"grunt-browserify": "^5.0.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-uglify": "^2.0.0",
Expand Down