Open2chイエローテーマ

おんJのスレ内のカラーテーマを変えます

// ==UserScript==
// @name         Open2chイエローテーマ
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  おんJのスレ内のカラーテーマを変えます
// @match        https://open.open2ch.net/*
// @match        https://hayabusa.open2ch.net/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

/*コードはAIに書いてもらいました*/




(function(){
  'use strict';

  const style = document.createElement('style');
  style.textContent = `


/* 全体の背景や文字色 */
html, body, .MAIN_WRAP, .thread, ol {
  background-color: #feffef;
  color: #7f0000;
}


/* 名前、ID、日付などの色調整 */
.name {
  color: #1a7c49 !important;
}
span.dateTime {
  color: #888888 !important;
}
a.num {
  color: #7f0000 !important;
}
span.idValue, .id {
  color: #7f0000 !important;
}

/* スレタイ色*/
h1 font[color="#FF0000"] {
//  background-color: #f0e0d6 !important;
  color: #ff0000 !important;
}

/* レスヘッダ全体を少し小さく */
dt.hd {
  font-size: 0.85em !important;
}

/* 2レス目以降のレス番号と名前だけ少し大きく補正 */
dt.hd:not([res="1"]) a.num,
dt.hd:not([res="1"]) font.name {
  font-size: 1.2em !important;
}

/* 1レス目のレス番号はそのまま */
dt.hd[res="1"] a.num {
  font-size: 1.2em !important;
}

/* 1レス目の名前は縮小補正 */
dt.hd[res="1"] font.name {
  font-size: 1em !important;
}

/* 本文との隙間調整 */
dt.hd + dd.body {
  margin-top: 2px !important;
}

/* レス区切り線(ヘッダの上) */
dt.hd::before {
  content: "";
  display: block;
  height: 1px;
  background-color: #fff8a2;
  margin: 12px 0 4px;
}
   /* OPのID(ID:+4桁英数字)をオレンジに */
   dt.hd.op a.id,
   dt.hd.op .idValue {
     color: #cc8d05 !important;
   }
  `;
  document.head.appendChild(style);
      // スレ主(OP)のIDを取って、該当投稿に .op を付与

// OP 判定&クラス付け
function markOP(){
  // 1レス目のID値を取得
  const firstSpan = document.querySelector('dt.hd[res="1"] .idValue');
  if (!firstSpan) return;

  const opId = firstSpan.textContent.trim();

  // 全レスの .idValue を調べて、一致すれば .op を追加
  document.querySelectorAll('dt.hd .idValue').forEach(span => {
    if (span.textContent.trim() === opId) {
      const dt = span.closest('dt.hd');
      if (dt) dt.classList.add('op');
    }
  });
}




  document.head.appendChild(style);

 function processNanashi() {
  document.querySelectorAll('.name').forEach(el => {
    if (el.dataset.nanashiDone) return;

    // 名前を書き換え
    if (el.textContent.trim() === '名無し') {
      el.textContent = '名無し@おーぷん';
    }

    // 「:」の削除処理 ←なんか動作しない
    let node = el;
    // name が <font><b>名無し</b></font> の場合、b→font→親(例えば dt)の順に登る
    while (node && node.nodeType === 1 && node.className !== 'hd') {
      if (node.nextSibling) break;
      node = node.parentNode;
    }

    if (!node || !node.nextSibling) return;

    let next = node.nextSibling;
    // テキストノードを探す(IDなどのspanの手前まで)
    while (next && !(next.nodeType === Node.TEXT_NODE || next.classList?.contains('_id'))) {
      next = next.nextSibling;
    }

    // テキストノードなら置換
    if (next && next.nodeType === Node.TEXT_NODE) {
      next.nodeValue = next.nodeValue.replace(/^[::]\s*/, '\u00A0\u00A0');
    }

    el.dataset.nanashiDone = '1';
  });
}


  function wrapDateTimeByDt(){
    document.querySelectorAll('dt.hd').forEach(dt=>{
      if(dt.querySelector('span.dateTime')) return;
      const numEl = dt.querySelector('a.num'),
            idEl  = dt.querySelector('._id');
      if(!numEl || !idEl) return;

      const toWrap = [];
      let node = numEl.nextSibling;
      while(node && node !== idEl){
        toWrap.push(node);
        node = node.nextSibling;
      }
      if(!toWrap.length) return;

      const span = document.createElement('span');
      span.className = 'dateTime';
      dt.insertBefore(span, idEl);
      toWrap.forEach(n => span.appendChild(n));
    });
  }

  function wrapIDValue(){
    document.querySelectorAll('span._id a.id').forEach(el=>{
      if(el.dataset.idWrapped) return;
      const m = el.textContent.match(/^ID:(.+)$/);
      if(!m) return;
      el.textContent = 'ID:';
      const span = document.createElement('span');
      span.className = 'idValue';
      span.textContent = m[1];
      el.appendChild(span);
      el.dataset.idWrapped = '1';
    });
  }


    function addSpaceAfterDateTime() {
  document.querySelectorAll('span.dateTime').forEach(el => {
    if (el.dataset.spaced) return;

    // 末尾に半角スペース追加(表示上の空白)
    el.appendChild(document.createTextNode(' '));

    el.dataset.spaced = '1';
  });
}






function doAll(){
  processNanashi();
  wrapDateTimeByDt();
  wrapIDValue();
  addSpaceAfterDateTime();
  markOP();           // ← 一番最後に OP マーク
}

doAll();
window.addEventListener('load', ()=>setTimeout(doAll, 200));

const obs = new MutationObserver(()=>{
  obs.disconnect();
  doAll();
  obs.observe(document.body, { childList:true, subtree:true });
});
obs.observe(document.body, { childList:true, subtree:true });

})();