MediaWiki:Common.js: Difference between revisions

From MEG Core
Jump to navigation Jump to search
Content deleted Content added
Created page with "mw.hook('wikipage.content').add(function($c){ $c.find('a[target="_blank"]').each(function(){ if (!this.rel || this.rel.indexOf('noopener') === -1) this.rel = (this.rel ? this.rel + ' ' : '') + 'noopener'; if (this.rel.indexOf('noreferrer') === -1) this.rel += ' noreferrer'; }); });"
 
No edit summary
 
Line 1: Line 1:
// MediaWiki:Common.js
mw.hook('wikipage.content').add(function($c){
$c.find('a[target="_blank"]').each(function(){
(function () {
function tagAll($root) {
if (!this.rel || this.rel.indexOf('noopener') === -1) this.rel = (this.rel ? this.rel + ' ' : '') + 'noopener';
$root.find('a[target="_blank"]').each(function () {
if (this.rel.indexOf('noreferrer') === -1) this.rel += ' noreferrer';
var rel = this.rel || '';
});
if (!/\bnoopener\b/.test(rel)) rel += (rel ? ' ' : '') + 'noopener';
});
if (!/\bnoreferrer\b/.test(rel)) rel += (rel ? ' ' : '') + 'noreferrer';
this.rel = rel;
});
}

// Run once on the whole document (covers header/indicators/skin chrome)
$(function () { tagAll($(document)); });

// Run again for content inserted later (e.g., after navigation/ajax)
mw.hook('wikipage.content').add(function ($c) { tagAll($c); });
})();

Latest revision as of 13:36, 6 November 2025

// MediaWiki:Common.js
(function () {
  function tagAll($root) {
    $root.find('a[target="_blank"]').each(function () {
      var rel = this.rel || '';
      if (!/\bnoopener\b/.test(rel)) rel += (rel ? ' ' : '') + 'noopener';
      if (!/\bnoreferrer\b/.test(rel)) rel += (rel ? ' ' : '') + 'noreferrer';
      this.rel = rel;
    });
  }

  // Run once on the whole document (covers header/indicators/skin chrome)
  $(function () { tagAll($(document)); });

  // Run again for content inserted later (e.g., after navigation/ajax)
  mw.hook('wikipage.content').add(function ($c) { tagAll($c); });
})();