Skip to content

Commit

Permalink
New library loader with fallback to mirrors for emergencies (#1293)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaifroid authored Dec 3, 2024
1 parent b72dbc1 commit 5a51bec
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 69 deletions.
8 changes: 7 additions & 1 deletion i18n/en.jsonp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions i18n/es.jsonp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion i18n/fr.jsonp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
68 changes: 3 additions & 65 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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 = '<html><head><title>';
htmlDoc += translateUI.t('configure-library-mirrors') || 'Library Mirrors';
htmlDoc += '</title></head><body><h1>';
htmlDoc += translateUI.t('configure-library-mirrors') || 'Library Mirrors';
htmlDoc += '</h1><p style="font-size: large;">'
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 += '</p><ul>';
params.kiwixDownloadMirrors.forEach(function (mirror) {
htmlDoc += '<li style="font-size: large;" class="console"><a href="' + mirror + '" target="_blank">' + mirror.replace(/^([^/]+\/\/[^/]+).*/, '$1') + '</a></li>';
});
htmlDoc += '</ul></body></html>';
iframe.onload = function () {
iframe.onload = null;
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(htmlDoc);
iframe.contentWindow.document.close();
uiUtil.tabTransitionToSection('library', params.showUIAnimations);
resizeIFrame();
}
iframe.src = 'about:blank';
}
}, 500); // Adjust the timeout if too long
};
iframe.setAttribute('src', params.altLibraryUrl);
};

// Add event listener to link which allows user to show file selectors
document.getElementById('selectorsDisplayLink').addEventListener('click', function (e) {
e.preventDefault();
Expand Down
Loading

0 comments on commit 5a51bec

Please sign in to comment.