From 34239163af8c96b1fc2831281bb9e6e3f9ffeb5e Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Sun, 3 Dec 2023 14:35:15 +0000 Subject: [PATCH] Fix opening of new Zimit tabs to local articles and PDFs (#1176) --- www/js/app.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/www/js/app.js b/www/js/app.js index b74da211..041a77c5 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -2022,8 +2022,11 @@ function handleClickOnReplayLink (ev, anchor) { var pseudoNamespace = selectedArchive.zimitPrefix.replace(/^(.*\/)[^/]{2,}\/$/, '$1'); var pseudoDomainPath = anchor.hostname + anchor.pathname; var containingDocDomainPath = anchor.ownerDocument.location.hostname + anchor.ownerDocument.location.pathname; - // If the paths are identical, then we are dealing with a link to an anchor in the same document, so we can return - if (anchor.protocol !== document.location.protocol || pseudoDomainPath === containingDocDomainPath) return; + // If it's for a different protocol (e.g. javascript:) we should let Replay handle that, or if the paths are identical, then we are dealing + // with a link to an anchor in the same document, or if the user has pressed the ctrl or command key, the document will open in a new window + // anyway, so we can return. Note that some PDFs are served with a protocol of http: instead of https:, so we need to account for that. + if (anchor.protocol.replace(/s:/, ':') !== document.location.protocol.replace(/s:/, ':') || pseudoDomainPath === containingDocDomainPath || + ev.ctrlKey || ev.metaKey || ev.button === 1) return; var zimUrl = pseudoNamespace + pseudoDomainPath + anchor.search; // We are dealing with a ZIM link transformed by Wombat, so we need to reconstruct the ZIM link if (zimUrl) { @@ -2031,11 +2034,12 @@ function handleClickOnReplayLink (ev, anchor) { ev.stopPropagation(); selectedArchive.getDirEntryByPath(zimUrl).then(function (dirEntry) { if (dirEntry) { + var pathToArticleDocumentRoot = document.location.href.replace(/www\/index\.html.*$/, selectedArchive.file.name + '/'); var mimetype = dirEntry.getMimetype(); // Due to the iframe sandbox, we have to prevent the PDF viewer from opening in the iframe and instead open it in a new tab // Note that some Replay PDFs have html mimetypes, or can be redirects to PDFs, we need to check the URL as well - if (/pdf/i.test(mimetype) || /\.pdf(?:[#?]|$)/i.test(anchor.href)) { - window.open(anchor.href, '_blank'); + if (/pdf/i.test(mimetype) || /\.pdf(?:[#?]|$)/i.test(anchor.href) || /\.pdf(?:[#?]|$)/i.test(dirEntry.url)) { + window.open(pathToArticleDocumentRoot + zimUrl, '_blank'); /* } else if (/\bx?html\b/i.test(mimetype)) { // If the SW has gone to sleep, loading this way gives it a chance to reload configuration @@ -2044,9 +2048,9 @@ function handleClickOnReplayLink (ev, anchor) { } else { // Fingers crossed, let Replay handle this link anchor.passthrough = true; - // Handle middle-clicks and ctrl-clicks + // Handle middle-clicks and ctrl-clicks (these should be filtered out above, but...) if (ev.ctrlKey || ev.metaKey || ev.button === 1) { - window.open(anchor.href, '_blank'); + window.open(pathToArticleDocumentRoot + zimUrl, '_blank'); } else { anchor.click(); }