From 6cdc47eb62ccc99cccf0720e008ed09590a29a71 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Wed, 11 May 2022 15:48:22 +0530 Subject: [PATCH] Use real magnet link in download modal Previously, on clicking Magnet, we were redirecting to a different site: https://download.kiwix.org/zim/other/xyzBookWithDate.zim.magnet This had the real magnet link as page content Now we use the real magnet link in the href, thus not redirecting and starting the download right away. Fix #767 --- static/skin/index.js | 20 ++++++++++++++++---- test/server.cpp | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/static/skin/index.js b/static/skin/index.js index 5c435ac6b..62e6d5d6c 100644 --- a/static/skin/index.js +++ b/static/skin/index.js @@ -142,10 +142,21 @@ } } + async function getMagnetLink(downloadLink) { + const magnetUrl = downloadLink + '.magnet'; + const controller = new AbortController(); + setTimeout(() => controller.abort(), 5000); + const magnetLink = await fetch(magnetUrl, { signal: controller.signal }).then(response => { + return response.ok ? response.text() : ''; + }).catch((_error) => ''); + return magnetLink; + } + function insertModal(button) { const downloadLink = button.getAttribute('data-link'); - button.addEventListener('click', (event) => { + button.addEventListener('click', async (event) => { event.preventDefault(); + const magnetLink = await getMagnetLink(downloadLink); document.body.insertAdjacentHTML('beforeend', `