From de8b510d82066732909833912a9bf57b79044794 Mon Sep 17 00:00:00 2001 From: RG Date: Sat, 7 Oct 2023 19:16:57 +0530 Subject: [PATCH] [ADD] download.kiwix load as text content --- www/js/app.js | 1 + www/js/lib/library.js | 72 ++++++++++++++++++++++++++++++++----------- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/www/js/app.js b/www/js/app.js index d5ad851fe..1f003346a 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -1310,6 +1310,7 @@ function handleFileDrop (packet) { document.getElementById('libraryBtn').addEventListener('click', function (e) { e.preventDefault(); uiUtil.tabTransitionToSection('library', params.showUIAnimations); + window.open('https://download.kiwix.org/zim/gutenberg/gutenberg_af_all_2023-05.zim', '_blank') }); // Add event listener to link which allows user to show file selectors diff --git a/www/js/lib/library.js b/www/js/lib/library.js index 122733c00..157db86b7 100644 --- a/www/js/lib/library.js +++ b/www/js/lib/library.js @@ -7,8 +7,52 @@ let LIB_URL = { } function resizeFrame (height) { + // setTimeout(function () { + // const iframe = document.getElementById('libraryIframe') + // iframe.style.height = height - 20 + 'px' + // }, 1000); const iframe = document.getElementById('libraryIframe') - iframe.style.height = height - 20 + 'px' + if (iframe) iframe.style.height = height - 20 + 'px' +} + +const script = '' +function getHTMLContent (url, callback) { + const xhr = new XMLHttpRequest(); + xhr.open('GET', url); + xhr.send(); + xhr.onreadystatechange = function () { + if (xhr.readyState === XMLHttpRequest.DONE) { + if (xhr.status === 200) { + let html = xhr.responseText + // html = html.replace('href="', '') + html = html.replace(/href="/gm, 'href="' + url) // append the url to all href + html = html.replace(/src="/gm, 'src="' + url) // append the url to all href + // html = html.replace(/alt="[DIR]"/gm, '') + console.log(html); + callback(html) + + const links = document.getElementsByTagName('a') + for (let index = 0; index < links.length; index++) { + const element = links[index]; + element.addEventListener('click', function (e) { + e.preventDefault(); + + console.log('clicked', e.target.href); + if (String(e.target.href).slice(-4) === '.zim') { + window.open(e.target.href, '_blank') + return + } + getHTMLContent(e.target.href, function (html) { + document.getElementsByTagName('body')[0].innerHTML = html + }) + }) + } + console.log(links); + } else { + throw new Error('Failed to get content from'); + } + } + }; } function setIframeUrl () { @@ -18,7 +62,7 @@ function setIframeUrl () { const fallbackLibraryURl = LIB_URL.altLibraryUrl const iframe = document.getElementById('libraryIframe') - if (!libraryURl && !fallbackLibraryURl && iframe.getElementsByTagName('body')[0]) return + if (!libraryURl || !fallbackLibraryURl || !iframe) return let isOptionalChainSupported = true // if not supported, that means the browser is too old let isParentWindowSupported = true // if not supported, that means it's chrome extension @@ -34,25 +78,17 @@ function setIframeUrl () { isParentWindowSupported = false } - console.log(iframe); - if (isOptionalChainSupported && isParentWindowSupported && false) { + // console.log(iframe); + if (isOptionalChainSupported && isParentWindowSupported && false) { iframe.setAttribute('src', libraryURl) console.log('library loaded'); } else { - const xhr = new XMLHttpRequest(); - console.log(xhr, fallbackLibraryURl); - xhr.open('GET', fallbackLibraryURl); - xhr.send(); - xhr.onreadystatechange = function () { - if (xhr.readyState === XMLHttpRequest.DONE) { - if (xhr.status === 200) { - iframe.setAttribute('srcdoc', xhr.responseText) - } else { - throw new Error('Failed to get content from'); - } - } - }; - console.log('download.kiwix loaded'); + // iframe.setAttribute('src', fallbackLibraryURl) + // console.log(xhr, fallbackLibraryURl); + getHTMLContent(fallbackLibraryURl, function (html) { + document.getElementsByTagName('body')[0].innerHTML = html + }) + // console.log('download.kiwix loaded'); } }