From 2d31b84a391f5684c7ad4521da618fd42fb3ebec Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Sun, 11 Feb 2024 16:21:34 +0000 Subject: [PATCH] Fix untransformed relative zimit2 URLs without wombat (#569) --- www/js/app.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/www/js/app.js b/www/js/app.js index 20b4cb98..ee62b1e3 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -5217,6 +5217,10 @@ function handleClickOnReplayLink (ev, anchor) { // If it starts with the path to the ZIM file, then we are dealing with an untransformed absolute local ZIM link if (!anchor.href.indexOf(pathToZim)) { zimUrl = anchor.href.replace(pathToZim, ''); + // If it is the same as the pseudoDomainPath, then we are dealing with an untransformed pseuodo relative link that looks like an absolute https:// link + // (this probably only applies to zimit2 without Wombat) + } else if (anchor.href.replace(/^[^:]+:\/\//, '') === pseudoDomainPath && /\.zim\/[CA]\//.test(anchor.href)) { + zimUrl = anchor.href.replace(/^(?:[^.]|\.(?!zim\/[CA]\/))+\.zim\//, ''); } else { zimUrl = pseudoNamespace + pseudoDomainPath + anchor.search; } @@ -5226,7 +5230,7 @@ function handleClickOnReplayLink (ev, anchor) { ev.stopPropagation(); // Note that true in the fourth argument instructs getDirEntryByPath to follow redirects by looking up the Header return appstate.selectedArchive.getDirEntryByPath(zimUrl, null, null, true).then(function (dirEntry) { - if (dirEntry) { + var processDirEntry = function (dirEntry) { var pathToArticleDocumentRoot = document.location.href.replace(/www\/index\.html.*$/, appstate.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 @@ -5318,13 +5322,29 @@ function handleClickOnReplayLink (ev, anchor) { } } } + }; + if (dirEntry) { + processDirEntry(dirEntry); } else { - // If dirEntry was not-found, it's probably an external link, so warn user before opening a new tab/window - uiUtil.warnAndOpenExternalLinkInNewTab(null, anchor); + // If URL has final slash, we need to try it without the slash + if (/\/$/.test(zimUrl)) { + zimUrl = zimUrl.replace(/\/$/, ''); + return appstate.selectedArchive.getDirEntryByPath(zimUrl).then(function (dirEntry) { + if (dirEntry) { + processDirEntry(dirEntry); + } else { + // If dirEntry was still not-found, it's probably an external link, so warn user before opening a new tab/window + uiUtil.warnAndOpenExternalLinkInNewTab(null, anchor); + } + }); + } else { + // It's probably an external link, so warn user before opening a new tab/window + uiUtil.warnAndOpenExternalLinkInNewTab(null, anchor); + } } }).catch(function (err) { console.error('Error getting dirEntry for ' + zimUrl, err); - appstate.isReplayWorkerAvailable = true; + uiUtil.systemAlert('There was an error looking up ' + zimUrl, 'Error reading direcotry entry!'); }); } }