mirror of
https://github.com/kiwix/libkiwix.git
synced 2025-06-28 05:49:35 +00:00
Mostly fixed external links in the viewer iframe
Before this fix clicking an external link in the viewer iframe had no effect (other than an error being reported in the browser dev tools console) because the attempt to navigate the top browser context was suppressed due to sandboxing - the click handling code changed the target of the link but navigating to that target was blocked. Now the click handler works as follows: 1. Changes the target of the link to the catch page only if the link is going to be opened in a new tab or window (in this case sandboxing restrictions do not apply). 2. Otherwise directly navigates the viewer window to external URL or the catch page. An unhandled scenario is opening an external link in a new tab/window via a middle click or context menu - such events cannot be intercepted and therefore there is no way of blocking external links accessed in the said way.
This commit is contained in:
@ -259,9 +259,10 @@ function matchingAncestorElement(el, context, selector) {
|
|||||||
|
|
||||||
const block_path = `${root}/catch/external`;
|
const block_path = `${root}/catch/external`;
|
||||||
|
|
||||||
function blockLink(target) {
|
function blockLink(url) {
|
||||||
const encodedHref = encodeURIComponent(target.href);
|
return viewerSettings.linkBlockingEnabled
|
||||||
target.setAttribute("href", block_path + "?source=" + encodedHref);
|
? block_path + "?source=" + encodeURIComponent(url)
|
||||||
|
: url;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isExternalUrl(url) {
|
function isExternalUrl(url) {
|
||||||
@ -278,9 +279,14 @@ function onClickEvent(e) {
|
|||||||
const target = matchingAncestorElement(e.target, iframeDocument, "a");
|
const target = matchingAncestorElement(e.target, iframeDocument, "a");
|
||||||
if (target !== null && "href" in target) {
|
if (target !== null && "href" in target) {
|
||||||
if ( isExternalUrl(target.href) ) {
|
if ( isExternalUrl(target.href) ) {
|
||||||
target.setAttribute("target", "_top");
|
const possiblyBlockedLink = blockLink(target.href);
|
||||||
if ( viewerSettings.linkBlockingEnabled ) {
|
if ( e.ctrlKey || e.shiftKey ) {
|
||||||
return blockLink(target);
|
// The link will be loaded in a new tab/window - update the link
|
||||||
|
// and let the browser handle the rest.
|
||||||
|
target.setAttribute("href", possiblyBlockedLink);
|
||||||
|
} else {
|
||||||
|
// Load the external URL in the viewer window (rather than iframe)
|
||||||
|
contentIframe.contentWindow.parent.location = possiblyBlockedLink;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ const ResourceCollection resources200Compressible{
|
|||||||
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/taskbar.css" },
|
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/taskbar.css" },
|
||||||
{ STATIC_CONTENT, "/ROOT%23%3F/skin/taskbar.css?cacheid=bbdaf425" },
|
{ STATIC_CONTENT, "/ROOT%23%3F/skin/taskbar.css?cacheid=bbdaf425" },
|
||||||
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/viewer.js" },
|
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/viewer.js" },
|
||||||
{ STATIC_CONTENT, "/ROOT%23%3F/skin/viewer.js?cacheid=725c95a2" },
|
{ STATIC_CONTENT, "/ROOT%23%3F/skin/viewer.js?cacheid=0c02871d" },
|
||||||
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/fonts/Poppins.ttf" },
|
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/fonts/Poppins.ttf" },
|
||||||
{ STATIC_CONTENT, "/ROOT%23%3F/skin/fonts/Poppins.ttf?cacheid=af705837" },
|
{ STATIC_CONTENT, "/ROOT%23%3F/skin/fonts/Poppins.ttf?cacheid=af705837" },
|
||||||
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/fonts/Roboto.ttf" },
|
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/fonts/Roboto.ttf" },
|
||||||
@ -312,7 +312,7 @@ R"EXPECTEDRESULT( <link type="text/css" href="./skin/taskbar.css?cacheid=bbda
|
|||||||
<link type="text/css" href="./skin/css/autoComplete.css?cacheid=08951e06" rel="Stylesheet" />
|
<link type="text/css" href="./skin/css/autoComplete.css?cacheid=08951e06" rel="Stylesheet" />
|
||||||
<script type="module" src="./skin/i18n.js?cacheid=2cf0f8c5" defer></script>
|
<script type="module" src="./skin/i18n.js?cacheid=2cf0f8c5" defer></script>
|
||||||
<script type="text/javascript" src="./skin/languages.js?cacheid=648526e1" defer></script>
|
<script type="text/javascript" src="./skin/languages.js?cacheid=648526e1" defer></script>
|
||||||
<script type="text/javascript" src="./skin/viewer.js?cacheid=725c95a2" defer></script>
|
<script type="text/javascript" src="./skin/viewer.js?cacheid=0c02871d" defer></script>
|
||||||
<script type="text/javascript" src="./skin/autoComplete.min.js?cacheid=1191aaaf"></script>
|
<script type="text/javascript" src="./skin/autoComplete.min.js?cacheid=1191aaaf"></script>
|
||||||
const blankPageUrl = root + "/skin/blank.html?cacheid=6b1fa032";
|
const blankPageUrl = root + "/skin/blank.html?cacheid=6b1fa032";
|
||||||
<img src="./skin/langSelector.svg?cacheid=00b59961">
|
<img src="./skin/langSelector.svg?cacheid=00b59961">
|
||||||
|
Reference in New Issue
Block a user