From e087f1c82fff824f1e0312a8fa6c061fe0b3a77d Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 3 Feb 2025 18:58:56 +0400 Subject: [PATCH] If PDF viewer is not enabled, PDFs are downloaded Chrome on Android doesn't support displaying PDF documents inline so an attempt to load a PDF into the Kiwix viewer iframe fails in a way that may be confusing to the users. In such situations it is better to offer to the users to download the PDF file so that they can view it with a dedicated application. Making the clicked PDF link to open in a new tab/window achieves exactly that effect. --- static/skin/viewer.js | 7 ++++++- test/server.cpp | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/static/skin/viewer.js b/static/skin/viewer.js index 99ee06c4..75be6dc1 100644 --- a/static/skin/viewer.js +++ b/static/skin/viewer.js @@ -352,6 +352,10 @@ function linkShouldBeOpenedInANewWindow(linkElement, mouseEvent) { || mouseEvent.metaKey /* on Macs */; } +function goingToOpenALinkToAnUndisplayableResource(url) { + return !navigator.pdfViewerEnabled && url.pathname.endsWith('.pdf'); +} + function onClickEvent(e) { const iframeDocument = contentIframe.contentDocument; const target = matchingAncestorElement(e.target, iframeDocument, "a"); @@ -359,7 +363,8 @@ function onClickEvent(e) { const target_href = getRealHref(target); const target_url = new URL(target_href, iframeDocument.location); const isExternalAppUrl = urlMustBeHandledByAnExternalApp(target_url); - if ( isExternalAppUrl && !viewerSettings.linkBlockingEnabled ) { + if ( (isExternalAppUrl && !viewerSettings.linkBlockingEnabled) + || goingToOpenALinkToAnUndisplayableResource(target_url) ) { target.setAttribute("target", "_blank"); } diff --git a/test/server.cpp b/test/server.cpp index 8f0b1a3d..f00bb691 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -75,7 +75,7 @@ const ResourceCollection resources200Compressible{ { DYNAMIC_CONTENT, "/ROOT%23%3F/skin/taskbar.css" }, { STATIC_CONTENT, "/ROOT%23%3F/skin/taskbar.css?cacheid=80d56607" }, { DYNAMIC_CONTENT, "/ROOT%23%3F/skin/viewer.js" }, - { STATIC_CONTENT, "/ROOT%23%3F/skin/viewer.js?cacheid=fdec37a7" }, + { STATIC_CONTENT, "/ROOT%23%3F/skin/viewer.js?cacheid=787e61d4" }, { DYNAMIC_CONTENT, "/ROOT%23%3F/skin/fonts/Poppins.ttf" }, { STATIC_CONTENT, "/ROOT%23%3F/skin/fonts/Poppins.ttf?cacheid=af705837" }, { DYNAMIC_CONTENT, "/ROOT%23%3F/skin/fonts/Roboto.ttf" }, @@ -327,7 +327,7 @@ R"EXPECTEDRESULT( - + const blankPageUrl = root + "/skin/blank.html?cacheid=6b1fa032";