Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/js/nntpchan/expand-image.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-04-30 17:26:14 +05:00
//
// expand images inline
//
// released into the public domain by Jeff on 2016-04-30
//
// is the filename matching an image?
function filenameIsImage(fname) {
2016-05-02 02:56:47 +05:00
return /\.(gif|jpeg|jpg|png|webp)/.test(fname.toLowerCase());
2016-04-30 17:26:14 +05:00
}
// setup image inlining for 1 element
function setupInlineImage(thumb, url) {
if(thumb.inlineIsSetUp) return;
thumb.inlineIsSetUp = true;
var img = thumb.querySelector("img.thumbnail");
var expanded = false;
2016-04-30 17:30:14 +05:00
var oldurl = img.src;
2016-04-30 17:42:54 +05:00
thumb.onclick = function() {
2016-04-30 17:26:14 +05:00
if (expanded) {
img.setAttribute("class", "thumbnail");
img.src = oldurl;
expanded = false;
} else {
2016-04-30 17:51:01 +05:00
img.setAttribute("class", "expanded-thumbnail");
2016-04-30 17:26:14 +05:00
img.src = url;
expanded = true;
}
2016-04-30 17:42:54 +05:00
return false;
}
2016-04-30 17:26:14 +05:00
}
// set up image inlining for all applicable children in an element
function setupInlineImageIn(element) {
var thumbs = element.querySelectorAll("a.file");
for ( var i = 0 ; i < thumbs.length ; i++ ) {
2016-04-30 17:32:28 +05:00
var url = thumbs[i].href;
2016-04-30 17:26:14 +05:00
if (filenameIsImage(url)) {
// match
2016-04-30 17:31:37 +05:00
console.log("matched url", url);
2016-04-30 17:26:14 +05:00
setupInlineImage(thumbs[i], url);
}
}
}
onready(function(){
// Setup Javascript events for document
setupInlineImageIn(document);
// Setup Javascript events via updatoer
if (window.MutationObserver) {
var observer = new MutationObserver(function(mutations) {
for (var i = 0; i < mutations.length; i++) {
var additions = mutations[i].addedNodes;
if (additions == null) continue;
for (var j = 0; j < additions.length; j++) {
var node = additions[j];
if (node.nodeType == 1) {
setupInlineImageIn(node);
}
}
}
});
observer.observe(document.body, {childList: true, subtree: true});
}
2016-04-30 17:30:14 +05:00
});