diff --git a/static/resources_list.txt b/static/resources_list.txt
index c52095cad..779eada01 100644
--- a/static/resources_list.txt
+++ b/static/resources_list.txt
@@ -24,6 +24,7 @@ skin/fonts/Poppins.ttf
skin/fonts/Roboto.ttf
skin/search_results.css
skin/blank.html
+skin/download.js
skin/polyfills.js
skin/viewer.js
skin/i18n.js
diff --git a/static/skin/download.js b/static/skin/download.js
new file mode 100644
index 000000000..59ae2af05
--- /dev/null
+++ b/static/skin/download.js
@@ -0,0 +1,119 @@
+function downloadButtonText(zimSize) {
+ return $t("download") + (zimSize ? ` - ${zimSize}`: '');
+}
+
+/* hack for library.kiwix.org magnet links (created by MirrorBrain)
+ See https://github.com/kiwix/container-images/issues/242 */
+async function getFixedMirrorbrainMagnet(magnetLink) {
+ // parse as query parameters
+ const params = new URLSearchParams(
+ magnetLink.replaceAll('&', '&').replace(/^magnet:/, ''));
+
+ const zimUrl = params.get('as'); // as= is fallback URL
+ // download metalink to build list of mirrored URLs
+ let mirrorUrls = [];
+
+ const metalink = await fetch(`${zimUrl}.meta4`).then(response => {
+ return response.ok ? response.text() : '';
+ }).catch((_error) => '');
+
+ if (metalink) {
+ try {
+ const parser = new DOMParser();
+ const doc = parser.parseFromString(metalink, "application/xml");
+ doc.querySelectorAll("url").forEach((node) => {
+ if (node.hasAttribute("priority")) { // ensures its a mirror link
+ mirrorUrls.push(node.innerHTML);
+ }
+ });
+ } catch (err) {
+ // not a big deal, magnet will only contain primary URL
+ console.debug(`Failed to parse mirror links for ${zimUrl}`);
+ }
+ }
+
+ // set webseed (ws=) URL to primary download URL (redirects to mirror)
+ params.set('ws', zimUrl);
+ // if we got metalink mirror URLs, append them all
+ if (mirrorUrls) {
+ mirrorUrls.forEach((url) => {
+ params.append('ws', url);
+ });
+ }
+
+ params.set('xs', `${zimUrl}.torrent`); // adding xs= to point to torrent URL
+
+ return 'magnet:?' + makeURLSearchString(params, ['ws', 'as', 'dn', 'xs', 'tr']);
+}
+
+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) => '');
+ if (magnetLink) {
+ return await getFixedMirrorbrainMagnet(magnetLink);
+ }
+ return magnetLink;
+}
+
+function insertDownloadZimModal(button, downloadLink, downloadSize, root) {
+ button.addEventListener('click', async (event) => {
+ event.preventDefault();
+ const magnetLink = await getMagnetLink(downloadLink);
+ document.body.insertAdjacentHTML('beforeend', `
+
+
+
+
+ ${downloadButtonText(downloadSize)}
+
+
+
+
+
+
+
+ ${magnetLink ?
+ `
` : ``}
+
+
+
+
`);
+ })
+}
+
diff --git a/static/skin/index.js b/static/skin/index.js
index 1818ae793..ae7a02911 100644
--- a/static/skin/index.js
+++ b/static/skin/index.js
@@ -254,122 +254,6 @@
return output.substring(1);
}
- /* hack for library.kiwix.org magnet links (created by MirrorBrain)
- See https://github.com/kiwix/container-images/issues/242 */
- async function getFixedMirrorbrainMagnet(magnetLink) {
- // parse as query parameters
- const params = new URLSearchParams(
- magnetLink.replaceAll('&', '&').replace(/^magnet:/, ''));
-
- const zimUrl = params.get('as'); // as= is fallback URL
- // download metalink to build list of mirrored URLs
- let mirrorUrls = [];
-
- const metalink = await fetch(`${zimUrl}.meta4`).then(response => {
- return response.ok ? response.text() : '';
- }).catch((_error) => '');
- if (metalink) {
- try {
- const parser = new DOMParser();
- const doc = parser.parseFromString(metalink, "application/xml");
- doc.querySelectorAll("url").forEach((node) => {
- if (node.hasAttribute("priority")) { // ensures its a mirror link
- mirrorUrls.push(node.innerHTML);
- }
- });
- } catch (err) {
- // not a big deal, magnet will only contain primary URL
- console.debug(`Failed to parse mirror links for ${zimUrl}`);
- }
- }
-
- // set webseed (ws=) URL to primary download URL (redirects to mirror)
- params.set('ws', zimUrl);
- // if we got metalink mirror URLs, append them all
- if (mirrorUrls) {
- mirrorUrls.forEach((url) => {
- params.append('ws', url);
- });
- }
-
- params.set('xs', `${zimUrl}.torrent`); // adding xs= to point to torrent URL
-
- return 'magnet:?' + makeURLSearchString(params, ['ws', 'as', 'dn', 'xs', 'tr']);
- }
-
- 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) => '');
- if (magnetLink) {
- return await getFixedMirrorbrainMagnet(magnetLink);
- }
- return magnetLink;
- }
-
- function insertModal(button) {
- const downloadSize = button.getAttribute('data-size');
- const downloadLink = button.getAttribute('data-link');
- button.addEventListener('click', async (event) => {
- event.preventDefault();
- const magnetLink = await getMagnetLink(downloadLink);
- document.body.insertAdjacentHTML('beforeend', `
-
-
-
-
- ${downloadButtonText(downloadSize)}
-
-
-
-
-
-
-
- ${magnetLink ?
- `
` : ``}
-
-
-
-
`);
- })
- }
-
async function getBookCount(query) {
const url = `${root}/catalog/v2/entries?${query}&count=0`;
return await fetch(url).then(async (resp) => {
@@ -483,7 +367,9 @@
iso.insert(generateBookHtml(book, sort))
const downloadButton = document.querySelector(`[data-id="${getInnerHtml(book, 'id')}"] .book__download`);
if (downloadButton) {
- insertModal(downloadButton);
+ const downloadSize = downloadButton.getAttribute('data-size');
+ const downloadLink = downloadButton.getAttribute('data-link');
+ insertDownloadZimModal(downloadButton, downloadLink, downloadSize, `${root}`);
}
});
refreshTagLinks();
diff --git a/static/templates/index.html b/static/templates/index.html
index 31222e9cd..55bdea352 100644
--- a/static/templates/index.html
+++ b/static/templates/index.html
@@ -32,11 +32,12 @@
-
+
+
diff --git a/static/viewer.html b/static/viewer.html
index f5eebd56f..1730ad843 100644
--- a/static/viewer.html
+++ b/static/viewer.html
@@ -26,6 +26,7 @@
+