diff --git a/i18n/en.jsonp.js b/i18n/en.jsonp.js index bf12d49fa..0125d229f 100644 --- a/i18n/en.jsonp.js +++ b/i18n/en.jsonp.js @@ -66,8 +66,14 @@ document.localeJson = { "configure-display-apptheme-info": "[ Show article with applied theme ]", "configure-language-selector-default": "Language", "configure-language-selector-other": "More soon...", - "configure-library-mirrors": "Library Mirrors", + "configure-library-all-unreachable": "All library servers are currently unreachable.", "configure-library-altlibrary": "The library at", + "configure-library-connecting": "Connecting to Library", + "configure-library-incompatible": "Browser is incompatible with primary library", + "configure-library-mirrors": "Library Mirrors", + "configure-library-primary-unreachable": "
", + "configure-library-trying-alternative": "Attempting to contact backup server", + "configure-library-trying-primary": "Attempting to contact primary library server", "configure-library-unreachable": "appears to be unreachable. Please try one of these mirrors:", "configure-performance-settings-title": "Performance / compatibility", "configure-performance-panel-header": "Caching and preview settings", diff --git a/i18n/es.jsonp.js b/i18n/es.jsonp.js index 5c8f8202c..e624bacf0 100644 --- a/i18n/es.jsonp.js +++ b/i18n/es.jsonp.js @@ -66,9 +66,15 @@ document.localeJson = { "configure-display-apptheme-info": "[ Mostrar artículo con tema seleccionado ]", "configure-language-selector-default": "Idioma", "configure-language-selector-other": "Más pronto...", - "configure-library-mirrors": "Espejos de la biblioteca", + "configure-library-all-unreachable": "Todos los servidores de la biblioteca están inalcanzables actualmente.", "configure-library-altlibrary": "La biblioteca en", - "configure-library-unreachable": "parece ser inalcanzable. Por favor, pruebe uno de estos espejos:", + "configure-library-connecting": "Conectando a la biblioteca", + "configure-library-incompatible": "Navegador no es compatible con la biblioteca primaria", + "configure-library-mirrors": "Espejos de la biblioteca", + "configure-library-primary-unreachable": "
", + "configure-library-trying-alternative": "Intentando contactar con el servidor de respaldo", + "configure-library-trying-primary": "Intentando contactar con el servidor de la biblioteca primaria", + "configure-library-unreachable": "parece inalcanzable. Por favor, pruebe uno de estos espejos:", "configure-performance-settings-title": "Rendimiento y compatibilidad", "configure-performance-panel-header": "Ajustes de caché y vista previa", "configure-performance-cacheassets-description": "Kiwix JS puede acelerar la visualización de artículos almacenando en caché los activos:", diff --git a/i18n/fr.jsonp.js b/i18n/fr.jsonp.js index c1805be61..db2b95948 100644 --- a/i18n/fr.jsonp.js +++ b/i18n/fr.jsonp.js @@ -66,8 +66,14 @@ document.localeJson = { "configure-display-apptheme-info": "[ Afficher l'article avec le thème sélectionné ]", "configure-language-selector-default": "Langue", "configure-language-selector-other": "Bientôt plus...", - "configure-library-mirrors": "Miroirs de bibliothèque", + "configure-library-all-unreachable": "Tous les serveurs de bibliothèque sont actuellement inaccessibles.", "configure-library-altlibrary": "La bibliothèque à", + "configure-library-connecting": "Connexion à la bibliothèque", + "configure-library-incompatible": "Navigateur est incompatible avec la bibliothèque principale", + "configure-library-mirrors": "Miroirs de bibliothèque", + "configure-library-primary-unreachable": "
", + "configure-library-trying-alternative": "Tentative de contact avec le serveur de sauvegarde", + "configure-library-trying-primary": "Tentative de contact avec le serveur de bibliothèque principal", "configure-library-unreachable": "semble être inaccessible. Veuillez essayer un de ces miroirs :", "configure-performance-settings-title": "Performances et compatibilité", "configure-performance-panel-header": "Mise en caché et prévisualisation", diff --git a/service-worker.js b/service-worker.js index 6f2342ca1..c72202a11 100644 --- a/service-worker.js +++ b/service-worker.js @@ -137,6 +137,7 @@ const precacheFiles = [ 'www/js/lib/arrayFromPolyfill.js', 'www/js/lib/filecache.js', 'www/js/lib/cache.js', + 'www/js/lib/kiwixLibrary.js', 'www/js/lib/popovers.js', 'www/js/lib/promisePolyfill.js', 'www/js/lib/settingsStore.js', diff --git a/www/js/app.js b/www/js/app.js index cdd25e345..fc89b096d 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -35,6 +35,7 @@ import popovers from './lib/popovers.js'; import settingsStore from './lib/settingsStore.js'; import abstractFilesystemAccess from './lib/abstractFilesystemAccess.js'; import translateUI from './lib/translateUI.js'; +import kiwixLibrary from './lib/kiwixLibrary.js'; if (params.abort) { // If the app was loaded only to pass a message from the remote code, then we exit immediately @@ -1791,37 +1792,11 @@ async function handleFileDrop (packet) { const btnLibrary = document.getElementById('btnLibrary'); btnLibrary.addEventListener('click', function (e) { - e.preventDefault(); const libraryContent = document.getElementById('libraryContent'); const libraryIframe = libraryContent.contentWindow.document.getElementById('libraryIframe'); - libraryIframe.src = 'about:blank'; // Empty the iframe - const xhr = new XMLHttpRequest(); - xhr.open('HEAD', params.libraryUrl, true); - xhr.onreadystatechange = function () { - if (xhr.readyState === 4) { - if (xhr.status >= 200 && xhr.status < 300) { - try { - // eslint-disable-next-line no-new-func - Function('try{}catch{}')(); // Tests the browser can run code that will be used by the library - libraryIframe.setAttribute('src', params.libraryUrl); - uiUtil.tabTransitionToSection('library', params.showUIAnimations); - resizeIFrame(); - } catch (error) { - console.warn('Browser cannot run code in the library iframe', error); - handleLibraryError(libraryIframe); - } - } else { - console.warn('Library server ' + params.libraryUrl + ' is unreachable...'); - handleLibraryError(libraryIframe); - } - } - }; - xhr.onerror = function () { - handleLibraryError(libraryIframe); - }; - xhr.send(); uiUtil.tabTransitionToSection('library', params.showUIAnimations); resizeIFrame(); + kiwixLibrary.loadLibrary(libraryIframe); }); // Add keyboard activation for library button btnLibrary.addEventListener('keydown', function (e) { @@ -1830,44 +1805,7 @@ btnLibrary.addEventListener('keydown', function (e) { btnLibrary.click(); } }); -// Error handler for library iframe -function handleLibraryError (iframe) { - iframe.onload = function () { - iframe.onload = null; - setTimeout(function () { - try { - if (!iframe.contentWindow || !iframe.contentWindow.document) { - console.warn('Library server ' + params.altLibraryUrl + ' is unreachable...'); - throw new Error('Iframe content not accessible'); - } - } catch (error) { - let htmlDoc = '
' - htmlDoc += translateUI.t('configure-library-altlibrary') || 'The library at'; - htmlDoc += ' ' + params.altLibraryUrl + ' '; - htmlDoc += translateUI.t('configure-library-unreachable') || 'appears to be unreachable. Please try one of these mirrors:'; - htmlDoc += '
${message}
`; + } + } catch (e) { + console.warn('Could not update status:', e); + } +} + +/** + * Initialize the iframe with the base template + * @param {Objet} frame The iframe DOM object to initialize + * @returns {Promise} A promise that resolves when the iframe is initialized + */ +function initializeFrame (frame) { + return new Promise((resolve) => { + frame.src = 'about:blank'; + frame.onload = () => { + frame.onload = null; + frame.contentWindow.document.open(); + frame.contentWindow.document.write(createBaseHtml()); + frame.contentWindow.document.close(); + resolve(); // Signal that initialization is complete + }; + }); +} + +/** + * Creates HTML content for the mirror list + * @returns {String} The HTML content for the mirror list + */ +function createMirrorListHtml () { + const mirrorListText = translateUI.t('configure-library-mirrors') || 'Library Mirrors'; + const unreachableMsg = translateUI.t('configure-library-unreachable') || 'appears to be unreachable. Please try one of these mirrors:'; + const altLibraryMsg = translateUI.t('configure-library-altlibrary') || 'The library at'; + + let html = ` ++ ${altLibraryMsg} ${params.altLibraryUrl} ${unreachableMsg} +
+${tryingAlternativeMsg} ${params.altLibraryUrl}
`); + } else { + // For connection errors, show the standard unreachable message + console.warn('Primary library unreachable:', primaryError); + tryingAlternativeMsg = translateUI.t('configure-library-primary-unreachable') || + ''; + tryingAlternativeMsg += translateUI.t('configure-library-trying-alternative') || + 'Attempting to contact backup server'; + updateStatus(iframe, tryingAlternativeMsg + ' ' + params.altLibraryUrl + '
'); + } + try { + // Try alternative library URL + await checkUrl(params.altLibraryUrl); + iframe.src = params.altLibraryUrl; + } catch (alternativeError) { + console.warn('Alternative library unreachable:', alternativeError); + showMirrorList(iframe); + } + } +} + +export default { + loadLibrary +};