Widget:UPDateOnline: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 227: | Line 227: | ||
*/ | */ | ||
// Find the first image AFTER the opening paragraph | |||
var temp = document.createElement('div'); | |||
temp.innerHTML = post.content.rendered; | |||
var paragraphs = temp.querySelectorAll('p'); | |||
var firstParagraph = null; | |||
// Find the first paragraph that actually contains text | |||
for (var i = 0; i < paragraphs.length; i++) { | |||
if (paragraphs[i].textContent.trim().length > 0) { | |||
firstParagraph = paragraphs[i]; | |||
break; | |||
} | |||
} | |||
var selectedImage = null; | |||
if (firstParagraph) { | |||
// Look at all paragraphs and images in document order | |||
var elements = temp.querySelectorAll('p, img'); | |||
var passedFirstParagraph = false; | |||
for (var i = 0; i < elements.length; i++) { | |||
var element = elements[i]; | |||
if (element === firstParagraph) { | |||
passedFirstParagraph = true; | |||
continue; | |||
} | } | ||
if ( | |||
passedFirstParagraph && | |||
element.tagName.toLowerCase() === 'img' && | |||
!firstParagraph.contains(element) | |||
) { | |||
selectedImage = element; | |||
break; | |||
} | |||
} | |||
} | |||
// Put the image into our widget | |||
if (selectedImage) { | |||
var imageUrl = | |||
selectedImage.getAttribute('src') || | |||
selectedImage.getAttribute('data-src') || | |||
selectedImage.getAttribute('data-lazy-src'); | |||
if (imageUrl) { | |||
var outputImage = | |||
document.getElementById('update-online-image'); | |||
outputImage.src = imageUrl; | |||
outputImage.alt = | |||
selectedImage.getAttribute('alt') || | |||
post.title.rendered; | |||
} | outputImage.style.display = 'block'; | ||
} | |||
} | |||
Revision as of 23:23, 6 October 2026
UPDate Online Widget
Displays the latest UPDate Online article and the first image appearing after the opening paragraph.
Usage
{{#widget:UPDateOnline}}