// ==UserScript== // @name Muse チャット背景色(チャットごと) // @namespace lumi.userscripts // @version 1.2 // @description Museのチャットごとに背景色を変える。カラーピッカーで現在のチャットに色を設定でき、対応はブラウザ内に保存される。 // @match *://muse.ai/* // @match *://*.muse.ai/* // @grant none // @run-at document-idle // ==/UserScript== // // 方式の履歴: // v1.0 body/main への CSS指定 → アプリの背景レイヤーに隠れて見えず。 // v1.1 画面全体オーバーレイ → 確実に見えるが、濃くすると文字や画像まで色を被る。 // v1.2 チャットフィード要素から祖先をたどり、「実際に背景を塗っている要素」だけを // 実行時に特定して、その要素の背景色を差し替える。文字・画像には色が被らない。 (function () { 'use strict'; const STORE_KEY = 'lumi-chat-bg-v1'; const MARK = 'data-lumi-bg'; const PANEL_ID = 'lumi-chat-bg-panel'; const BTN_ID = 'lumi-chat-bg-btn'; // ---------- 保存 ---------- function loadStore() { try { return JSON.parse(localStorage.getItem(STORE_KEY) || '{}'); } catch (e) { return {}; } } function saveStore(s) { try { localStorage.setItem(STORE_KEY, JSON.stringify(s)); } catch (e) {} } // ---------- 今のチャットの識別キー ---------- function getChatKey() { const url = location.pathname + location.hash; const m = url.match(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i); if (m) return 'id:' + m[0].toLowerCase(); if (location.pathname && location.pathname !== '/') return 'path:' + location.pathname; return 'title:' + (document.title || 'unknown'); } function getChatLabel() { const h = document.querySelector('header h1, header h2, [role="banner"] h1, [role="banner"] h2'); if (h && h.textContent.trim()) return h.textContent.trim(); return document.title || ''; } // ---------- 色の適用(背景を塗っている要素を特定して差し替え) ---------- function hexToRgb(hex) { const m = /^#?([0-9a-f]{6})$/i.exec(hex || ''); if (!m) return null; const n = parseInt(m[1], 16); return { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255 }; } function isTransparent(color) { return !color || color === 'transparent' || /rgba\(\s*0\s*,\s*0\s*,\s*0\s*,\s*0\s*\)/.test(color); } function clearTints() { document.querySelectorAll('[' + MARK + ']').forEach(el => { el.style.removeProperty('background-color'); el.removeAttribute(MARK); }); } function findFeed() { return document.querySelector('[class*="@container/chat-feed"]') || document.querySelector('[class*="--hatch-chat-thread-inline-padding"]') || document.querySelector('main') || document.body; } function applyColor() { const store = loadStore(); const entry = store[getChatKey()]; clearTints(); if (!entry || !entry.color) return; const rgb = hexToRgb(entry.color); if (!rgb) return; const a = typeof entry.alpha === 'number' ? entry.alpha : 0.35; const rgba = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${a})`; // フィードから祖先をたどり、不透明な背景を持つ一番深い要素=実際に塗っている層を狙う const chain = []; let n = findFeed(); while (n && n !== document.documentElement) { chain.push(n); n = n.parentElement; } let painter = null; for (const el of chain) { if (el === document.body) continue; const bg = getComputedStyle(el).backgroundColor; if (!isTransparent(bg)) { painter = el; break; } } const targets = painter ? [painter] : [document.body]; targets.forEach(el => { el.style.setProperty('background-color', rgba, 'important'); el.setAttribute(MARK, '1'); }); } // ---------- ピッカーUI ---------- const PRESETS = [ ['#f6e8f0', 'さくら'], ['#e8f0fe', 'あおぞら'], ['#e6f4ea', 'わかば'], ['#fff4e0', 'はちみつ'], ['#f3e8fd', 'ふじ'], ['#fdecea', 'もも'], ['#e0f2f1', 'ミント'], ['#eceff1', 'グレー'], ['#2b2b3a', 'よる(濃いめ注意)'] ]; function buildUI() { if (document.getElementById(BTN_ID)) return; const btn = document.createElement('button'); btn.id = BTN_ID; btn.textContent = '🎨'; btn.title = 'このチャットの背景色'; btn.style.cssText = [ 'position:fixed', 'right:14px', 'bottom:14px', 'z-index:2147483000', 'width:40px', 'height:40px', 'border-radius:50%', 'border:1px solid rgba(0,0,0,.25)', 'background:rgba(255,255,255,.9)', 'font-size:19px', 'cursor:pointer', 'box-shadow:0 2px 8px rgba(0,0,0,.25)', 'line-height:1', 'padding:0' ].join(';'); btn.addEventListener('click', togglePanel); document.body.appendChild(btn); } function togglePanel() { const old = document.getElementById(PANEL_ID); if (old) { old.remove(); return; } const store = loadStore(); const key = getChatKey(); const entry = store[key] || {}; const panel = document.createElement('div'); panel.id = PANEL_ID; panel.style.cssText = [ 'position:fixed', 'right:14px', 'bottom:62px', 'z-index:2147483000', 'width:250px', 'padding:12px', 'border-radius:10px', 'border:1px solid rgba(0,0,0,.25)', 'background:#fff', 'color:#222', 'box-shadow:0 4px 16px rgba(0,0,0,.3)', 'font-size:13px', 'font-family:sans-serif', 'text-align:left' ].join(';'); const label = getChatLabel(); panel.innerHTML = `