// ==UserScript== // @name Muse 直前のチャットに戻るボタン // @namespace fuwari-tools // @version 1.1 // @description 左サイドバーのチャットアイコンの上に「直前のサイドチャットに戻る」ボタンを追加する。メインチャットに10秒いると記録を捨てる。 // @match https://muse.ai/* // @run-at document-idle // @noframes // @grant none // ==/UserScript== (function () { 'use strict'; if (window.top !== window) return; const KEY = 'museLastChatV1'; const BTN_ID = 'muse-back-to-chat-btn'; const LOG = '[戻るボタン]'; const STABLE_MS = 2500; // 同じURLをこの時間開いていたら記録する const MAIN_CLEAR_MS = 10000; // メインチャットをこの時間開いていたら記録を捨てる // ---------- 記録 ---------- function loadLast() { try { return JSON.parse(localStorage.getItem(KEY) || 'null'); } catch (e) { return null; } } function saveLast(data) { try { localStorage.setItem(KEY, JSON.stringify(data)); } catch (e) {} } function clearLast() { try { localStorage.removeItem(KEY); } catch (e) {} } function pathSegments(url) { try { return new URL(url).pathname.split('/').filter(Boolean).length; } catch (e) { return 0; } } // メインチャット = パスが空のURL(https://muse.ai/) function isMainChat(url) { return pathSegments(url) === 0; } function isChatView() { const els = document.querySelectorAll('textarea, [contenteditable="true"]'); for (const el of els) { if (el.offsetParent !== null) return true; } return false; } // 「同じURLを開き続けている時間」を数える let stableUrl = location.href; let stableSince = Date.now(); function checkSave() { const url = location.href; if (url !== stableUrl) { stableUrl = url; stableSince = Date.now(); refreshBtn(); return; } if (document.visibilityState !== 'visible') return; if (navigating) return; const elapsed = Date.now() - stableSince; // メインチャットを10秒開いていたら、記録を捨てる(メインチャット自体は記録しない) if (isMainChat(url)) { if (elapsed >= MAIN_CLEAR_MS && loadLast()) { clearLast(); console.log(LOG, 'メインチャットに10秒いたので記録を捨てました'); refreshBtn(); } return; } // サイドチャット:一定時間開いていたら記録する if (elapsed < STABLE_MS) return; if (!isChatView()) return; const prev = loadLast(); if (prev && prev.url === url) return; // すでに記録済み saveLast({ url: url, title: document.title, at: Date.now() }); console.log(LOG, '記録:', url); refreshBtn(); } // ---------- ページ遷移 ---------- let navigating = false; function snapshot() { const t = document.body.innerText || ''; return t.length + ':' + t.slice(0, 300) + t.slice(-300); } function findLink(full) { return Array.from(document.querySelectorAll('a[href]')).find(a => { if (a.id === BTN_ID) return false; try { const h = new URL(a.getAttribute('href'), location.href); return h.pathname + h.search + h.hash === full; } catch (e) { return false; } }); } function goTo(url) { if (navigating) return; let u; try { u = new URL(url, location.href); } catch (e) { location.assign(url); return; } if (u.origin !== location.origin) { location.assign(url); return; } const full = u.pathname + u.search + u.hash; const before = snapshot(); navigating = true; function finish(delay) { setTimeout(() => { navigating = false; if (location.href !== u.href || snapshot() === before) { console.log(LOG, '切り替わらなかったのでリロード遷移に切替'); location.assign(url); } else { console.log(LOG, 'リロードなしで切り替え成功'); } }, delay); } function tryPushState() { console.log(LOG, 'pushState で遷移を試行:', full); history.pushState(history.state, '', full); window.dispatchEvent(new PopStateEvent('popstate', { state: history.state })); finish(700); } // 1. 今の画面にリンクがあればクリック const link = findLink(full); if (link) { console.log(LOG, 'リンクをクリックして遷移:', full); link.click(); finish(700); return; } // 2. なければチャットアイコンを押して一覧を開き、リンクが出た瞬間にクリック const nav = findChatNav(); if (!nav) { tryPushState(); return; } console.log(LOG, 'チャット一覧を開いてリンクを探します'); nav.click(); let waited = 0; const timer = setInterval(() => { waited += 30; const l = findLink(full); if (l) { clearInterval(timer); console.log(LOG, '一覧のリンクをクリック:', full); l.click(); finish(700); } else if (waited >= 800) { clearInterval(timer); tryPushState(); } }, 30); } // ---------- ボタン ---------- function findChatNav() { return document.querySelector('a[data-testid="hatch-nav-chat"]') || document.querySelector('a[aria-label="チャット"]') || document.querySelector('a[aria-label="Chat"]'); } function ensureBtn() { const existing = document.getElementById(BTN_ID); if (existing && existing.isConnected) { return; } const nav = findChatNav(); if (!nav) return; const slot = nav.closest('[data-slot="tooltip-trigger"]') || nav; if (!slot.parentElement) return; const btn = document.createElement('button'); btn.id = BTN_ID; btn.type = 'button'; btn.textContent = '◀️'; btn.style.cssText = [ 'display:flex', 'align-items:center', 'justify-content:center', 'width:44px', 'height:28px', 'margin:0 auto 8px auto', 'padding:0', 'border-radius:8px', 'border:1px solid rgba(127,127,127,0.35)', 'background:rgba(127,127,127,0.12)', 'color:inherit', 'font-size:15px', 'cursor:pointer', 'white-space:nowrap', 'user-select:none', 'flex-shrink:0' ].join(';'); btn.addEventListener('click', () => { const last = loadLast(); if (last && last.url && last.url !== location.href) { console.log(LOG, '戻り先:', last.url); goTo(last.url); } }); slot.parentElement.insertBefore(btn, slot); console.log(LOG, 'ボタンをサイドバーに配置'); refreshBtn(); } function refreshBtn() { const btn = document.getElementById(BTN_ID); if (!btn) return; const last = loadLast(); const active = !!(last && last.url && last.url !== location.href); btn.style.opacity = active ? '1' : '0.45'; btn.style.cursor = active ? 'pointer' : 'default'; btn.title = last ? '戻り先: ' + last.url + '\n記録: ' + new Date(last.at).toLocaleString() : '戻り先がありません(サイドチャットを開くと記録されます)'; } // ---------- 監視 ---------- setInterval(checkSave, 500); setInterval(ensureBtn, 5000); let rafPending = false; new MutationObserver(() => { if (rafPending) return; rafPending = true; requestAnimationFrame(() => { rafPending = false; ensureBtn(); }); }).observe(document.documentElement, { childList: true, subtree: true }); ensureBtn(); })();