「MediaWiki:Common.js」の版間の差分

提供: 小樽のじかん事典
編集の要約なし
編集の要約なし
 
(同じ利用者による、間の17版が非表示)
1行目: 1行目:
/* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */
/**
mw.loader.using('mediawiki.util', function () {
* MediaWiki 1.44.3 アカウント作成画面 無限ループ防止版
* 既に書き換え済みの場合は処理をスキップするように修正しました。
*/
(function () {
  function enforce() {
    if (!window.mw || !mw.config) return;
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'CreateAccount') return;


/* ===== 表示名入力欄を追加 ===== */
    // 1. ラベルの書き換え
function addNameField() {
    var labels = document.getElementsByTagName('label');
const form = document.querySelector('.comment-form');
    for (var i = 0; i < labels.length; i++) {
if (!form || form.querySelector('.comment-name-input')) return;
      var label = labels[i];


const wrapper = document.createElement('div');
      // メールアドレス
wrapper.className = 'comment-name-input';
      if (label.getAttribute('for') === 'wpEmail' || (label.innerHTML || '').indexOf('メールアドレス') !== -1) {
        if ((label.innerHTML || '').indexOf('(省略可能)') !== -1) {
          label.innerHTML = 'メールアドレス';
        }
      }


const input = document.createElement('input');
      // 本名(HN)
input.type = 'text';
      if (label.getAttribute('for') === 'wpRealName' || (label.innerHTML || '').indexOf('HN') !== -1) {
input.placeholder = '投稿者名(省略可)';
        if ((label.innerHTML || '').indexOf('(省略可能)') !== -1) {
input.style.width = '200px';
          label.innerHTML = 'HN(省略可能)';
        }
      }
    }


wrapper.appendChild(input);
    // 2. 必須属性の付与
form.prepend(wrapper);
    var emailInput = document.getElementById('wpEmail');
}
    if (emailInput && !emailInput.required) {
      emailInput.required = true;
      emailInput.setAttribute('required', 'required');
    }
  }


/* ===== 送信時:本文に名前を埋め込む ===== */
  function boot() {
function hookSubmit() {
    enforce();
const form = document.querySelector('.comment-form');
if (!form) return;


const textarea = form.querySelector('textarea');
    // 監視対象を限定
const input = form.querySelector('.comment-name-input input');
    var target = document.getElementById('mw-content-text');
const button = form.querySelector('button[type="submit"]');
    if (!target || !window.MutationObserver) return;


if (!textarea || !input || !button) return;
    var observer = new MutationObserver(function (mutations) {
      for (var i = 0; i < mutations.length; i++) {
        if (mutations[i].addedNodes && mutations[i].addedNodes.length > 0) {
          enforce();
          break;
        }
      }
    });


button.addEventListener('click', function () {
    observer.observe(target, { childList: true, subtree: true });
let name = input.value.trim();
  }
if (!name) name = '名無しさん';


// すでに埋め込み済みなら何もしない
  if (document.readyState === 'loading') {
if (!textarea.value.startsWith('')) {
    document.addEventListener('DOMContentLoaded', boot);
textarea.value = `【${name}】` + textarea.value;
  } else {
}
    boot();
});
  }
}
})();


/* ===== 表示時:名前を取り出して表示 ===== */
/**
function applyDisplayNames() {
* Comments: 「並べ替え」「自動更新」UIを非表示(mw依存なし)
document.querySelectorAll('.comment').forEach(function (comment) {
*/
(function () {
  function hideAutoUpdate(root) {
    var links = root.querySelectorAll('a');
    for (var i = 0; i < links.length; i++) {
      var a = links[i];
      var t = (a.textContent || '').trim();
      if (t.indexOf('コメントの自動更新を有効化') !== -1) {
        a.style.display = 'none';
      }
    }
  }


if (comment.querySelector('.comment-display-name')) return;
  function apply() {
    var root = document.getElementById('comments-body');
    if (!root) return;
    hideAutoUpdate(root);
  }


const body = comment.querySelector('.comment-body, .comment-text');
  function boot() {
if (!body) return;
    apply();


const text = body.innerHTML;
    var watchRoot =
const match = text.match(/^【(.+?)】/);
      document.getElementById('comments-body') ||
      document.getElementById('mw-data-after-content') ||
      document.body;


let name = '名無しさん';
    if (watchRoot && window.MutationObserver) {
      var obs = new MutationObserver(function () { apply(); });
      obs.observe(watchRoot, { childList: true, subtree: true });
    }
  }


if (match) {
  if (window.RLQ && typeof window.RLQ.push === 'function') {
name = match[1];
    window.RLQ.push(boot);
body.innerHTML = body.innerHTML.replace(/^【.+?】/, '');
  } else if (document.readyState === 'loading') {
}
    document.addEventListener('DOMContentLoaded', boot);
  } else {
    boot();
  }
})();


const header = comment.querySelector('.comment-header') || comment;
/* anonの時刻補正(必ず落ちない版) */
const nameElem = document.createElement('div');
window.RLQ = window.RLQ || [];
nameElem.className = 'comment-display-name';
window.RLQ.push(function () {
nameElem.textContent = name;
  if (!window.mw || !mw.loader) return;


header.prepend(nameElem);
  mw.loader.using(['mediawiki.user', 'user.options']).done(function () {
});
    if (!mw.user || typeof mw.user.isAnon !== 'function' || !mw.user.options) return;
}


/* ===== 実行 ===== */
    // 匿名で timecorrection が未設定のときだけ補う
addNameField();
    if (mw.user.isAnon() && !mw.user.options.get('timecorrection')) {
hookSubmit();
      mw.user.options.set('timecorrection', 'ZoneInfo|Asia/Tokyo');
applyDisplayNames();
    }
 
  });
/* コメント追加後にも再適用 */
document.addEventListener('click', function () {
setTimeout(applyDisplayNames, 500);
});
});
});

2026年1月25日 (日) 19:24時点における最新版

/**
 * MediaWiki 1.44.3 アカウント作成画面 無限ループ防止版
 * 既に書き換え済みの場合は処理をスキップするように修正しました。
 */
(function () {
  function enforce() {
    if (!window.mw || !mw.config) return;
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'CreateAccount') return;

    // 1. ラベルの書き換え
    var labels = document.getElementsByTagName('label');
    for (var i = 0; i < labels.length; i++) {
      var label = labels[i];

      // メールアドレス
      if (label.getAttribute('for') === 'wpEmail' || (label.innerHTML || '').indexOf('メールアドレス') !== -1) {
        if ((label.innerHTML || '').indexOf('(省略可能)') !== -1) {
          label.innerHTML = 'メールアドレス';
        }
      }

      // 本名(HN)
      if (label.getAttribute('for') === 'wpRealName' || (label.innerHTML || '').indexOf('HN') !== -1) {
        if ((label.innerHTML || '').indexOf('(省略可能)') !== -1) {
          label.innerHTML = 'HN(省略可能)';
        }
      }
    }

    // 2. 必須属性の付与
    var emailInput = document.getElementById('wpEmail');
    if (emailInput && !emailInput.required) {
      emailInput.required = true;
      emailInput.setAttribute('required', 'required');
    }
  }

  function boot() {
    enforce();

    // 監視対象を限定
    var target = document.getElementById('mw-content-text');
    if (!target || !window.MutationObserver) return;

    var observer = new MutationObserver(function (mutations) {
      for (var i = 0; i < mutations.length; i++) {
        if (mutations[i].addedNodes && mutations[i].addedNodes.length > 0) {
          enforce();
          break;
        }
      }
    });

    observer.observe(target, { childList: true, subtree: true });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', boot);
  } else {
    boot();
  }
})();

/**
 * Comments: 「並べ替え」「自動更新」UIを非表示(mw依存なし)
 */
(function () {
  function hideAutoUpdate(root) {
    var links = root.querySelectorAll('a');
    for (var i = 0; i < links.length; i++) {
      var a = links[i];
      var t = (a.textContent || '').trim();
      if (t.indexOf('コメントの自動更新を有効化') !== -1) {
        a.style.display = 'none';
      }
    }
  }

  function apply() {
    var root = document.getElementById('comments-body');
    if (!root) return;
    hideAutoUpdate(root);
  }

  function boot() {
    apply();

    var watchRoot =
      document.getElementById('comments-body') ||
      document.getElementById('mw-data-after-content') ||
      document.body;

    if (watchRoot && window.MutationObserver) {
      var obs = new MutationObserver(function () { apply(); });
      obs.observe(watchRoot, { childList: true, subtree: true });
    }
  }

  if (window.RLQ && typeof window.RLQ.push === 'function') {
    window.RLQ.push(boot);
  } else if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', boot);
  } else {
    boot();
  }
})();

/* anonの時刻補正(必ず落ちない版) */
window.RLQ = window.RLQ || [];
window.RLQ.push(function () {
  if (!window.mw || !mw.loader) return;

  mw.loader.using(['mediawiki.user', 'user.options']).done(function () {
    if (!mw.user || typeof mw.user.isAnon !== 'function' || !mw.user.options) return;

    // 匿名で timecorrection が未設定のときだけ補う
    if (mw.user.isAnon() && !mw.user.options.get('timecorrection')) {
      mw.user.options.set('timecorrection', 'ZoneInfo|Asia/Tokyo');
    }
  });
});