Jump to content

Widget:UPDateOnline: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
Line 51: Line 51:


     /*
     /*
     * Get the latest UPDate Online article
     * --------------------------------
    * UPDate Online API
    * --------------------------------
     */
     */


Line 76: Line 78:
         .then(function (posts) {
         .then(function (posts) {


             if (!posts || !posts.length) {
             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:
             /*
             /*
             * --------------------------------
             * --------------------------------
             * FIND THE FIRST IMAGE AFTER
             * CREATE TEMPORARY ARTICLE DOM
            * THE FIRST NON-EMPTY PARAGRAPH
             * --------------------------------
             * --------------------------------
             */
             */
Line 153: Line 155:
             var article =
             var article =
                 document.createElement('div');
                 document.createElement('div');


             article.innerHTML =
             article.innerHTML =
Line 159: Line 162:


             /*
             /*
             * Find all paragraphs
             * --------------------------------
            * FIND FIRST NON-EMPTY PARAGRAPH
            * --------------------------------
             */
             */


Line 168: Line 173:
             var firstParagraph = null;
             var firstParagraph = null;


            /*
            * Find the first paragraph
            * containing actual text
            */


             for (
             for (
Line 183: Line 183:
                     paragraphs[i]
                     paragraphs[i]
                         .textContent
                         .textContent
                         .trim()
                         .trim() !== ''
                        .length > 0
                 ) {
                 ) {


Line 196: Line 195:


             /*
             /*
             * Find all images
             * --------------------------------
            * FIND ALL IMAGES
            * --------------------------------
             */
             */


Line 207: Line 208:


             /*
             /*
             * Find the first image that occurs
             * --------------------------------
             * AFTER the first paragraph
             * 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:
                         );
                         );


                    /*
                    * 4 means the image is
                    * following the paragraph
                    */


                     if (
                     if (
Line 244: Line 247:
                         break;
                         break;
                     }
                     }
                 }
                 }
             }
             }


Line 250: Line 255:
             /*
             /*
             * --------------------------------
             * --------------------------------
             * DISPLAY THE IMAGE
             * DISPLAY IMAGE
             * --------------------------------
             * --------------------------------
             */
             */
Line 257: Line 262:


                 /*
                 /*
                 * WordPress normally uses src,
                 * WordPress image URL
                * but check several possibilities
                * for future articles.
                 */
                 */


                 var imageUrl =
                 var imageUrl =
                     selectedImage.getAttribute('src') ||
                     selectedImage.getAttribute(
                    selectedImage.getAttribute('data-src') ||
                        'src'
                     selectedImage.getAttribute('data-lazy-src') ||
                     );
                    selectedImage.getAttribute('data-original');




                 /*
                 /*
                 * If there is no src, try srcset
                 * Fallbacks for lazy-loaded images
                 */
                 */


                 if (!imageUrl) {
                 if (!imageUrl) {


                     var srcset =
                     imageUrl =
                         selectedImage.getAttribute('srcset') ||
                         selectedImage.getAttribute(
                        selectedImage.getAttribute('data-srcset');
                            'data-src'
                        );


                }


                    if (srcset) {


                        /*
                if (!imageUrl) {
                        * Take the largest image
                        * from the srcset.
                        */


                        var sources =
                    imageUrl =
                            srcset.split(',');
                        selectedImage.getAttribute(
                            'data-lazy-src'
                        );
 
                }




                        var lastSource =
                if (!imageUrl) {
                            sources[sources.length - 1]
                                .trim();


                    imageUrl =
                        selectedImage.getAttribute(
                            'data-original'
                        );


                        imageUrl =
                            lastSource.split(' ')[0];
                    }
                 }
                 }




                 /*
                 /*
                 * Make sure the image URL is
                 * --------------------------------
                 * absolute.
                 * SET IMAGE
                * --------------------------------
                 */
                 */


                 if (imageUrl) {
                 if (imageUrl) {
                    if (
                        imageUrl.indexOf('http://') !== 0 &&
                        imageUrl.indexOf('https://') !== 0
                    ) {
                        imageUrl =
                            new URL(
                                imageUrl,
                                'https://upd.edu.ph'
                            ).href;
                    }


                     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}}