Skip to content

Commit

Permalink
Merge pull request #383 from kiwix/Always-use-iframe-src-in-Service-W…
Browse files Browse the repository at this point in the history
…orker-mode

In ServiceWorker mode, set the iframe src to display articles.
  • Loading branch information
mossroy committed May 30, 2018
2 parents 95afdee + 294facd commit 78ee035
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,26 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
* @param {DirEntry} dirEntry
*/
function readArticle(dirEntry) {
if (dirEntry.isRedirect()) {
selectedArchive.resolveRedirect(dirEntry, readArticle);
if (contentInjectionMode === 'serviceworker') {
// In ServiceWorker mode, we simply set the iframe src and show it when it's ready.
// (reading the backend is handled by the ServiceWorker itself)
var iframeArticleContent = document.getElementById('articleContent');
iframeArticleContent.onload = function () {
iframeArticleContent.onload = function () {};
// Actually display the iframe content
$("#readingArticle").hide();
$("#articleContent").show();
};
iframeArticleContent.src = dirEntry.namespace + "/" + dirEntry.url;
}
else {
selectedArchive.readUtf8File(dirEntry, displayArticleInForm);
// In jQuery mode, we read the article content in the backend and manually insert it in the iframe
if (dirEntry.isRedirect()) {
selectedArchive.resolveRedirect(dirEntry, readArticle);
}
else {
selectedArchive.readUtf8File(dirEntry, displayArticleContentInIframe);
}
}
}

Expand Down Expand Up @@ -825,15 +840,13 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
* @param {DirEntry} dirEntry
* @param {String} htmlArticle
*/
function displayArticleInForm(dirEntry, htmlArticle) {
function displayArticleContentInIframe(dirEntry, htmlArticle) {
// Scroll the iframe to its top
$("#articleContent").contents().scrollTop(0);

if (contentInjectionMode === 'jquery') {
// Replaces ZIM-style URLs of img, script and link tags with a data-url to prevent 404 errors [kiwix-js #272 #376]
// This replacement also processes the URL to remove the path so that the URL is ready for subsequent jQuery functions
htmlArticle = htmlArticle.replace(regexpTagsWithZimUrl, "$1data-kiwixurl$2$3");
}
// Replaces ZIM-style URLs of img, script and link tags with a data-url to prevent 404 errors [kiwix-js #272 #376]
// This replacement also processes the URL to remove the path so that the URL is ready for subsequent jQuery functions
htmlArticle = htmlArticle.replace(regexpTagsWithZimUrl, "$1data-kiwixurl$2$3");

// Compute base URL
var urlPath = regexpPath.test(dirEntry.url) ? urlPath = dirEntry.url.match(regexpPath)[1] : '';
Expand Down Expand Up @@ -862,15 +875,12 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
$("#articleContent").show();
// Allow back/forward in browser history
pushBrowserHistoryState(dirEntry.namespace + "/" + dirEntry.url);
// If the ServiceWorker is not useable, we need to fallback to parse the DOM
// to inject images, CSS etc, and replace links with javascript calls
if (contentInjectionMode === 'jquery') {
parseAnchorsJQuery();
loadImagesJQuery();
loadCSSJQuery();
//JavaScript loading currently disabled
//loadJavaScriptJQuery();
}

parseAnchorsJQuery();
loadImagesJQuery();
loadCSSJQuery();
//JavaScript loading currently disabled
//loadJavaScriptJQuery();
};

// Load the blank article to clear the iframe (NB iframe onload event runs *after* this)
Expand Down

0 comments on commit 78ee035

Please sign in to comment.