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

提供: 小樽のじかん事典
編集の要約なし
編集の要約なし
3行目: 3行目:
  * 既に書き換え済みの場合は処理をスキップするように修正しました。
  * 既に書き換え済みの場合は処理をスキップするように修正しました。
  */
  */
(function() {
(function () {
    var enforce = function() {
  function enforce() {
        if (mw.config.get('wgCanonicalSpecialPageName') !== 'CreateAccount') return;
    if (!window.mw || !mw.config) return;
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'CreateAccount') return;


        // 1. メールアドレスの書き換え
    // 1. ラベルの書き換え
        var labels = document.getElementsByTagName('label');
    var labels = document.getElementsByTagName('label');
        for (var i = 0; i < labels.length; i++) {
    for (var i = 0; i < labels.length; i++) {
            var label = labels[i];
      var label = labels[i];
            // 「(省略可能)」が含まれている場合のみ書き換える(無限ループ防止)
            if (label.getAttribute('for') === 'wpEmail' || label.innerHTML.indexOf('メールアドレス') !== -1) {
                if (label.innerHTML.indexOf('(省略可能)') !== -1) {
                    label.innerHTML = 'メールアドレス';
                }
            }
            // 本名の書き換え
            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 (label.getAttribute('for') === 'wpEmail' || (label.innerHTML || '').indexOf('メールアドレス') !== -1) {
         if (emailInput && !emailInput.required) {
         if ((label.innerHTML || '').indexOf('(省略可能)') !== -1) {
            emailInput.required = true;
          label.innerHTML = 'メールアドレス';
            emailInput.setAttribute('required', 'required');
         }
         }
    };
      }
 
    if (mw.config.get('wgCanonicalSpecialPageName') === 'CreateAccount') {
        // 初回実行
        enforce();


        // 監視対象を限定し、無限ループを回避
      // 本名(HN)
        var observer = new MutationObserver(function(mutations) {
      if (label.getAttribute('for') === 'wpRealName' || (label.innerHTML || '').indexOf('HN') !== -1) {
            for (var i = 0; i < mutations.length; i++) {
        if ((label.innerHTML || '').indexOf('(省略可能)') !== -1) {
                if (mutations[i].addedNodes.length > 0) {
          label.innerHTML = 'HN(省略可能)';
                    enforce();
                    break;
                }
            }
        });
 
        var target = document.getElementById('mw-content-text');
        if (target) {
            observer.observe(target, {childList: true, subtree: true});
         }
         }
      }
     }
     }
})();


(function () {
     // 2. 必須属性の付与
  function isVisible(el) {
     var emailInput = document.getElementById('wpEmail');
     if (!el) return false;
     if (emailInput && !emailInput.required) {
     var cs = window.getComputedStyle ? getComputedStyle(el) : null;
      emailInput.required = true;
     return !cs || (cs.display !== 'none' && cs.visibility !== 'hidden' && cs.opacity !== '0');
      emailInput.setAttribute('required', 'required');
  }
 
  function hasVisibleContent(el) {
    if (!el) return false;
 
    // visibleな要素(入力/ボタン/リンク/セレクト等)が残っていれば空ではない
    var keep = el.querySelectorAll('input, textarea, button, select, a, .oo-ui-widget, .mw-ui-button');
    for (var i = 0; i < keep.length; i++) {
      if (isVisible(keep[i])) return true;
     }
     }
    // 表示テキストが残っていれば空ではない
    var t = (el.textContent || '').replace(/\s+/g, '');
    return t.length > 0;
   }
   }


   function collapseUp(el, maxDepth) {
   function boot() {
     var cur = el;
     enforce();
    for (var d = 0; d < (maxDepth || 6); d++) {
      if (!cur || !cur.parentElement) break;
 
      // 自分自身が見えていて、中身が実質空なら潰す
      if (isVisible(cur) && !hasVisibleContent(cur)) {
        cur.style.display = 'none';
      }
 
      cur = cur.parentElement;
    }
  }
 
  function apply() {
    var root = document.getElementById('comments-body');
    if (!root) return;


     // 1) 並べ替えUI:option文言から親selectを特定して消す
     // 監視対象を限定
     var orderTexts = ['日付順に並べ替え', '得点順に並べ替え'];
     var target = document.getElementById('mw-content-text');
    if (!target || !window.MutationObserver) return;


     var options = root.querySelectorAll('option');
     var observer = new MutationObserver(function (mutations) {
    for (var i = 0; i < options.length; i++) {
      for (var i = 0; i < mutations.length; i++) {
      var opt = options[i];
        if (mutations[i].addedNodes && mutations[i].addedNodes.length > 0) {
      var t = (opt.textContent || '').trim();
          enforce();
      if (!t) continue;
 
      var hit = false;
      for (var j = 0; j < orderTexts.length; j++) {
        if (t.indexOf(orderTexts[j]) !== -1) {
          hit = true;
           break;
           break;
         }
         }
       }
       }
      if (!hit) continue;
    });


      var sel = opt.closest ? opt.closest('select') : null;
    observer.observe(target, { childList: true, subtree: true });
      if (!sel) continue;
  }


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


      // 周辺の親を優先的に潰す(ツールバー想定)
/**
      var w = sel.closest
* Comments: 「並べ替え」「自動更新」UIを非表示(mw依存なし)
        ? sel.closest('.c-nav, .c-toolbar, .comments-toolbar, .comments-header, .c-order, .comments-order, .comments-sort, li, div, span')
*/
        : null;
(function () {
 
  function hideAutoUpdate(root) {
      if (w) {
        collapseUp(w, 6);
      } else {
        collapseUp(sel, 6);
      }
    }
 
    // 2) 自動更新リンク:文言一致のaを消す
     var links = root.querySelectorAll('a');
     var links = root.querySelectorAll('a');
     for (var k = 0; k < links.length; k++) {
     for (var i = 0; i < links.length; i++) {
       var a = links[k];
       var a = links[i];
       var at = (a.textContent || '').trim();
       var t = (a.textContent || '').trim();
       if (at.indexOf('コメントの自動更新を有効化') === -1) continue;
       if (t.indexOf('コメントの自動更新を有効化') !== -1) {
 
        a.style.display = 'none';
      a.style.display = 'none';
 
      var aw = a.closest
        ? a.closest('.c-nav, .c-toolbar, .comments-toolbar, .comments-header, li, div, span')
        : null;
 
      if (aw) {
        collapseUp(aw, 6);
      } else {
        collapseUp(a, 6);
       }
       }
     }
     }
  }


    // 3) 最後の掃除:コメント領域直下の空コンテナを潰す
  function apply() {
    // ここで「== コメント == の下の空白」になっている箱を落とす
     var root = document.getElementById('comments-body');
     var candidates = root.querySelectorAll('div, li, ul, ol');
     if (!root) return;
     for (var c = 0; c < candidates.length; c++) {
    hideAutoUpdate(root);
      var box = candidates[c];
      if (!isVisible(box)) continue;
      if (hasVisibleContent(box)) continue;
 
      // 高さ/余白を持つ空箱だけ潰す
      var cs = getComputedStyle(box);
      var hasSpace =
        (parseFloat(cs.marginTop) || 0) +
          (parseFloat(cs.marginBottom) || 0) +
          (parseFloat(cs.paddingTop) || 0) +
          (parseFloat(cs.paddingBottom) || 0) >
        0;
 
      if (hasSpace) {
        box.style.display = 'none';
      }
    }
   }
   }


174行目: 86行目:
     apply();
     apply();


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

2026年1月8日 (木) 09:21時点における版

/**
 * 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();
  }
})();