2019-10-30 17:06:55 +01:00
|
|
|
var controller = new AbortController()
|
|
|
|
|
|
|
|
// Cancel any pending image loads (there might be none) and reset the controller
|
|
|
|
function cancelImageLoad() {
|
|
|
|
controller.abort();
|
|
|
|
controller = new AbortController();
|
|
|
|
}
|
2019-10-19 23:25:06 +02:00
|
|
|
|
2019-10-13 22:47:11 +02:00
|
|
|
// Used to pop a resizable image view
|
2019-10-14 02:41:34 +02:00
|
|
|
async function imageWasClicked(img) {
|
2019-10-30 17:06:55 +01:00
|
|
|
cancelImageLoad();
|
|
|
|
showNetworkLoading(img);
|
2019-10-14 02:41:34 +02:00
|
|
|
|
|
|
|
try {
|
2019-10-30 17:06:55 +01:00
|
|
|
|
|
|
|
const signal = controller.signal;
|
|
|
|
const response = await fetch(img.src, { signal: signal });
|
2019-10-14 02:41:34 +02:00
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Network response was not ok.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const imgBlob = await response.blob();
|
2019-10-30 17:06:55 +01:00
|
|
|
if (signal.aborted) {
|
|
|
|
throw new Error('Network response was aborted.');
|
|
|
|
}
|
|
|
|
|
2019-10-19 23:54:55 +02:00
|
|
|
hideNetworkLoading(img);
|
2019-10-30 17:06:55 +01:00
|
|
|
|
2019-10-14 02:41:34 +02:00
|
|
|
var reader = new FileReader();
|
|
|
|
reader.readAsDataURL(imgBlob);
|
|
|
|
reader.onloadend = function() {
|
2019-10-19 23:54:55 +02:00
|
|
|
|
2019-10-30 17:06:55 +01:00
|
|
|
img.classList.add("nnwClicked");
|
|
|
|
|
2019-10-19 23:54:55 +02:00
|
|
|
const rect = img.getBoundingClientRect();
|
|
|
|
var message = {
|
|
|
|
x: rect.x,
|
|
|
|
y: rect.y,
|
|
|
|
width: rect.width,
|
2020-01-01 02:06:27 +01:00
|
|
|
height: rect.height,
|
|
|
|
imageTitle: img.title
|
2019-10-19 23:54:55 +02:00
|
|
|
};
|
2019-10-14 02:41:34 +02:00
|
|
|
message.imageURL = reader.result;
|
2019-10-19 23:54:55 +02:00
|
|
|
|
2019-10-14 02:41:34 +02:00
|
|
|
var jsonMessage = JSON.stringify(message);
|
|
|
|
window.webkit.messageHandlers.imageWasClicked.postMessage(jsonMessage);
|
2019-10-30 17:06:55 +01:00
|
|
|
|
2019-10-14 02:41:34 +02:00
|
|
|
}
|
2019-10-30 17:06:55 +01:00
|
|
|
|
2019-10-14 02:41:34 +02:00
|
|
|
} catch (error) {
|
2019-10-19 23:54:55 +02:00
|
|
|
hideNetworkLoading(img);
|
2019-10-14 02:41:34 +02:00
|
|
|
console.log('There has been a problem with your fetch operation: ', error.message);
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:47:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 23:25:06 +02:00
|
|
|
function showNetworkLoading(img) {
|
|
|
|
|
|
|
|
var wrapper = document.createElement("div");
|
|
|
|
wrapper.classList.add("activityIndicatorWrap");
|
|
|
|
img.parentNode.insertBefore(wrapper, img);
|
|
|
|
wrapper.appendChild(img);
|
|
|
|
|
|
|
|
var activityIndicatorImg = document.createElement("img");
|
|
|
|
activityIndicatorImg.classList.add("activityIndicator");
|
|
|
|
activityIndicatorImg.style.opacity = 0;
|
|
|
|
activityIndicatorImg.src = activityIndicator;
|
|
|
|
wrapper.appendChild(activityIndicatorImg);
|
|
|
|
|
2019-10-19 23:54:55 +02:00
|
|
|
// Wait a bit before showing the indicator
|
2019-10-19 23:25:06 +02:00
|
|
|
function showActivityIndicator() {
|
|
|
|
activityIndicatorImg.style.opacity = 1;
|
|
|
|
}
|
2019-10-19 23:54:55 +02:00
|
|
|
setTimeout(showActivityIndicator, 300);
|
2019-10-19 23:25:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function hideNetworkLoading(img) {
|
|
|
|
var wrapper = img.parentNode;
|
|
|
|
var wrapperParent = wrapper.parentNode;
|
|
|
|
wrapperParent.insertBefore(img, wrapper);
|
|
|
|
wrapperParent.removeChild(wrapper);
|
|
|
|
}
|
|
|
|
|
2019-10-16 23:40:49 +02:00
|
|
|
// Used to animate the transition to a fullscreen image
|
|
|
|
function hideClickedImage() {
|
|
|
|
var img = document.querySelector('.nnwClicked')
|
|
|
|
img.style.opacity = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to animate the transition from a fullscreen image
|
|
|
|
function showClickedImage() {
|
|
|
|
var img = document.querySelector('.nnwClicked')
|
|
|
|
img.classList.remove("nnwClicked");
|
|
|
|
img.style.opacity = 1
|
|
|
|
window.webkit.messageHandlers.imageWasShown.postMessage("");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the click listener for images
|
2019-10-13 22:47:11 +02:00
|
|
|
function imageClicks() {
|
2019-10-15 16:04:21 +02:00
|
|
|
window.onclick = function(event) {
|
2019-10-30 17:06:55 +01:00
|
|
|
if (event.target.matches('img')) {
|
2019-10-15 16:04:21 +02:00
|
|
|
imageWasClicked(event.target);
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 22:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the playsinline attribute to any HTML5 videos that don't have it.
|
|
|
|
// Without this attribute videos may autoplay and take over the whole screen
|
|
|
|
// on an iphone when viewing an article.
|
|
|
|
function inlineVideos() {
|
|
|
|
document.querySelectorAll("video").forEach(element => {
|
|
|
|
element.setAttribute("playsinline", true)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function postRenderProcessing() {
|
2019-10-30 17:06:55 +01:00
|
|
|
cancelImageLoad();
|
|
|
|
imageClicks();
|
|
|
|
inlineVideos();
|
2019-10-13 22:47:11 +02:00
|
|
|
}
|
2019-10-19 23:25:06 +02:00
|
|
|
|
|
|
|
const activityIndicator = "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PHN2ZyB4bWxuczpzdmc9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxucz0i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