]*infobox)[\s\S]+?<\/table>[^<]*)(]*>(?:(?=([^<]+))\3|<(?!p\b[^>]*>))*?<\/p>)/ig, "$2$1");
htmlArticle = htmlArticle.replace(/(table\s+(?=[^>]*class\s*=\s*["'][^"']*infobox)[^>]*style\s*=\s*["'][^"']+[^;'"]);?\s*["']/ig, '$1; position: relative; border: 1px solid #eaecf0; text-align: left; background-color: #f8f9fa;"');
}
if (cssSource == "desktop") { //If user has selected desktop display mode...
htmlArticle = htmlArticle.replace(/class\s*=\s*["']\s*mw-body\s*["']\s*/ig, 'style="background-color: white; padding: 1em; border-width: 0px; max-width: 55.8em; margin: 0 auto 0 auto;"');
htmlArticle = htmlArticle.replace(/
]+titleHeading[^>]+>\s*<\/h1>\s*/ig, ""); //Void empty header title
}
htmlArticle = htmlArticle.replace(/\s*(<\/head>)/i, cssArray$ + "$1");
console.log("All CSS resolved");
injectHTML(htmlArticle); //This passes the revised HTML to the image and JS subroutine...
} else {
//console.log("Waiting for " + (cssArray.length - blobArray.length) + " out of " + cssArray.length + " to resolve...")
}
}
//End of preload stylesheets code
function injectHTML(htmlContent) {
$("#readingArticle").hide();
$("#articleContent").show();
// Scroll the iframe to its top
$("#articleContent").contents().scrollTop(0);
$('#articleContent').contents().find('body').html(htmlContent);
// If the ServiceWorker is not useable, we need to fallback to parse the DOM
// to inject math images, and replace some links with javascript calls
if (contentInjectionMode === 'jquery') {
// Convert links into javascript calls
$('#articleContent').contents().find('body').find('a').each(function () {
// Store current link's url
var url = $(this).attr("href");
if (url === null || url === undefined) {
return;
}
var lowerCaseUrl = url.toLowerCase();
var cssClass = $(this).attr("class");
if (cssClass === "new") {
// It's a link to a missing article : display a message
$(this).on('click', function (e) {
alert("Missing article in Wikipedia");
return false;
});
}
else if (url.slice(0, 1) === "#") {
// It's an anchor link : do nothing
}
else if (url.substring(0, 4) === "http") {
// It's an external link : open in a new tab
$(this).attr("target", "_blank");
}
else if (url.match(regexpImageLink)
&& (util.endsWith(lowerCaseUrl, ".png")
|| util.endsWith(lowerCaseUrl, ".svg")
|| util.endsWith(lowerCaseUrl, ".jpg")
|| util.endsWith(lowerCaseUrl, ".jpeg"))) {
// It's a link to a file of Wikipedia : change the URL to the online version and open in a new tab
var onlineWikipediaUrl = url.replace(regexpImageLink, "https://" + selectedArchive._language + ".wikipedia.org/wiki/File:$1");
$(this).attr("href", onlineWikipediaUrl);
$(this).attr("target", "_blank");
}
else {
// It's a link to another article
// Add an onclick event to go to this article
// instead of following the link
if (url.substring(0, 2) === "./") {
url = url.substring(2);
}
// Remove the initial slash if it's an absolute URL
else if (url.substring(0, 1) === "/") {
url = url.substring(1);
}
$(this).on('click', function (e) {
var decodedURL = decodeURIComponent(url);
pushBrowserHistoryState(decodedURL);
goToArticle(decodedURL);
return false;
});
}
});
// Load images
$('#articleContent').contents().find('body').find('img').each(function () {
var image = $(this);
// It's a standard image contained in the ZIM file
// We try to find its name (from an absolute or relative URL)
var imageMatch = image.attr('data-kiwixsrc').match(regexpImageUrl); //kiwix-js #272
if (imageMatch) {
var title = decodeURIComponent(imageMatch[1]);
selectedArchive.getDirEntryByTitle(title).then(function (dirEntry) {
selectedArchive.readBinaryFile(dirEntry, function (readableTitle, content) {
// TODO : use the complete MIME-type of the image (as read from the ZIM file)
uiUtil.feedNodeWithBlob(image, 'src', content, 'image');
});
}).fail(function (e) {
console.error("could not find DirEntry for image:" + title, e);
});
}
});
/*/ Load Javascript content
$('#articleContent').contents().find('script').each(function () {
var script = $(this);
// We try to find its name (from an absolute or relative URL)
if (script) { var srcMatch = script.attr("src").match(regexpMetadataUrl) }
// TODO check that the type of the script is text/javascript or application/javascript
if (srcMatch) {
// It's a Javascript file contained in the ZIM file
var title = uiUtil.removeUrlParameters(decodeURIComponent(srcMatch[1]));
selectedArchive.getDirEntryByTitle(title).then(function (dirEntry) {
if (dirEntry === null)
console.log("Error: js file not found: " + title);
else
selectedArchive.readBinaryFile(dirEntry, function (readableTitle, content) {
// TODO : I have to disable javascript for now
// var jsContent = encodeURIComponent(util.uintToString(content));
//script.attr("src", 'data:text/javascript;charset=UTF-8,' + jsContent);
});
}).fail(function (e) {
console.error("could not find DirEntry for javascript : " + title, e);
});
}
});*/
}
}
}
/**
* Changes the URL of the browser page, so that the user might go back to it
*
* @param {String} title
* @param {String} titleSearch
*/
function pushBrowserHistoryState(title, titleSearch) {
var stateObj = {};
var urlParameters;
var stateLabel;
if (title && !(""===title)) {
stateObj.title = title;
urlParameters = "?title=" + title;
stateLabel = "Wikipedia Article : " + title;
}
else if (titleSearch && !(""===titleSearch)) {
stateObj.titleSearch = titleSearch;
urlParameters = "?titleSearch=" + titleSearch;
stateLabel = "Wikipedia search : " + titleSearch;
}
else {
return;
}
window.history.pushState(stateObj, stateLabel, urlParameters);
}
/**
* Replace article content with the one of the given title
* @param {String} title
*/
function goToArticle(title) {
selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) {
if (dirEntry === null || dirEntry === undefined) {
$("#readingArticle").hide();
alert("Article with title " + title + " not found in the archive");
}
else {
$("#articleName").html(title);
$("#readingArticle").show();
$('#articleContent').contents().find('body').html("");
readArticle(dirEntry);
}
}).fail(function() { alert("Error reading article with title " + title); });
}
function goToRandomArticle() {
selectedArchive.getRandomDirEntry(function(dirEntry) {
if (dirEntry === null || dirEntry === undefined) {
alert("Error finding random article.");
}
else {
if (dirEntry.namespace === 'A') {
$("#articleName").html(dirEntry.title);
pushBrowserHistoryState(dirEntry.url);
$("#readingArticle").show();
$('#articleContent').contents().find('body').html("");
readArticle(dirEntry);
}
else {
// If the random title search did not end up on an article,
// we try again, until we find one
goToRandomArticle();
}
}
});
}
function goToMainArticle() {
selectedArchive.getMainPageDirEntry(function(dirEntry) {
if (dirEntry === null || dirEntry === undefined) {
console.error("Error finding main article.");
}
else {
if (dirEntry.namespace === 'A') {
$("#articleName").html(dirEntry.title);
pushBrowserHistoryState(dirEntry.url);
$("#readingArticle").show();
$('#articleContent').contents().find('body').html("");
readArticle(dirEntry);
}
else {
console.error("The main page of this archive does not seem to be an article");
}
}
});
}
});