Last edited one month ago
by WikiSysop

MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
Line 14: Line 14:




// Function to find text iframes and convert them to real iframes
// Function to convert MediaWiki processed iframe text to real iframes
function convertTextIframesToReal() {
function convertMediaWikiIframesToReal() {
console.log("convertTextIframesToReal()");
   // Look for paragraphs that contain escaped iframe tags
   // Get all elements in the document
   var paragraphs = document.querySelectorAll('p');
   var allElements = document.querySelectorAll('*');
    
    
  // Look for elements with text content that contains iframe tags
   for (var i = 0; i < paragraphs.length; i++) {
   for (var i = 0; i < allElements.length; i++) {
     var paragraph = paragraphs[i];
     var element = allElements[i];
    var content = paragraph.innerHTML;
      
      
     if (element.childNodes && element.childNodes.length > 0) {
    // Check if this paragraph contains an escaped iframe
       for (var j = 0; j < element.childNodes.length; j++) {
     if (content.indexOf('&lt;iframe') !== -1 && content.indexOf('&lt;/iframe&gt;') !== -1) {
         var node = element.childNodes[j];
        
      // 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;
          
          
         // Check if this is a text node containing an iframe
         // Create an actual iframe element
         if (node.nodeType === 3 && node.textContent.trim().indexOf('<iframe') !== -1) {
         var iframe = document.createElement('iframe');
          var text = node.textContent.trim();
        iframe.src = url;
         
        iframe.width = "100%";
          // Use regex to extract the URL from src attribute
        iframe.height = "500px";
          var srcMatch = /src="([^"]+)"/.exec(text);
        iframe.style.border = "none";
         
       
          if (srcMatch && srcMatch[1]) {
        // Replace the paragraph with the iframe
            var url = srcMatch[1];
        if (paragraph.parentNode) {
           
          paragraph.parentNode.replaceChild(iframe, paragraph);
            // Create a real iframe element with src attribute
          console.log('Converted MediaWiki iframe to real iframe with URL: ' + url);
            var iframe = document.createElement('iframe');
            iframe.src = url;
           
            // You can add additional attributes as needed
            iframe.width = "100%";
            iframe.height = "500px";
            iframe.style.border = "none";
           
            // Replace the text node with the real iframe
            node.parentNode.replaceChild(iframe, node);
            console.log('Converted iframe with URL: ' + url);
          }
         }
         }
       }
       }
Line 59: Line 52:
// Call the function when the page is loaded
// Call the function when the page is loaded
if (document.readyState === 'loading') {
if (document.readyState === 'loading') {
   document.addEventListener('DOMContentLoaded', convertTextIframesToReal);
   document.addEventListener('DOMContentLoaded', convertMediaWikiIframesToReal);
} else {
} else {
   // If the page is already loaded
   // If the page is already loaded
   convertTextIframesToReal();
   convertMediaWikiIframesToReal();
}
}

Revision as of 22:03, 14 March 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.');
        }
    });
});

//	hide the sidebar navigation. 
//document.querySelector('#sb-pri-tgl-btn').style.display = 'none';
//document.getElementById('sb-pri-tgl-btn').remove();



// Function to convert MediaWiki processed iframe text 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 = "500px";
        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);
        }
      }
    }
  }
}

// Call the function when the page is loaded
if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', convertMediaWikiIframesToReal);
} else {
  // If the page is already loaded
  convertMediaWikiIframesToReal();
}