Last edited one month ago
by WikiSysop

MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
 
(22 intermediate revisions by the same user not shown)
Line 8: Line 8:
});
});


// hide the sidebar navigation.
// Function to convert Blue spice iframe to real iframes
//document.querySelector('#sb-pri-tgl-btn').style.display = 'none';
function convertMediaWikiIframesToReal() {
//document.getElementById('sb-pri-tgl-btn').remove();
    // Look for paragraphs that contain escaped iframe tags
    var paragraphs = document.querySelectorAll('p');
    for (var i = 0; i < paragraphs.length; i++) {
        var paragraph = paragraphs[i];
        var content = paragraph.innerHTML;
        // Check if this paragraph contains an escaped iframe
        if (content.indexOf('&lt;iframe') !== -1 && content.indexOf('&lt;/iframe&gt;') !== -1) {
            // Find any links inside this iframe text
            var links = paragraph.querySelectorAll('a.external');


            if (links.length > 0) {
                // Use the href from the first link as our iframe src
                var url = links[0].href;


                // Create an actual iframe element
                var iframe = document.createElement('iframe');
                iframe.src = url;
                iframe.width = "100%";
                iframe.height = "2400px";
                iframe.style.border = "none";


// Function to find text iframes and convert them to real iframes
                // Replace the paragraph with the iframe
function convertTextIframesToReal() {
                if (paragraph.parentNode) {
  // Get all elements in the document
                    paragraph.parentNode.replaceChild(iframe, paragraph);
  const allElements = document.querySelectorAll('*');
                    console.log('Converted MediaWiki iframe to real iframe with URL: ' + url);
 
                }
  // Look for elements with text content that contains iframe tags
            }
  allElements.forEach(element => {
        }
     if (element.childNodes && element.childNodes.length > 0) {
    }
      element.childNodes.forEach(node => {
 
         // Check if this is a text node containing an iframe
    // Now hide the #title-section - with better error handling
         if (node.nodeType === 3 && node.textContent.trim().includes('<iframe')) {
    var titleSection = document.querySelector('#title-section');
          const text = node.textContent.trim();
   
         
    if (!titleSection) {
          // Use regex to extract the URL from src attribute
        console.log('title-section not found, retrying in 100ms...');
          const srcMatch = text.match(/src="([^"]+)"/);
        setTimeout(convertMediaWikiIframesToReal, 100);
         
        return;
          if (srcMatch && srcMatch[1]) {
    }
            const url = srcMatch[1];
 
              
    console.log('Found title-section:', titleSection);
             // Create a real iframe element with src attribute
   
             const iframe = document.createElement('iframe');
    // Look for the correct page title element - it's an h1 with id "firstHeading"
             iframe.src = url;
    var pageTitle = document.querySelector('#firstHeading');
   
     if (pageTitle) {
        console.log('Found page title:', pageTitle.textContent);
          
         if (pageTitle.textContent.trim() === 'Rt-search') {
            console.log('Hiding title section for Rt-search page');
            titleSection.style.display = 'none';
 
            // Now we hide the form #bs-extendedsearch-box
            var searchBox = document.querySelector('#bs-extendedsearch-box');
            if (searchBox) {
                searchBox.style.display = 'none';
                console.log('Hidden search box');
            } else {
                console.log('Search box not found');
            }
 
            // Make the background grey
            var body = document.querySelector('body');
            if (body) {
                body.style.backgroundColor = '#e0e0e0';
                console.log('Set body background color');
             }
 
             // Now we hide the aftercontent
             var afterContent = document.querySelector('#aftercontent');
             if (afterContent) {
                afterContent.style.display = 'none';
                console.log('Hidden aftercontent');
            }
              
              
             // You can add additional attributes as needed
             // Hide the access restriction alert
             iframe.width = "100%";
             var alertContainer = document.querySelector('#mwstake-alert-container');
             iframe.height = "500px";
             if (alertContainer) {
            iframe.style.border = "none";
                alertContainer.style.display = 'none';
                console.log('Hidden access restriction alert');
            }
              
              
             // Replace the text node with the real iframe
             var main = document.querySelector('#main');
             node.parentNode.replaceChild(iframe, node);
             if (main) {
             console.log('Converted iframe with URL:', url);
                main.style.backgroundColor = '#e0e0e0';
          }
                console.log('Set main background color');
            }
        } else {
             console.log('Page title is not Rt-search, it is:', pageTitle.textContent.trim());
         }
         }
      });
    } else {
        console.log('Page title element not found');
     }
     }
  });
}
}


// Call the function when the page is loaded
// Enhanced page load handling with multiple fallbacks
document.addEventListener('DOMContentLoaded', convertTextIframesToReal);
function initializeWhenReady() {
    // Try immediate execution
    convertMediaWikiIframesToReal();
   
    // Also set up additional fallbacks
    setTimeout(convertMediaWikiIframesToReal, 500);
    setTimeout(convertMediaWikiIframesToReal, 1000);
}
 
// Multiple event listeners for different loading states
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initializeWhenReady);
} else if (document.readyState === 'interactive') {
    // DOM is loaded but resources might still be loading
    initializeWhenReady();
} else {
    // Document is fully loaded
    initializeWhenReady();
}


// If you need to run it immediately on an already loaded page
// Also try when the window is fully loaded
convertTextIframesToReal();
window.addEventListener('load', convertMediaWikiIframesToReal);

Latest revision as of 18:42, 23 May 2025

mw.loader.using('mediawiki.user').then(function () {
    mw.user.getGroups().then(function (groups) {
        console.log('User groups:', groups);
        if (groups.includes('sysop')) {
            console.log('User is an administrator.');
        }
    });
});

// Function to convert Blue spice iframe to real iframes
function convertMediaWikiIframesToReal() {
    // Look for paragraphs that contain escaped iframe tags
    var paragraphs = document.querySelectorAll('p');
    for (var i = 0; i < paragraphs.length; i++) {
        var paragraph = paragraphs[i];
        var content = paragraph.innerHTML;
        // Check if this paragraph contains an escaped iframe
        if (content.indexOf('&lt;iframe') !== -1 && content.indexOf('&lt;/iframe&gt;') !== -1) {
            // Find any links inside this iframe text
            var links = paragraph.querySelectorAll('a.external');

            if (links.length > 0) {
                // Use the href from the first link as our iframe src
                var url = links[0].href;

                // Create an actual iframe element
                var iframe = document.createElement('iframe');
                iframe.src = url;
                iframe.width = "100%";
                iframe.height = "2400px";
                iframe.style.border = "none";

                // Replace the paragraph with the iframe
                if (paragraph.parentNode) {
                    paragraph.parentNode.replaceChild(iframe, paragraph);
                    console.log('Converted MediaWiki iframe to real iframe with URL: ' + url);
                }
            }
        }
    }

    // Now hide the #title-section - with better error handling
    var titleSection = document.querySelector('#title-section');
    
    if (!titleSection) {
        console.log('title-section not found, retrying in 100ms...');
        setTimeout(convertMediaWikiIframesToReal, 100);
        return;
    }

    console.log('Found title-section:', titleSection);
    
    // Look for the correct page title element - it's an h1 with id "firstHeading"
    var pageTitle = document.querySelector('#firstHeading');
    
    if (pageTitle) {
        console.log('Found page title:', pageTitle.textContent);
        
        if (pageTitle.textContent.trim() === 'Rt-search') {
            console.log('Hiding title section for Rt-search page');
            titleSection.style.display = 'none';

            // Now we hide the form #bs-extendedsearch-box
            var searchBox = document.querySelector('#bs-extendedsearch-box');
            if (searchBox) {
                searchBox.style.display = 'none';
                console.log('Hidden search box');
            } else {
                console.log('Search box not found');
            }

            // Make the background grey
            var body = document.querySelector('body');
            if (body) {
                body.style.backgroundColor = '#e0e0e0';
                console.log('Set body background color');
            }

            // Now we hide the aftercontent
            var afterContent = document.querySelector('#aftercontent');
            if (afterContent) {
                afterContent.style.display = 'none';
                console.log('Hidden aftercontent');
            }
            
            // Hide the access restriction alert
            var alertContainer = document.querySelector('#mwstake-alert-container');
            if (alertContainer) {
                alertContainer.style.display = 'none';
                console.log('Hidden access restriction alert');
            }
            
            var main = document.querySelector('#main');
            if (main) {
                main.style.backgroundColor = '#e0e0e0';
                console.log('Set main background color');
            }
        } else {
            console.log('Page title is not Rt-search, it is:', pageTitle.textContent.trim());
        }
    } else {
        console.log('Page title element not found');
    }
}

// Enhanced page load handling with multiple fallbacks
function initializeWhenReady() {
    // Try immediate execution
    convertMediaWikiIframesToReal();
    
    // Also set up additional fallbacks
    setTimeout(convertMediaWikiIframesToReal, 500);
    setTimeout(convertMediaWikiIframesToReal, 1000);
}

// Multiple event listeners for different loading states
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initializeWhenReady);
} else if (document.readyState === 'interactive') {
    // DOM is loaded but resources might still be loading
    initializeWhenReady();
} else {
    // Document is fully loaded
    initializeWhenReady();
}

// Also try when the window is fully loaded
window.addEventListener('load', convertMediaWikiIframesToReal);