Interpret noscript tags in jQuery mode.

Fixes #433
This commit is contained in:
Mossroy 2018-12-11 16:34:33 +01:00
parent 3c1f74c0e0
commit ea60fb9a9b

View File

@ -862,9 +862,12 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
parseAnchorsJQuery();
loadImagesJQuery();
// JavaScript is currently disabled, so we need to make the browser interpret noscript tags
// NB : if javascript is properly handled in jQuery mode in the future, this call should be removed
// and noscript tags should be ignored
loadNoScriptTags();
//loadJavaScriptJQuery();
loadCSSJQuery();
//JavaScript loading currently disabled
//loadJavaScriptJQuery();
insertMediaBlobsJQuery();
};
@ -947,12 +950,23 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
});
});
}
function loadNoScriptTags() {
// For each noscript tag, we replace it with its content, so that the browser interprets it
$('#articleContent').contents().find('noscript').replaceWith(function () {
// When javascript is enabled, browsers interpret the content of noscript tags as text
// (see https://html.spec.whatwg.org/multipage/scripting.html#the-noscript-element)
// So we can read this content with .textContent
return this.textContent;
});
}
function loadCSSJQuery() {
// Ensure all sections are open for clients that lack JavaScript support, or that have some restrictive CSP [kiwix-js #355].
// This is needed only for some versions of ZIM files generated by mwoffliner (at least in early 2018), where the article sections are closed by default on small screens.
// These sections can be opened by clicking on them, but this is done with some javascript.
// The code below is a workaround, a better fix is tracked on [mwoffliner #324]
// The code below is a workaround we still need for compatibility with ZIM files generated by mwoffliner in 2018.
// A better fix has been made for more recent ZIM files, with the use of noscript tags : see https://github.com/openzim/mwoffliner/issues/324
var iframe = document.getElementById('articleContent').contentDocument;
var collapsedBlocks = iframe.querySelectorAll('.collapsible-block:not(.open-block), .collapsible-heading:not(.open-block)');
// Using decrementing loop to optimize performance : see https://stackoverflow.com/questions/3520688