Widget:UPDateOnline: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 13: | Line 13: | ||
<img id="update-online-image" | <img id="update-online-image" | ||
src="" | src="" | ||
alt=""> | alt="" | ||
style="display:none;"> | |||
</div> | </div> | ||
| Line 50: | Line 51: | ||
/* | /* | ||
* Get the latest UPDate Online article | * Get the latest UPDate Online article | ||
*/ | */ | ||
| Line 60: | Line 61: | ||
fetch(api) | fetch(api) | ||
.then(function (response) { | |||
if (!response.ok) { | |||
throw new Error( | |||
'Unable to retrieve UPDate Online' | |||
); | |||
} | |||
return response.json(); | |||
}) | |||
.then(function (posts) { | |||
if (!posts || !posts.length) { | |||
throw new Error( | |||
'No UPDate Online article found' | |||
); | |||
} | |||
var post = posts[0]; | |||
/* | |||
* -------------------------------- | |||
* TITLE | |||
* -------------------------------- | |||
*/ | |||
document.getElementById( | |||
'update-online-title' | |||
).innerHTML = | |||
post.title.rendered; | |||
/* | |||
* -------------------------------- | |||
* EXCERPT | |||
* -------------------------------- | |||
*/ | |||
document.getElementById( | |||
'update-online-excerpt' | |||
).innerHTML = | |||
post.excerpt.rendered; | |||
/* | |||
* -------------------------------- | |||
* ARTICLE LINK | |||
* -------------------------------- | |||
*/ | |||
document.getElementById( | |||
'update-online-link' | |||
).href = | |||
post.link; | |||
/* | |||
* -------------------------------- | |||
* DATE | |||
* -------------------------------- | |||
*/ | |||
var date = | |||
new Date(post.date); | |||
document.getElementById( | |||
'update-online-date' | |||
).textContent = | |||
date.toLocaleDateString( | |||
'en-US', | |||
{ | |||
year: 'numeric', | |||
month: 'long', | |||
day: 'numeric' | |||
} | |||
); | |||
/* | |||
* -------------------------------- | |||
* FIND THE FIRST IMAGE AFTER | |||
* THE FIRST NON-EMPTY PARAGRAPH | |||
* -------------------------------- | |||
*/ | |||
var article = | |||
document.createElement('div'); | |||
article.innerHTML = | |||
post.content.rendered; | |||
/* | |||
* Find all paragraphs | |||
*/ | |||
var paragraphs = | |||
article.querySelectorAll('p'); | |||
var firstParagraph = null; | |||
/* | /* | ||
* | * Find the first paragraph | ||
* | * containing actual text | ||
*/ | */ | ||
var | for ( | ||
firstParagraph | var i = 0; | ||
i < paragraphs.length; | |||
i++ | |||
) { | |||
if ( | |||
paragraphs[i] | |||
.textContent | |||
.trim() | |||
.length > 0 | |||
) { | |||
firstParagraph = | |||
paragraphs[i]; | |||
break; | |||
} | |||
} | |||
/* | |||
* Find all images | |||
*/ | |||
var images = | |||
article.querySelectorAll('img'); | |||
var selectedImage = null; | |||
/* | |||
* Find the first image that occurs | |||
* AFTER the first paragraph | |||
*/ | |||
if (firstParagraph) { | |||
for ( | |||
var j = 0; | |||
j < images.length; | |||
j++ | |||
) { | ) { | ||
var image = | |||
images[j]; | |||
var position = | |||
firstParagraph.compareDocumentPosition( | |||
image | |||
); | |||
/* | |||
* 4 means the image is | |||
* following the paragraph | |||
*/ | |||
if ( | |||
position & | |||
Node.DOCUMENT_POSITION_FOLLOWING | |||
) { | |||
selectedImage = | |||
image; | |||
break; | |||
} | |||
} | } | ||
} | |||
/* | |||
* -------------------------------- | |||
* DISPLAY THE IMAGE | |||
* -------------------------------- | |||
*/ | |||
if (selectedImage) { | |||
/* | /* | ||
* | * WordPress normally uses src, | ||
* | * but check several possibilities | ||
* for future articles. | |||
*/ | */ | ||
var | var imageUrl = | ||
selectedImage.getAttribute('src') || | |||
selectedImage.getAttribute('data-src') || | |||
selectedImage.getAttribute('data-lazy-src') || | |||
selectedImage.getAttribute('data-original'); | |||
/* | |||
* If there is no src, try srcset | |||
*/ | |||
if (!imageUrl) { | |||
var srcset = | |||
selectedImage.getAttribute('srcset') || | |||
selectedImage.getAttribute('data-srcset'); | |||
if (srcset) { | |||
/* | |||
* Take the largest image | |||
* from the srcset. | |||
*/ | |||
var sources = | |||
srcset.split(','); | |||
var lastSource = | |||
sources[sources.length - 1] | |||
.trim(); | |||
imageUrl = | |||
lastSource.split(' ')[0]; | |||
} | |||
} | |||
/ | /* | ||
* Make sure the image URL is | |||
* absolute. | |||
*/ | |||
if (imageUrl) { | |||
if ( | if ( | ||
imageUrl.indexOf('http://') !== 0 && | |||
imageUrl.indexOf('https://') !== 0 | |||
) { | |||
imageUrl = | |||
new URL( | |||
imageUrl, | |||
'https://upd.edu.ph' | |||
).href; | |||
} | |||
var outputImage = | |||
document.getElementById( | |||
'update-online-image' | |||
); | |||
outputImage.src = | |||
imageUrl; | |||
outputImage.alt = | |||
selectedImage.getAttribute('alt') || | |||
post.title.rendered; | |||
outputImage.style.display = | |||
'block'; | |||
} | |||
} | |||
} | |||
/* | |||
* -------------------------------- | |||
* SHOW ARTICLE | |||
* -------------------------------- | |||
*/ | |||
document.getElementById( | |||
'update-online-loading' | |||
).style.display = | |||
'none'; | |||
document.getElementById( | |||
'update-online-content' | |||
).style.display = | |||
'flex'; | |||
}) | |||
/* | |||
* -------------------------------- | |||
* ERROR HANDLING | |||
* -------------------------------- | |||
*/ | |||
.catch(function (error) { | |||
document.getElementById( | |||
'update-online-loading' | |||
).innerHTML = | |||
'Unable to load the latest UPDate Online article.'; | |||
console.error(error); | |||
}); | |||
})(); | })(); | ||
Revision as of 23:25, 6 October 2026
UPDate Online Widget
Displays the latest UPDate Online article and the first image appearing after the opening paragraph.
Usage
{{#widget:UPDateOnline}}