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

提供: 小樽のじかん事典
編集の要約なし
編集の要約なし
 
(同じ利用者による、間の13版が非表示)
1行目: 1行目:
/**
/**
* アカウント作成画面のラベル強制書き換えおよびバリデーション
  * MediaWiki 1.44.3 アカウント作成画面 無限ループ防止版
  * MediaWiki 1.44.3 対応版
* 既に書き換え済みの場合は処理をスキップするように修正しました。
  */
  */
(function() {
(function () {
    // アカウント作成画面(Special:CreateAccount)でのみ実行
  function enforce() {
    if (mw.config.get('wgCanonicalSpecialPageName') === 'CreateAccount') {
    if (!window.mw || !mw.config) return;
       
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'CreateAccount') return;
        var customizeAccountForm = function() {
            // 1. メールアドレスのラベル変更(省略可能を削除)
            // 複数のセレクタを用いて、確実に要素を特定します
            var $emailLabel = $('label[for="wpEmail"], #mw-createaccount-join-form label:contains("メールアドレス")');
            if ($emailLabel.length) {
                $emailLabel.text('メールアドレス');
            }
           
            // 2. 本名のラベルを「HN(省略可能)」に変更
            var $realNameLabel = $('label[for="wpRealName"], #mw-createaccount-join-form label:contains("本名")');
            if ($realNameLabel.length) {
                $realNameLabel.text('HN(省略可能)');
            }
           
            // 3. メールアドレス入力をブラウザレベルで必須にする
            var $emailInput = $('#wpEmail');
            if ($emailInput.length) {
                $emailInput.prop('required', true);
                // HTML5のバリデーションを有効化
                $emailInput.attr('aria-required', 'true');
            }
           
            // 4. アカウント作成ボタンが押された際の最終チェック
            var $submitButton = $('#wpCreateaccount');
            if ($submitButton.length) {
                $submitButton.on('click', function(e) {
                    if ($emailInput.length && !$emailInput.val()) {
                        alert('メールアドレスは必須項目です。');
                        $emailInput.focus();
                        e.preventDefault();
                        return false;
                    }
                });
            }
        };


        // DOMの構築を待って実行
    // 1. ラベルの書き換え
        $(document).ready(function() {
    var labels = document.getElementsByTagName('label');
            customizeAccountForm();
    for (var i = 0; i < labels.length; i++) {
            // 動的な変更を監視(MutationObserver)
      var label = labels[i];
            if (window.MutationObserver) {
 
                var observer = new MutationObserver(function() {
      // メールアドレス
                    customizeAccountForm();
      if (label.getAttribute('for') === 'wpEmail' || (label.innerHTML || '').indexOf('メールアドレス') !== -1) {
                });
        if ((label.innerHTML || '').indexOf('(省略可能)') !== -1) {
                observer.observe(document.body, { childList: true, subtree: true });
          label.innerHTML = 'メールアドレス';
            }
        }
        });
      }
       
 
        // フォールバック:0.5秒後と2秒後にも実行
      // 本名(HN)
        setTimeout(customizeAccountForm, 500);
      if (label.getAttribute('for') === 'wpRealName' || (label.innerHTML || '').indexOf('HN') !== -1) {
        setTimeout(customizeAccountForm, 2000);
        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');
    }
  });
});

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');
    }
  });
});