MediaWiki:Common.js: Difference between revisions

From Doc-Wiki
Jump to navigation Jump to search
Adding JavaScript fix for YouTube playlist iframe sources
Comprehensive fix for Level Up RN playlist embeds and thumbnails
Line 3: Line 3:




/* YouTube Playlists Gallery - Final Version */
 
 
/* Level Up RN - Comprehensive Fix for Playlists */
(function() {
(function() {
     'use strict';
     'use strict';
    // Only run on Level Up RN Videos category pages
    var pageName = mw.config.get('wgPageName');
    if (pageName !== 'Category:Level_Up_RN_Videos' && pageName !== 'Category:Level_Up_RN') return;


     mw.loader.using(['jquery'], function() {
     mw.loader.using(['jquery'], function() {
         $(document).ready(function() {
         $(document).ready(function() {
            // Find the playlists table and transform it
            var $table = $('.wikitable.sortable').first();
            if (!$table.length) return;


             var $gallery = $('<div class="playlists-gallery"></div>');
             // Fix 1: Create working iframe embeds for playlist pages
            $('.youtube-playlist-embed[data-playlist-id]').each(function() {
                var $container = $(this);
                var playlistId = $container.attr('data-playlist-id');


            $table.find('tr').each(function(index) {
                if (playlistId) {
                if (index === 0) return; // Skip header
                    var iframe = $('<iframe>')
                        .attr({
                            'src': 'https://www.youtube.com/embed/videoseries?list=' + playlistId,
                            'frameborder': '0',
                            'allow': 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture',
                            'allowfullscreen': ''
                        })
                        .css({
                            'position': 'absolute',
                            'top': '0',
                            'left': '0',
                            'width': '100%',
                            'height': '100%'
                        });


                var $row = $(this);
                    $container.empty().append(iframe);
                var $cells = $row.find('td');
                    console.log('Created iframe for playlist:', playlistId);
                }
            });


                if ($cells.length < 3) return;
            // Fix 2: Transform gallery table with real thumbnails
 
            var pageName = mw.config.get('wgPageName');
                var $titleCell = $cells.eq(0);
            if (pageName === 'Category:Level_Up_RN_Videos' || pageName === 'Category:Level_Up_RN') {
                 var $descCell = $cells.eq(1);
                 var $table = $('.wikitable.sortable').first();
                 var $linkCell = $cells.eq(2);
                 if (!$table.length) return;


                // Get playlist ID from data attribute
                 var $gallery = $('<div class="playlists-gallery"></div>');
                 var playlistId = $titleCell.attr('data-playlist-id');


                 var $titleLink = $titleCell.find('a').first();
                 $table.find('tr').each(function(index) {
                var wikiPageUrl = $titleLink.attr('href');
                    if (index === 0) return; // Skip header
                var title = $titleLink.text();
                var description = $descCell.text().trim();


                var $watchLink = $linkCell.find('a').first();
                    var $row = $(this);
                    var $cells = $row.find('td');


                if (!playlistId || !wikiPageUrl) return;
                    if ($cells.length < 3) return;


                // Create box
                    var $titleCell = $cells.eq(0);
                var $box = $('<div class="playlist-box"></div>');
                    var $descCell = $cells.eq(1);
                var $thumb = $('<div class="playlist-box-thumb"></div>');
                var $thumbLink = $('<a></a>').attr('href', wikiPageUrl);


                // Create image with fallback chain
                    var playlistId = $titleCell.attr('data-playlist-id');
                var $img = $('<img>')
                     var $titleLink = $titleCell.find('a').first();
                    .attr('alt', title)
                    var wikiPageUrl = $titleLink.attr('href');
                     .css({'width': '100%', 'height': '100%', 'object-fit': 'cover'})
                    var title = $titleLink.text();
                    .on('error', function() {
                    var description = $descCell.text().trim();
                        // If first attempt fails, try a different thumbnail
                        var currentSrc = $(this).attr('src');
                        if (currentSrc.indexOf('maxresdefault') !== -1) {
                            $(this).attr('src', 'https://i.ytimg.com/vi/' + playlistId + '/hqdefault.jpg');
                        } else if (currentSrc.indexOf('hqdefault') !== -1) {
                            $(this).attr('src', 'https://i.ytimg.com/vi/' + playlistId + '/mqdefault.jpg');
                        } else {
                            // Last resort: show a placeholder
                            $(this).replaceWith('<div style="width:100%;height:100%;background:#ccc;display:flex;align-items:center;justify-content:center;color:#666;font-size:48px;">▶</div>');
                        }
                    });


                // Try to fetch thumbnail using YouTube's img service
                    if (!playlistId || !wikiPageUrl) return;
                // We'll try the playlist thumbnail first
                fetchPlaylistThumbnail(playlistId, $img);


                $thumbLink.append($img);
                    // Create card
                $thumbLink.append('<div class="playlist-box-overlay">▶ Playlist</div>');
                    var $box = $('<div class="playlist-box"></div>');
                $thumb.append($thumbLink);
                    var $thumb = $('<div class="playlist-box-thumb"></div>');
                    var $thumbLink = $('<a></a>').attr('href', wikiPageUrl);


                var $info = $('<div class="playlist-box-info"></div>');
                    // Create image - try to get thumbnail via YouTube API
                $info.append('<div class="playlist-box-title">' + title + '</div>');
                    var $img = $('<img>')
                if (description) {
                        .attr('alt', title)
                    $info.append('<div style="margin: 8px 0; font-size: 13px; color: #666;">' + description + '</div>');
                        .css({
                }
                            'width': '100%',
                $info.append('<a href="' + wikiPageUrl + '" class="playlist-box-link">Watch Playlist →</a>');
                            'height': '100%',
                            'object-fit': 'cover',
                            'position': 'absolute',
                            'top': '0',
                            'left': '0'
                        });


                $box.append($thumb).append($info);
                    // Fetch thumbnail using JSONP
                $gallery.append($box);
                    $.ajax({
            });
                        url: 'https://www.youtube.com/oembed?url=https://www.youtube.com/playlist?list=' + playlistId + '&format=json',
                        dataType: 'jsonp',
                        success: function(data) {
                            if (data && data.thumbnail_url) {
                                $img.attr('src', data.thumbnail_url);
                            }
                        },
                        error: function() {
                            // Fallback: use generic YouTube playlist icon
                            $img.attr('src', 'data:image/svg+xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\' width=\'120\' height=\'90\'%3E%3Crect fill=\'%23E52D27\' width=\'120\' height=\'90\'/%3E%3Cpath fill=\'%23fff\' d=\'M60 20L30 70h20v10h20V70h20z\'/%3E%3C/svg%3E');
                        }
                    });


            if ($gallery.children().length > 0) {
                    $thumbLink.append($img);
                $table.replaceWith($gallery);
                    $thumbLink.append('<div class="playlist-box-overlay">▶ Playlist</div>');
            }
                    $thumb.append($thumbLink);


            function fetchPlaylistThumbnail(playlistId, $img) {
                    var $info = $('<div class="playlist-box-info"></div>');
                // Try to get thumbnail from YouTube's oembed service
                    $info.append('<div class="playlist-box-title">' + title + '</div>');
                var oembedUrl = 'https://www.youtube.com/oembed?url=https://www.youtube.com/playlist?list=' + playlistId + '&format=json';
                    if (description) {
                        $info.append('<div style="margin: 8px 0; font-size: 13px; color: #666;">' + description + '</div>');
                    }
                    $info.append('<a href="' + wikiPageUrl + '" class="playlist-box-link">Watch Playlist →</a>');


                $.ajax({
                    $box.append($thumb).append($info);
                    url: oembedUrl,
                     $gallery.append($box);
                    dataType: 'jsonp',
                    success: function(data) {
                        if (data && data.thumbnail_url) {
                            $img.attr('src', data.thumbnail_url);
                        } else {
                            // Fallback to standard format
                            $img.attr('src', 'https://i.ytimg.com/vi/' + playlistId + '/maxresdefault.jpg');
                        }
                     },
                    error: function() {
                        // Fallback: use playlist ID as video ID (may or may not work)
                        $img.attr('src', 'https://i.ytimg.com/vi/' + playlistId + '/maxresdefault.jpg');
                    }
                 });
                 });
            }
        });
    });
})();
/* Fix YouTube Playlist iframes */
(function() {
    'use strict';


    var playlistMap = {"Level_Up_RN/Fundamentals_of_Nursing": "PLs7jUj_Xjh6TUqvM2R-8gGj_UaQvdCbqy", "Level_Up_RN/Medical-Surgical_Nursing": "PLs7jUj_Xjh6SqCfxLJy0gPjODR_oqGgCJ", "Level_Up_RN/Pharmacology": "PLs7jUj_Xjh6RW9X0YuSNqh5eoRfmPCRlR", "Level_Up_RN/Pediatric_Nursing": "PLs7jUj_Xjh6S5u2wnVJLbP0FCM5YiYEDn", "Level_Up_RN/Maternity_Nursing": "PLs7jUj_Xjh6TVhjdqOeZ0TvQnm6Lk3KqI", "Level_Up_RN/Psychiatric_Mental_Health": "PLs7jUj_Xjh6TpGd4yFr6jWCgD0kKjHfG_", "Level_Up_RN/Pathophysiology": "PLs7jUj_Xjh6QXcJXm-VYHYMlKOyVlBMqS", "Level_Up_RN/Health_Assessment": "PLs7jUj_Xjh6S4KqN8lJCLXpqF0kVQKqYT", "Level_Up_RN/Lab_Values": "PLs7jUj_Xjh6SJMWMHJNaZNgr0kPQf_YOw", "Level_Up_RN/Skills_&_Procedures": "PLs7jUj_Xjh6S3PqXVYxIpvMnLrM6QC2Mz"};
                if ($gallery.children().length > 0) {
 
                     $table.replaceWith($gallery);
    mw.loader.using(['jquery'], function() {
                 }
        $(document).ready(function() {
            var pageName = mw.config.get('wgPageName');
            var playlistId = playlistMap[pageName];
 
            if (playlistId) {
                // Find YouTube iframes and fix their src
                $('iframe[data-extension="youtube"]').each(function() {
                     var $iframe = $(this);
                    var currentSrc = $iframe.attr('src');
 
                    // If src is broken or missing list parameter, fix it
                    if (currentSrc && (currentSrc.indexOf('videoseries?') !== -1 || currentSrc.indexOf('videoseries') !== -1)) {
                        var newSrc = '//www.youtube-nocookie.com/embed/videoseries?list=' + playlistId;
                        $iframe.attr('src', newSrc);
                        console.log('Fixed YouTube playlist iframe for:', pageName);
                    }
                 });
             }
             }
         });
         });
     });
     });
})();
})();

Revision as of 19:22, 17 January 2026

/* Any JavaScript here will be loaded for all users on every page load. */





/* Level Up RN - Comprehensive Fix for Playlists */
(function() {
    'use strict';

    mw.loader.using(['jquery'], function() {
        $(document).ready(function() {

            // Fix 1: Create working iframe embeds for playlist pages
            $('.youtube-playlist-embed[data-playlist-id]').each(function() {
                var $container = $(this);
                var playlistId = $container.attr('data-playlist-id');

                if (playlistId) {
                    var iframe = $('<iframe>')
                        .attr({
                            'src': 'https://www.youtube.com/embed/videoseries?list=' + playlistId,
                            'frameborder': '0',
                            'allow': 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture',
                            'allowfullscreen': ''
                        })
                        .css({
                            'position': 'absolute',
                            'top': '0',
                            'left': '0',
                            'width': '100%',
                            'height': '100%'
                        });

                    $container.empty().append(iframe);
                    console.log('Created iframe for playlist:', playlistId);
                }
            });

            // Fix 2: Transform gallery table with real thumbnails
            var pageName = mw.config.get('wgPageName');
            if (pageName === 'Category:Level_Up_RN_Videos' || pageName === 'Category:Level_Up_RN') {
                var $table = $('.wikitable.sortable').first();
                if (!$table.length) return;

                var $gallery = $('<div class="playlists-gallery"></div>');

                $table.find('tr').each(function(index) {
                    if (index === 0) return; // Skip header

                    var $row = $(this);
                    var $cells = $row.find('td');

                    if ($cells.length < 3) return;

                    var $titleCell = $cells.eq(0);
                    var $descCell = $cells.eq(1);

                    var playlistId = $titleCell.attr('data-playlist-id');
                    var $titleLink = $titleCell.find('a').first();
                    var wikiPageUrl = $titleLink.attr('href');
                    var title = $titleLink.text();
                    var description = $descCell.text().trim();

                    if (!playlistId || !wikiPageUrl) return;

                    // Create card
                    var $box = $('<div class="playlist-box"></div>');
                    var $thumb = $('<div class="playlist-box-thumb"></div>');
                    var $thumbLink = $('<a></a>').attr('href', wikiPageUrl);

                    // Create image - try to get thumbnail via YouTube API
                    var $img = $('<img>')
                        .attr('alt', title)
                        .css({
                            'width': '100%',
                            'height': '100%',
                            'object-fit': 'cover',
                            'position': 'absolute',
                            'top': '0',
                            'left': '0'
                        });

                    // Fetch thumbnail using JSONP
                    $.ajax({
                        url: 'https://www.youtube.com/oembed?url=https://www.youtube.com/playlist?list=' + playlistId + '&format=json',
                        dataType: 'jsonp',
                        success: function(data) {
                            if (data && data.thumbnail_url) {
                                $img.attr('src', data.thumbnail_url);
                            }
                        },
                        error: function() {
                            // Fallback: use generic YouTube playlist icon
                            $img.attr('src', 'data:image/svg+xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\' width=\'120\' height=\'90\'%3E%3Crect fill=\'%23E52D27\' width=\'120\' height=\'90\'/%3E%3Cpath fill=\'%23fff\' d=\'M60 20L30 70h20v10h20V70h20z\'/%3E%3C/svg%3E');
                        }
                    });

                    $thumbLink.append($img);
                    $thumbLink.append('<div class="playlist-box-overlay">▶ Playlist</div>');
                    $thumb.append($thumbLink);

                    var $info = $('<div class="playlist-box-info"></div>');
                    $info.append('<div class="playlist-box-title">' + title + '</div>');
                    if (description) {
                        $info.append('<div style="margin: 8px 0; font-size: 13px; color: #666;">' + description + '</div>');
                    }
                    $info.append('<a href="' + wikiPageUrl + '" class="playlist-box-link">Watch Playlist →</a>');

                    $box.append($thumb).append($info);
                    $gallery.append($box);
                });

                if ($gallery.children().length > 0) {
                    $table.replaceWith($gallery);
                }
            }
        });
    });
})();