MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 9: Line 9:
     }
     }
   }  
   }  
}
};
 
// Force links to open in new tab in embed mode
if (document.body.classList.contains('vc-embed-mode')) {
  document.addEventListener('DOMContentLoaded', function () {
    const links = document.querySelectorAll('a[href]');
   
    links.forEach(link => {
      const href = link.getAttribute('href');
 
      // Skip anchors and empty links
      if (!href || href.startsWith('#')) return;
 
      // Make all links open in new tab
      link.setAttribute('target', '_blank');
      link.setAttribute('rel', 'noopener noreferrer');
    });
  });
}


/* ── Embed: dynamic height + anchor scroll ── */
( function () {
( function () {
   if ( window === window.top ) return;
   if ( window === window.top ) return;
Line 34: Line 17:
   function sendHeight() {
   function sendHeight() {
     var h = document.body ? document.body.scrollHeight : 0;
     var h = document.body ? document.body.scrollHeight : 0;
     if ( h > 0 ) window.parent.postMessage( { embedHeight: h }, '*' );
     if ( h > 0 ) window.parent.postMessage( { embedHeight: h + 10 }, '*' );
   }
   }


  // Fire on multiple events to catch all page types
   document.addEventListener( 'DOMContentLoaded', sendHeight );
   document.addEventListener( 'DOMContentLoaded', sendHeight );
   window.addEventListener( 'load', function () {
   window.addEventListener( 'load', function () {
Line 48: Line 30:
   }
   }


  // Anchor scroll forwarding
   document.addEventListener( 'click', function ( e ) {
   document.addEventListener( 'click', function ( e ) {
     var link = e.target.closest( 'a' );
     var link = e.target.closest( 'a' );
Line 60: Line 41:
     window.parent.postMessage( { embedScrollTo: target.getBoundingClientRect().top + window.scrollY }, '*' );
     window.parent.postMessage( { embedScrollTo: target.getBoundingClientRect().top + window.scrollY }, '*' );
   } );
   } );
} )();
/* ── Embed: open links in new tab (only when ?embed=1) ── */
( function () {
  if ( window === window.top ) return;
  if ( !window.location.search.includes( 'embed=1' ) ) return;
  function retargetLinks() {
    var links = document.querySelectorAll( 'a[href]' );
    for ( var i = 0; i < links.length; i++ ) {
      var href = links[ i ].getAttribute( 'href' );
      if ( !href || href.charAt( 0 ) === '#' ) continue;
      links[ i ].setAttribute( 'target', '_blank' );
      links[ i ].setAttribute( 'rel', 'noopener noreferrer' );
    }
  }
  window.addEventListener( 'load', retargetLinks );
  if ( typeof mw !== 'undefined' ) {
    mw.hook( 'wikipage.content' ).add( retargetLinks );
  }
} )();
} )();