diff --git a/www/js/app.js b/www/js/app.js index 73e4db9b..2e2bff0b 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -455,7 +455,7 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett if (params.contentInjectionMode == 'jquery') { images.prepareImagesJQuery(articleWindow, true); } else { - images.prepareImagesServiceWorker(true); + images.prepareImagesServiceWorker(articleWindow, true); } } } else { @@ -1152,8 +1152,14 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett document.getElementById('manipulateImagesCheck').addEventListener('click', function () { params.manipulateImages = this.checked; settingsStore.setItem('manipulateImages', params.manipulateImages, Infinity); - if (this.checked && params.contentInjectionMode === 'serviceworker') { - uiUtil.systemAlert('Please be aware that image manipulation can interfere badly with non-Wikimedia ZIMs (particularly ZIMs that have active content). If you cannot access the articles in such a ZIM, please turn this setting off.'); + if (this.checked) { + if (/UWP/.test(params.appType)) { + uiUtil.systemAlert('This option does not work in UWP apps. WORKAROUND: To save an image to disk, please select the "Add breakout link ..." option below, load the article you require, and export it to a browser window by clicking the breakout link. You will then be able to right-click or long-press images in the exported page and save them.'); + } else if (params.contentInjectionMode === 'serviceworker') { + uiUtil.systemAlert('Please be aware that Image manipulation can interfere badly with non-Wikimedia ZIMs (particularly ZIMs that have active content). If you cannot access the articles in such a ZIM, please turn this setting off.'); + } else if (/PWA/.test(params.appType)) { + uiUtil.systemAlert('Be aware that this option may interfere with active content if you switch to Service Worker mode.'); + } } params.themeChanged = true; }); @@ -1231,12 +1237,13 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett document.getElementById('allowHTMLExtractionCheck').addEventListener('change', function (e) { params.allowHTMLExtraction = e.target.checked; var alertMessage = ''; - if (params.windowOpener && params.allowHTMLExtraction) alertMessage = 'Enabling this option disables the more advanced tab/window opening option above.'; + if (params.windowOpener && params.allowHTMLExtraction) alertMessage = 'Enabling this option disables the more advanced tab/window opening option above. '; if (params.allowHTMLExtraction) { if (params.contentInjectionMode === 'serviceworker') { - alertMessage = 'WARNING: This option can interfere badly with non-Wikimedia ZIMs that have active content: turn it off if affected. ' + alertMessage; + alertMessage = 'Please be aware that Image manipulation can interfere badly with non-Wikimedia ZIMs (particularly ZIMs that have active content). ' + + 'If you cannot access the articles in such a ZIM, please turn this setting off. ' + alertMessage; } else if (/PWA/.test(params.appType)) { - alertMessage += ' Be aware that this option may interfere with active content if you switch to Service Worker mode.'; + alertMessage += 'Be aware that this option may interfere with active content if you switch to Service Worker mode.'; } uiUtil.systemAlert(alertMessage); params.windowOpener = false; @@ -3455,7 +3462,7 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett // URLs that begin 'http' (i.e. non-relative URLs). It then captures the whole of the URL up until either the opening delimiter // (" or ', which is capture group \3) or a querystring or hash character (? or #). When the regex is used below, it will be further // processed to calculate the ZIM URL from the relative path. This regex can cope with legitimate single quote marks (') in the URL. - var regexpTagsWithZimUrl = /(<(?:img|script|link)\b[^>]*?\s)(?:src|href)(\s*=\s*(["']))(?!http|app:)(.+?)(?=\3|\?|#)/ig; + params.regexpTagsWithZimUrl = /(<(?:img|script|link)\b[^>]*?\s)(?:src|href)(\s*=\s*(["']))(?!http|app:)(.+?)(?=\3|\?|#)/ig; // Regex below tests the html of an article for active content [kiwix-js #466] // It inspects every '; - }); + // Process Wikimedia MathML, but not if we'll be using the jQuery routine later + if (!(params.manipulateImages || params.allowHTMLExtraction)) { + imageHtml = documentImages[i].outerHTML; + if (params.useMathJax && /\bmath\/tex\b/i.test(imageHtml)) { + params.containsMathTex = true; + documentImages[i].outerHTML = imageHtml.replace(transformMathTextRegexp, function (p0, p1, math) { + // Remove any rogue ampersands in MathJax due to double escaping (by Wikipedia) + math = math.replace(/&/g, '&'); + return ''; + }); + } } - // DEV: make sure list of file types here is the same as the list in Service Worker code - if (/(^|\/).+\.(jpe?g|png|svg|gif|webp)($|[?#])/i.test(documentImages[i].src)) { + if (imageURLRegexp.test(documentImages[i].src)) { documentImages[i].dataset.kiwixurl = documentImages[i].getAttribute('src'); if (params.imageDisplayMode === 'progressive') { documentImages[i].style.opacity = '0'; } + if (params.manipulateImages || params.allowHTMLExtraction) { + documentImages[i].outerHTML = documentImages[i].outerHTML.replace(params.regexpTagsWithZimUrl, function(match, blockStart, equals, quote, relAssetUrl) { + var assetZIMUrl = uiUtil.deriveZimUrlFromRelativeUrl(relAssetUrl, params.baseUrl); + // DEV: Note that deriveZimUrlFromRelativeUrl produces a *decoded* URL (and incidentally would remove any URI component + // if we had captured it). We therefore re-encode the URI with encodeURI (which does not encode forward slashes) instead + // of encodeURIComponent. + return blockStart + 'data-kiwixurl' + equals + encodeURI(assetZIMUrl); + }); + } } } + if (params.manipulateImages || params.allowHTMLExtraction) { + prepareImagesJQuery(win, forPrinting); + return; + } + if (forPrinting) { + if (params.preloadAllImages) document.getElementById('searchingArticles').style.display = 'block'; extractImages(documentImages, params.preloadingAllImages ? params.preloadAllImages : params.printImagesLoaded); } else { if (params.imageDisplayMode === 'manual') { diff --git a/www/js/lib/uiUtil.js b/www/js/lib/uiUtil.js index 9eb9392c..22a19d26 100644 --- a/www/js/lib/uiUtil.js +++ b/www/js/lib/uiUtil.js @@ -321,11 +321,11 @@ define(rqDef, function() { 'switch to Service Worker mode ' + 'if your platform supports it. [Permanently hide]' + ''; - if (params.contentInjectionMode === 'serviceworker' && params.manipulateImages) { + if (params.contentInjectionMode === 'serviceworker' && (params.manipulateImages || params.allowHTMLExtraction)) { alertHTML = '