Skip to content
Merged
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
53 changes: 38 additions & 15 deletions www/core/directives/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@ angular.module('mm.core')

/**
* Intercept window.open in a frame and its subframes, shows an error modal instead.
* Search links (<a>) and open them in browser or InAppBrowser if needed.
*
* @param {DOMElement} element Element to treat.
* @return {Void}
*/
function interceptPopups(element) {
function treatFrame(element) {
if (element) {
// Redefine window.open in this element and sub frames, it might have been loaded already.
redefineWindowOpen(element);
// Treat links.
treatLinks(element);

element.on('load', function() {
// Element loaded, redefine window.open again.
// Element loaded, redefine window.open and treat links again.
redefineWindowOpen(element);
treatLinks(element);
});
}
}
Expand Down Expand Up @@ -96,11 +100,41 @@ angular.module('mm.core')
// Search sub frames.
angular.forEach(tags, function(tag) {
angular.forEach(contents.find(tag), function(subelement) {
interceptPopups(angular.element(subelement));
treatFrame(angular.element(subelement));
});
});
}

/**
* Search links (<a>) and open them in browser or InAppBrowser if needed.
*
* @param {DOMElement} element Element to treat.
* @return {Void}
*/
function treatLinks(element) {
var links = element.contents().find('a');
angular.forEach(links, function(el) {
var href = el.href;

// Check that href is not null.
if (href) {
if (href.indexOf('http') === 0) {
// Link has protocol http(s), open it in browser.
angular.element(el).on('click', function(e) {
e.preventDefault();
$mmUtil.openInBrowser(href);
});
} else if (el.target == '_parent' || el.target == '_top' || el.target == '_blank') {
// Opening links with _parent, _top or _blank can break the app. We'll open it in InAppBrowser.
angular.element(el).on('click', function(e) {
e.preventDefault();
$mmUtil.openInApp(href);
});
}
}
});
}

return {
restrict: 'E',
template: '<div class="iframe-wrapper"><iframe class="mm-iframe" ng-style="{\'width\': width, \'height\': height}" ng-src="{{src}}"></iframe></div>',
Expand All @@ -112,18 +146,7 @@ angular.module('mm.core')
scope.height = $mmUtil.formatPixelsSize(attrs.iframeHeight) || '100%';

var iframe = angular.element(element.find('iframe')[0]);
interceptPopups(iframe);
iframe.on('load', function() {
angular.forEach(iframe.contents().find('a'), function(el) {
var href = el.getAttribute('href');
if (href && href.indexOf('http') === 0) { // Check that href is not null.
angular.element(el).on('click', function(e) {
$mmUtil.openInBrowser(href);
e.preventDefault();
});
}
});
});
treatFrame(iframe);

}
};
Expand Down