MOBILE-2650 utils: Fix links inside iframes created after page load

main
Albert Gasset 2018-10-08 09:49:41 +02:00
parent ff08501397
commit 82ca471c5b
1 changed files with 42 additions and 48 deletions

View File

@ -183,55 +183,49 @@ export class CoreIframeUtilsProvider {
return; return;
} }
const links = Array.from(contentDocument.querySelectorAll('a')); contentDocument.addEventListener('click', (event) => {
links.forEach((el: HTMLAnchorElement) => { if (event.defaultPrevented) {
const href = el.href; // Event already prevented by some other code.
return;
}
// Check that href is not null. // Find the link being clicked.
if (href) { let el = <Element> event.target;
const scheme = this.urlUtils.getUrlScheme(href); while (el && el.tagName !== 'A') {
if (scheme && scheme == 'javascript') { el = el.parentElement;
// Javascript links should be treated by the iframe's Javascript. }
// There's nothing to be done with these links, so they'll be ignored. if (!el || el.tagName !== 'A') {
return; return;
} else if (scheme && scheme != 'file' && scheme != 'filesystem') { }
// Scheme suggests it's an external resource, open it in browser. const link = <HTMLAnchorElement> el;
el.addEventListener('click', (e) => {
// If the link's already prevented by SCORM JS then we won't open it in browser. const scheme = this.urlUtils.getUrlScheme(link.href);
if (!e.defaultPrevented) { if (!link.href || (scheme && scheme == 'javascript')) {
e.preventDefault(); // Links with no URL and Javascript links are ignored.
if (!this.sitesProvider.isLoggedIn()) { return;
this.utils.openInBrowser(href); }
} else {
this.sitesProvider.getCurrentSite().openInBrowserWithAutoLoginIfSameSite(href); if (scheme && scheme != 'file' && scheme != 'filesystem') {
} // Scheme suggests it's an external resource, open it in browser.
} event.preventDefault();
}); if (!this.sitesProvider.isLoggedIn()) {
} else if (el.target == '_parent' || el.target == '_top' || el.target == '_blank') { this.utils.openInBrowser(link.href);
// Opening links with _parent, _top or _blank can break the app. We'll open it in InAppBrowser. } else {
el.addEventListener('click', (e) => { this.sitesProvider.getCurrentSite().openInBrowserWithAutoLoginIfSameSite(link.href);
// If the link's already prevented by SCORM JS then we won't open it in InAppBrowser. }
if (!e.defaultPrevented) { } else if (link.target == '_parent' || link.target == '_top' || link.target == '_blank') {
e.preventDefault(); // Opening links with _parent, _top or _blank can break the app. We'll open it in InAppBrowser.
this.utils.openFile(href).catch((error) => { event.preventDefault();
this.domUtils.showErrorModal(error); this.utils.openFile(link.href).catch((error) => {
}); this.domUtils.showErrorModal(error);
} });
}); } else if (this.platform.is('ios') && (!link.target || link.target == '_self')) {
} else if (this.platform.is('ios') && (!el.target || el.target == '_self')) { // In cordova ios 4.1.0 links inside iframes stopped working. We'll manually treat them.
// In cordova ios 4.1.0 links inside iframes stopped working. We'll manually treat them. event.preventDefault();
el.addEventListener('click', (e) => { if (element.tagName.toLowerCase() == 'object') {
// If the link's already prevented by SCORM JS then we won't treat it. element.setAttribute('data', link.href);
if (!e.defaultPrevented) { } else {
if (element.tagName.toLowerCase() == 'object') { element.setAttribute('src', link.href);
e.preventDefault();
element.setAttribute('data', href);
} else {
e.preventDefault();
element.setAttribute('src', href);
}
}
});
} }
} }
}); });