Widget:UPDateOnline: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 51: | Line 51: | ||
/* | /* | ||
* | * -------------------------------- | ||
* UPDate Online API | |||
* -------------------------------- | |||
*/ | */ | ||
| Line 76: | Line 78: | ||
.then(function (posts) { | .then(function (posts) { | ||
if (!posts || | if (!posts || posts.length === 0) { | ||
throw new Error( | throw new Error( | ||
'No UPDate Online article found' | 'No UPDate Online article found' | ||
| Line 130: | Line 132: | ||
var date = | var date = | ||
new Date(post.date); | new Date(post.date); | ||
document.getElementById( | document.getElementById( | ||
| Line 146: | Line 149: | ||
/* | /* | ||
* -------------------------------- | * -------------------------------- | ||
* | * CREATE TEMPORARY ARTICLE DOM | ||
* -------------------------------- | * -------------------------------- | ||
*/ | */ | ||
| Line 153: | Line 155: | ||
var article = | var article = | ||
document.createElement('div'); | document.createElement('div'); | ||
article.innerHTML = | article.innerHTML = | ||
| Line 159: | Line 162: | ||
/* | /* | ||
* | * -------------------------------- | ||
* FIND FIRST NON-EMPTY PARAGRAPH | |||
* -------------------------------- | |||
*/ | */ | ||
| Line 168: | Line 173: | ||
var firstParagraph = null; | var firstParagraph = null; | ||
for ( | for ( | ||
| Line 183: | Line 183: | ||
paragraphs[i] | paragraphs[i] | ||
.textContent | .textContent | ||
.trim() | .trim() !== '' | ||
) { | ) { | ||
| Line 196: | Line 195: | ||
/* | /* | ||
* | * -------------------------------- | ||
* FIND ALL IMAGES | |||
* -------------------------------- | |||
*/ | */ | ||
| Line 207: | Line 208: | ||
/* | /* | ||
* | * -------------------------------- | ||
* AFTER | * FIND FIRST IMAGE AFTER | ||
* FIRST PARAGRAPH | |||
* -------------------------------- | |||
*/ | */ | ||
| Line 222: | Line 225: | ||
images[j]; | images[j]; | ||
/* | |||
* Check whether this image | |||
* appears after the paragraph. | |||
*/ | |||
var position = | var position = | ||
| Line 228: | Line 236: | ||
); | ); | ||
if ( | if ( | ||
| Line 244: | Line 247: | ||
break; | break; | ||
} | } | ||
} | } | ||
} | } | ||
| Line 250: | Line 255: | ||
/* | /* | ||
* -------------------------------- | * -------------------------------- | ||
* DISPLAY | * DISPLAY IMAGE | ||
* -------------------------------- | * -------------------------------- | ||
*/ | */ | ||
| Line 257: | Line 262: | ||
/* | /* | ||
* WordPress | * WordPress image URL | ||
*/ | */ | ||
var imageUrl = | var imageUrl = | ||
selectedImage.getAttribute( | selectedImage.getAttribute( | ||
'src' | |||
); | |||
/* | /* | ||
* | * Fallbacks for lazy-loaded images | ||
*/ | */ | ||
if (!imageUrl) { | if (!imageUrl) { | ||
imageUrl = | |||
selectedImage.getAttribute( | selectedImage.getAttribute( | ||
'data-src' | |||
); | |||
} | |||
if (!imageUrl) { | |||
imageUrl = | |||
selectedImage.getAttribute( | |||
'data-lazy-src' | |||
); | |||
} | |||
if (!imageUrl) { | |||
imageUrl = | |||
selectedImage.getAttribute( | |||
'data-original' | |||
); | |||
} | } | ||
/* | /* | ||
* | * -------------------------------- | ||
* | * SET IMAGE | ||
* -------------------------------- | |||
*/ | */ | ||
if (imageUrl) { | if (imageUrl) { | ||
var outputImage = | var outputImage = | ||
| Line 333: | Line 324: | ||
outputImage.alt = | outputImage.alt = | ||
selectedImage.getAttribute('alt') || | selectedImage.getAttribute( | ||
'alt' | |||
) || | |||
post.title.rendered; | post.title.rendered; | ||
| Line 339: | Line 332: | ||
outputImage.style.display = | outputImage.style.display = | ||
'block'; | 'block'; | ||
} | } | ||
| Line 360: | Line 354: | ||
).style.display = | ).style.display = | ||
'flex'; | 'flex'; | ||
}) | }) | ||
Revision as of 23:27, 6 October 2026
UPDate Online Widget
Displays the latest UPDate Online article and the first image appearing after the opening paragraph.
Usage
{{#widget:UPDateOnline}}