// ==UserScript== // @name YouTube Chapter Marker // @namespace https://the-coven.fr // @version 2.7.0 // @description Add, edit, import chapter markers on YouTube, with visual segments on the progress bar // @author VooDoo / The Coven // @match https://www.youtube.com/watch* // @icon https://www.youtube.com/favicon.ico // @grant GM_addStyle // ==/UserScript== (function () { 'use strict'; let chapters = []; let injectInterval = null; let panel = null; // --- Styles --- GM_addStyle(` /* Button tooltip */ #yt-chapter-btn { position: relative; } #yt-chapter-btn .yt-cm-tooltip { position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%); background: rgba(28,28,28,.95); color: #fff; font-size: 12px; font-family: 'YouTube Sans', Roboto, sans-serif; white-space: nowrap; padding: 5px 10px; border-radius: 4px; pointer-events: none; opacity: 0; transition: opacity .15s .4s; z-index: 100000; box-shadow: 0 2px 8px rgba(0,0,0,.4); } .ytp-right-controls, .ytp-right-controls-right { overflow: visible !important; } #yt-chapter-btn:hover .yt-cm-tooltip { opacity: 1; } /* Main panel */ #yt-cm-panel { position: fixed; top: 0; right: 0; bottom: 0; width: 400px; background: #0f0f0f; color: #f1f1f1; font-family: 'YouTube Sans', Roboto, Arial, sans-serif; display: flex; flex-direction: column; z-index: 99999; box-shadow: -2px 0 32px rgba(0,0,0,.7); transform: translateX(100%); transition: transform .22s cubic-bezier(.4,0,.2,1); box-sizing: border-box; } #yt-cm-panel * { box-sizing: border-box; } #yt-cm-panel.open { transform: translateX(0); } /* Header */ #yt-cm-panel .cm-header { display: flex; align-items: center; padding: 0 12px 0 20px; height: 56px; border-bottom: 1px solid #272727; flex-shrink: 0; gap: 8px; } #yt-cm-panel .cm-header-title { flex: 1; font-size: 16px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #yt-cm-panel .cm-icon-btn { background: none; border: none; cursor: pointer; padding: 8px; border-radius: 50%; color: #aaa; display: inline-flex; align-items: center; justify-content: center; transition: background .15s, color .15s; width: 36px; height: 36px; flex-shrink: 0; } #yt-cm-panel .cm-icon-btn:hover { background: rgba(255,255,255,.1); color: #fff; } #yt-cm-panel .cm-icon-btn svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; } /* Current time section */ #yt-cm-panel .cm-current { padding: 16px 20px; background: #161616; border-bottom: 1px solid #272727; flex-shrink: 0; display: flex; flex-direction: column; gap: 12px; } #yt-cm-panel .cm-current-info { width: 100%; } #yt-cm-panel .cm-current-label { font-size: 10px; font-weight: 600; color: #717171; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 4px; } #yt-cm-panel .cm-current-time { font-size: 28px; font-weight: 700; color: #ff0000; font-variant-numeric: tabular-nums; letter-spacing: -1px; line-height: 1.2; } #yt-cm-panel .cm-current-chapter { font-size: 12px; color: #aaa; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 6px; } .cm-skip-buttons { display: flex; justify-content: center; align-items: center; gap: 8px; flex-wrap: wrap; width: 100%; } .cm-skip-btn { background: rgba(255,255,255,.08); border: none; border-radius: 20px; padding: 6px 12px; font-size: 13px; font-weight: 600; cursor: pointer; color: #f1f1f1; transition: background .15s; } .cm-skip-btn:hover { background: rgba(255,255,255,.16); } .cm-playpause { background: rgba(255,255,255,.12); border: none; border-radius: 50%; width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; color: #f1f1f1; transition: background .15s; } .cm-playpause:hover { background: rgba(255,255,255,.2); } .cm-playpause svg { width: 20px; height: 20px; } /* Input row with embedded + button */ #yt-cm-panel .cm-input-row { padding: 14px 20px; border-bottom: 1px solid #272727; flex-shrink: 0; } #yt-cm-panel .cm-section-label { font-size: 10px; font-weight: 600; color: #717171; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 10px; } #yt-cm-panel .cm-input-wrapper { position: relative; display: flex; align-items: center; } #yt-cm-panel .cm-input-wrapper input { width: 100%; background: #272727; border: none; border-radius: 20px; color: #f1f1f1; font-size: 14px; font-family: inherit; padding: 10px 40px 10px 16px; outline: none; caret-color: #ff0000; transition: background .15s, box-shadow .15s; } #yt-cm-panel .cm-input-wrapper input:focus { background: #1f1f1f; box-shadow: 0 0 0 1.5px #ff0000; } #yt-cm-panel .cm-input-wrapper input::placeholder { color: #555; } #yt-cm-panel .cm-input-add { position: absolute; right: 6px; background: #ff0000; color: #fff; border: none; border-radius: 50%; width: 30px; height: 30px; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: background .15s; } #yt-cm-panel .cm-input-add:hover { background: #cc0000; } #yt-cm-panel .cm-quick-btn { background: #3f3f3f; border: none; border-radius: 20px; padding: 8px 16px; font-size: 12px; font-weight: 600; cursor: pointer; color: #f1f1f1; transition: background .15s; margin-top: 8px; width: 100%; display: flex; align-items: center; justify-content: center; gap: 6px; } #yt-cm-panel .cm-quick-btn:hover { background: #555; } /* Chapters list */ #yt-cm-panel .cm-list-section { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-height: 0; } #yt-cm-panel .cm-list-header-row { display: flex; align-items: center; padding: 14px 20px 6px; flex-shrink: 0; } #yt-cm-panel .cm-list-count { font-size: 10px; font-weight: 600; color: #717171; text-transform: uppercase; letter-spacing: 1px; flex: 1; } #yt-cm-panel .cm-clear-btn { background: none; border: none; cursor: pointer; color: #717171; font-size: 12px; font-family: inherit; padding: 4px 8px; border-radius: 12px; transition: background .15s, color .15s; } #yt-cm-panel .cm-clear-btn:hover { background: rgba(255,255,255,.08); color: #f1f1f1; } #yt-cm-panel .cm-clear-btn:disabled { opacity: .3; cursor: default; } #yt-cm-panel .cm-list { flex: 1; overflow-y: auto; padding: 4px 0 8px; scrollbar-width: thin; scrollbar-color: #333 transparent; } #yt-cm-panel .cm-list::-webkit-scrollbar { width: 4px; } #yt-cm-panel .cm-list::-webkit-scrollbar-thumb { background: #333; border-radius: 2px; } #yt-cm-panel .cm-item { display: flex; align-items: center; gap: 12px; padding: 9px 20px; border-radius: 12px; margin: 1px 8px; transition: background .1s; cursor: default; } #yt-cm-panel .cm-item:hover { background: #1a1a1a; } #yt-cm-panel .cm-item-tc { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; color: #ff0000; min-width: 50px; flex-shrink: 0; } #yt-cm-panel .cm-item-title { flex: 1; font-size: 14px; color: #f1f1f1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #yt-cm-panel .cm-item-edit, #yt-cm-panel .cm-item-del { background: none; border: none; cursor: pointer; padding: 5px; border-radius: 50%; color: #555; display: inline-flex; align-items: center; opacity: 0; transition: background .1s, color .1s, opacity .1s; flex-shrink: 0; } #yt-cm-panel .cm-item:hover .cm-item-edit, #yt-cm-panel .cm-item:hover .cm-item-del { opacity: 1; } #yt-cm-panel .cm-item-edit:hover, #yt-cm-panel .cm-item-del:hover { background: rgba(255,255,255,.1); color: #f1f1f1; } #yt-cm-panel .cm-item-edit svg, #yt-cm-panel .cm-item-del svg { width: 16px; height: 16px; } /* Empty state */ #yt-cm-panel .cm-empty { display: flex; flex-direction: column; align-items: center; justify-content: center; color: #404040; font-size: 13px; gap: 10px; padding: 40px 20px; } /* Footer */ #yt-cm-panel .cm-footer { padding: 12px 20px; border-top: 1px solid #272727; flex-shrink: 0; display: flex; flex-direction: column; gap: 10px; } #yt-cm-panel .cm-copy-btn { width: 100%; background: #ff0000; color: #fff; border: none; border-radius: 20px; padding: 12px 0; font-size: 14px; font-weight: 700; font-family: inherit; cursor: pointer; transition: background .15s; } #yt-cm-panel .cm-copy-btn:hover { background: #cc0000; } #yt-cm-panel .cm-copy-btn:disabled { background: #272727; color: #555; cursor: default; } #yt-cm-panel .cm-footer-credit { display: flex; justify-content: space-between; align-items: center; font-size: 11px; color: #717171; margin-top: 4px; flex-wrap: wrap; gap: 8px; } #yt-cm-panel .cm-footer-credit a { color: #ff0000; text-decoration: none; display: inline-flex; align-items: center; gap: 4px; } #yt-cm-panel .cm-footer-credit a:hover { text-decoration: underline; } #yt-cm-panel .cm-footer-credit svg { width: 12px; height: 12px; vertical-align: middle; } /* Modal - clean professional design */ .cm-modal-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,.8); z-index: 1000000; display: flex; align-items: center; justify-content: center; } .cm-modal { background: #1e1e1e; border-radius: 16px; max-width: 500px; width: 90%; padding: 0; color: #e0e0e0; font-family: 'YouTube Sans', Roboto, Arial, sans-serif; box-shadow: 0 20px 35px rgba(0,0,0,.5); overflow: hidden; } .cm-modal-header { padding: 20px 24px 12px; border-bottom: 1px solid #333; } .cm-modal-header h3 { margin: 0; font-size: 20px; font-weight: 500; color: #ff0000; } .cm-modal-body { padding: 20px 24px; max-height: 60vh; overflow-y: auto; } .cm-modal-section { margin-bottom: 24px; } .cm-modal-section h4 { margin: 0 0 10px 0; font-size: 16px; font-weight: 500; color: #fff; border-left: 3px solid #ff0000; padding-left: 12px; } .cm-modal-section p { margin: 8px 0; line-height: 1.5; font-size: 14px; } .cm-modal-section ul { margin: 8px 0; padding-left: 24px; } .cm-modal-section li { margin: 6px 0; line-height: 1.4; } .cm-modal-footer { padding: 16px 24px; border-top: 1px solid #333; text-align: right; } .cm-modal-close { background: #ff0000; border: none; border-radius: 20px; padding: 8px 20px; color: white; font-weight: 600; cursor: pointer; font-size: 13px; transition: background .15s; } .cm-modal-close:hover { background: #cc0000; } /* Progress bar segments (always visible) */ .ytp-progress-bar-container { position: relative; } .cm-chapter-segment { position: absolute; top: 0; height: 100%; background-color: rgba(255, 0, 0, 0.35); pointer-events: none; z-index: 5; } .cm-chapter-marker { position: absolute; top: 0; width: 2px; height: 100%; background-color: #ff0000; pointer-events: none; z-index: 6; } .cm-progress-tooltip { position: absolute; background: rgba(0,0,0,0.8); color: #fff; font-size: 12px; padding: 4px 8px; border-radius: 4px; pointer-events: none; z-index: 1000001; white-space: nowrap; font-family: 'YouTube Sans', Roboto, sans-serif; transform: translateX(-50%); bottom: 20px; } /* Edit icons */ .cm-edit-check, .cm-edit-cancel { background: none; border: none; cursor: pointer; padding: 4px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; color: #aaa; transition: all .15s; } .cm-edit-check:hover { background: #00aa00; color: white; } .cm-edit-cancel:hover { background: #aa0000; color: white; } .cm-edit-check svg, .cm-edit-cancel svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; } `); // --- SVG Lucide helpers --- function createLucideIcon(pathD, size = 24, strokeWidth = 2) { const ns = 'http://www.w3.org/2000/svg'; const svg = document.createElementNS(ns, 'svg'); svg.setAttribute('width', size); svg.setAttribute('height', size); svg.setAttribute('viewBox', '0 0 24 24'); svg.setAttribute('fill', 'none'); svg.setAttribute('stroke', 'currentColor'); svg.setAttribute('stroke-width', strokeWidth); svg.setAttribute('stroke-linecap', 'round'); svg.setAttribute('stroke-linejoin', 'round'); const path = document.createElementNS(ns, 'path'); path.setAttribute('d', pathD); svg.appendChild(path); return svg; } const ICON_INFO = createLucideIcon('M12 16v-4M12 8h.01M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10z', 20); const ICON_IMPORT = createLucideIcon('M12 3v12m-4-4 4 4 4-4M8 5H4a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2h-4', 20); const ICON_CLOSE = createLucideIcon('M18 6 6 18M6 6l12 12', 20); const ICON_PLAY = createLucideIcon('M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z', 20); const ICON_PAUSE = (() => { const ns = 'http://www.w3.org/2000/svg'; const svg = document.createElementNS(ns, 'svg'); svg.setAttribute('width', 20); svg.setAttribute('height', 20); svg.setAttribute('viewBox', '0 0 24 24'); svg.setAttribute('fill', 'none'); svg.setAttribute('stroke', 'currentColor'); svg.setAttribute('stroke-width', '2'); svg.setAttribute('stroke-linecap', 'round'); svg.setAttribute('stroke-linejoin', 'round'); const rect1 = document.createElementNS(ns, 'rect'); rect1.setAttribute('x', '14'); rect1.setAttribute('y', '3'); rect1.setAttribute('width', '5'); rect1.setAttribute('height', '18'); rect1.setAttribute('rx', '1'); const rect2 = document.createElementNS(ns, 'rect'); rect2.setAttribute('x', '5'); rect2.setAttribute('y', '3'); rect2.setAttribute('width', '5'); rect2.setAttribute('height', '18'); rect2.setAttribute('rx', '1'); svg.appendChild(rect1); svg.appendChild(rect2); return svg; })(); const ICON_PLUS = createLucideIcon('M5 12h14M12 5v14', 20); const ICON_PENCIL = createLucideIcon('M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497zM15 5l4 4', 18); const ICON_TRASH = createLucideIcon('M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6M3 6h18M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2', 18); const ICON_COPYRIGHT = createLucideIcon('M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10zM14.83 14.83a4 4 0 1 1 0-5.66', 14); const ICON_PIN = createLucideIcon('M12 17v5M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z', 14); const ICON_CHECK = createLucideIcon('M20 6 9 17l-5-5', 18); const ICON_X = createLucideIcon('M18 6 6 18M6 6l12 12', 18); // --- Helpers --- function getVideo() { return document.querySelector('video'); } function formatTime(secs) { secs = Math.floor(secs); const h = Math.floor(secs / 3600); const m = Math.floor((secs % 3600) / 60); const s = secs % 60; const mm = String(m).padStart(2, '0'); const ss = String(s).padStart(2, '0'); return h > 0 ? `${h}:${mm}:${ss}` : `${mm}:${ss}`; } function parseTimecode(str) { const parts = str.split(':').map(Number); if (parts.length === 2) return parts[0]*60 + parts[1]; if (parts.length === 3) return parts[0]*3600 + parts[1]*60 + parts[2]; const asFloat = parseFloat(str); return isNaN(asFloat) ? 0 : asFloat; } function buildExportText() { return [...chapters] .sort((a, b) => a.time - b.time) .map(c => `${formatTime(c.time)} ${c.title}`) .join('\n'); } function el(tag, attrs = {}, ...children) { const node = document.createElement(tag); for (const [k, v] of Object.entries(attrs)) { if (k === 'cls') node.className = v; else if (k === 'txt') node.textContent = v; else node.setAttribute(k, v); } for (const child of children) { if (child == null) continue; node.appendChild(typeof child === 'string' ? document.createTextNode(child) : child); } return node; } // --- Progress bar segments --- let segmentContainer = null; let progressTooltip = null; function updateProgressSegments() { if (segmentContainer) segmentContainer.remove(); const progressBar = document.querySelector('.ytp-progress-bar'); if (!progressBar) return; const video = getVideo(); const duration = video?.duration; if (!duration || chapters.length === 0) return; segmentContainer = document.createElement('div'); segmentContainer.style.position = 'relative'; segmentContainer.style.width = '100%'; segmentContainer.style.height = '100%'; progressBar.style.position = 'relative'; progressBar.appendChild(segmentContainer); const sorted = [...chapters].sort((a,b) => a.time - b.time); for (let i = 0; i < sorted.length; i++) { const start = sorted[i].time; const end = (i+1 < sorted.length) ? sorted[i+1].time : duration; const startPercent = (start / duration) * 100; const widthPercent = ((end - start) / duration) * 100; if (widthPercent > 0) { const segment = document.createElement('div'); segment.className = 'cm-chapter-segment'; segment.style.left = `${startPercent}%`; segment.style.width = `${widthPercent}%`; segmentContainer.appendChild(segment); } const marker = document.createElement('div'); marker.className = 'cm-chapter-marker'; marker.style.left = `${startPercent}%`; marker.title = sorted[i].title; segmentContainer.appendChild(marker); } setupProgressHoverTooltip(progressBar, duration, sorted); } function setupProgressHoverTooltip(progressBar, duration, sortedChapters) { if (progressTooltip) progressTooltip.remove(); progressTooltip = document.createElement('div'); progressTooltip.className = 'cm-progress-tooltip'; progressTooltip.style.display = 'none'; document.body.appendChild(progressTooltip); const onMouseMove = (e) => { const rect = progressBar.getBoundingClientRect(); const x = e.clientX - rect.left; const percent = Math.min(Math.max(x / rect.width, 0), 1); const time = percent * duration; let chapterTitle = ''; for (let i = sortedChapters.length-1; i >= 0; i--) { if (sortedChapters[i].time <= time) { chapterTitle = sortedChapters[i].title; break; } } if (chapterTitle) { progressTooltip.textContent = chapterTitle; progressTooltip.style.display = 'block'; progressTooltip.style.left = `${e.clientX}px`; progressTooltip.style.top = `${rect.top - 8}px`; } else { progressTooltip.style.display = 'none'; } }; const onMouseLeave = () => { progressTooltip.style.display = 'none'; }; progressBar.addEventListener('mousemove', onMouseMove); progressBar.addEventListener('mouseleave', onMouseLeave); if (window._cmProgressCleanup) window._cmProgressCleanup(); window._cmProgressCleanup = () => { progressBar.removeEventListener('mousemove', onMouseMove); progressBar.removeEventListener('mouseleave', onMouseLeave); if (progressTooltip) progressTooltip.remove(); }; } // --- Chapter management --- function getCurrentChapterTitle(time) { if (!chapters.length) return ''; const sorted = [...chapters].sort((a,b) => a.time - b.time); let current = null; for (let i = sorted.length-1; i >= 0; i--) { if (sorted[i].time <= time) { current = sorted[i]; break; } } return current ? current.title : ''; } function addQuickChapter() { const video = getVideo(); if (!video) return; const count = chapters.length + 1; const title = `Chapter ${count}`; chapters.push({ time: video.currentTime, title }); refreshList(); } function importYouTubeChapters(event) { const targetBtn = event?.target.closest?.('.cm-icon-btn') || document.querySelector('.cm-icon-btn[aria-label="Import YouTube chapters"]'); const originalTitle = targetBtn?.getAttribute('aria-label') || 'Import YouTube chapters'; function setButtonMessage(msg, duration = 2000) { if (targetBtn) { targetBtn.setAttribute('aria-label', msg); setTimeout(() => { if (targetBtn) targetBtn.setAttribute('aria-label', originalTitle); }, duration); } } let descriptionElem = document.querySelector('#description-inline-expander, #description, ytd-video-secondary-info-renderer #description'); let text = descriptionElem ? (descriptionElem.innerText || descriptionElem.textContent || '') : ''; if (!text || text.trim() === '') { try { const playerResponse = window.ytInitialPlayerResponse; if (playerResponse?.videoDetails?.shortDescription) text = playerResponse.videoDetails.shortDescription; else if (playerResponse?.microformat?.playerMicroformatRenderer?.description) text = playerResponse.microformat.playerMicroformatRenderer.description; } catch(e) {} } if (!text || text.trim() === '') { setButtonMessage('Loading...'); setTimeout(() => { const descElem = document.querySelector('#description-inline-expander, #description, ytd-video-secondary-info-renderer #description'); if (descElem) { const newText = descElem.innerText || descElem.textContent || ''; if (newText.trim()) parseChaptersFromText(newText, setButtonMessage); else setButtonMessage('Not found'); } else setButtonMessage('Not found'); }, 1000); return; } parseChaptersFromText(text, setButtonMessage); } function parseChaptersFromText(text, setButtonMessage) { if (!text || typeof text !== 'string') { if (setButtonMessage) setButtonMessage('Invalid text'); return; } const lines = text.split(/\r?\n/); const timecodeRegex = /^\s*(\d+):(\d+)(?::(\d+))?\s+(.+)$/; const newChapters = []; for (const line of lines) { const match = line.match(timecodeRegex); if (match) { let hours = 0, minutes, seconds; if (match[3] !== undefined) { hours = parseInt(match[1], 10); minutes = parseInt(match[2], 10); seconds = parseInt(match[3], 10); } else { minutes = parseInt(match[1], 10); seconds = parseInt(match[2], 10); } const title = match[4].trim(); if (!title) continue; const timeInSecs = hours * 3600 + minutes * 60 + seconds; newChapters.push({ time: timeInSecs, title }); } } if (newChapters.length > 0) { const existingKeys = new Set(chapters.map(c => `${c.time}|${c.title}`)); let addedCount = 0; for (const ch of newChapters) { const key = `${ch.time}|${ch.title}`; if (!existingKeys.has(key)) { chapters.push(ch); addedCount++; } } if (addedCount > 0) { refreshList(); if (setButtonMessage) setButtonMessage(`+${addedCount} imported`); } else if (setButtonMessage) setButtonMessage('No new'); } else if (setButtonMessage) setButtonMessage('No chapters'); } function editChapter(chapter, itemElement) { const tcSpan = itemElement.querySelector('.cm-item-tc'); const titleSpan = itemElement.querySelector('.cm-item-title'); const oldTime = chapter.time; const oldTitle = chapter.title; const timeInput = el('input', { type: 'text', value: formatTime(oldTime), style: 'width:70px; background:#333; color:#fff; border:none; border-radius:4px; padding:4px;' }); const titleInput = el('input', { type: 'text', value: oldTitle, style: 'flex:1; background:#333; color:#fff; border:none; border-radius:4px; padding:4px; margin:0 8px;' }); const checkBtn = el('button', { cls: 'cm-edit-check', 'aria-label': 'Save' }, ICON_CHECK.cloneNode(true)); const cancelBtn = el('button', { cls: 'cm-edit-cancel', 'aria-label': 'Cancel' }, ICON_X.cloneNode(true)); const editContainer = el('div', { style: 'display:flex; align-items:center; width:100%; gap:6px;' }, timeInput, titleInput, checkBtn, cancelBtn); tcSpan.style.display = 'none'; titleSpan.style.display = 'none'; itemElement.insertBefore(editContainer, itemElement.querySelector('.cm-item-del')); const editBtn = itemElement.querySelector('.cm-item-edit'); const delBtn = itemElement.querySelector('.cm-item-del'); if (editBtn) editBtn.style.display = 'none'; if (delBtn) delBtn.style.display = 'none'; function save() { let newTime = parseTimecode(timeInput.value); if (isNaN(newTime)) newTime = oldTime; const newTitle = titleInput.value.trim() || oldTitle; chapter.time = newTime; chapter.title = newTitle; refreshList(); } checkBtn.addEventListener('click', save); cancelBtn.addEventListener('click', () => refreshList()); timeInput.addEventListener('keypress', e => { if (e.key === 'Enter') save(); }); titleInput.addEventListener('keypress', e => { if (e.key === 'Enter') save(); }); } // --- Refresh list --- let listEl, listCountEl, clearBtn, copyBtn, currentChapterSpan; function refreshList() { if (!listEl) return; while (listEl.firstChild) listEl.removeChild(listEl.firstChild); listCountEl.textContent = chapters.length ? `${chapters.length} chapter${chapters.length > 1 ? 's' : ''}` : '0 chapter'; clearBtn.disabled = chapters.length === 0; copyBtn.disabled = chapters.length === 0; updateProgressSegments(); if (chapters.length === 0) { listEl.appendChild(el('div', { cls: 'cm-empty' }, createLucideIcon('M14.4 6 14 4H5v17h2v-7h5.6l.4 2h7V6z', 40), el('span', { txt: 'No chapters yet' }) )); return; } const sorted = [...chapters].sort((a,b) => a.time - b.time); for (const c of sorted) { const editBtn = el('button', { cls: 'cm-item-edit', 'aria-label': 'Edit' }, ICON_PENCIL.cloneNode(true)); const delBtn = el('button', { cls: 'cm-item-del', 'aria-label': 'Delete' }, ICON_TRASH.cloneNode(true)); const listItem = el('div', { cls: 'cm-item' }, el('span', { cls: 'cm-item-tc', txt: formatTime(c.time) }), el('span', { cls: 'cm-item-title', txt: c.title }), editBtn, delBtn ); editBtn.addEventListener('click', (e) => { e.stopPropagation(); editChapter(c, listItem); }); delBtn.addEventListener('click', () => { chapters = chapters.filter(x => x !== c); refreshList(); }); listEl.appendChild(listItem); } listEl.scrollTop = listEl.scrollHeight; } // --- Build panel --- function buildPanel() { if (panel) { panel.remove(); panel = null; } panel = el('div', { id: 'yt-cm-panel' }); const video = getVideo(); // Header const infoBtn = el('button', { cls: 'cm-icon-btn', 'aria-label': 'Info' }, ICON_INFO.cloneNode(true)); infoBtn.addEventListener('click', showInfoModal); const importHeaderBtn = el('button', { cls: 'cm-icon-btn', 'aria-label': 'Import YouTube chapters' }, ICON_IMPORT.cloneNode(true)); importHeaderBtn.addEventListener('click', importYouTubeChapters); const closeBtn = el('button', { cls: 'cm-icon-btn', 'aria-label': 'Close' }, ICON_CLOSE.cloneNode(true)); closeBtn.addEventListener('click', closePanel); panel.appendChild(el('div', { cls: 'cm-header' }, el('span', { cls: 'cm-header-title', txt: 'YouTube Chapter Marker' }), infoBtn, importHeaderBtn, closeBtn )); // Current time + chapter name const tcEl = el('div', { cls: 'cm-current-time', txt: video ? formatTime(video.currentTime) : '00:00' }); const currentChapterContainer = el('div', { cls: 'cm-current-chapter' }); const pinIcon = ICON_PIN.cloneNode(true); pinIcon.style.width = '14px'; pinIcon.style.height = '14px'; const chapterNameSpan = el('span'); currentChapterContainer.appendChild(pinIcon); currentChapterContainer.appendChild(chapterNameSpan); function updateCurrentChapter() { if (video) { const title = getCurrentChapterTitle(video.currentTime); chapterNameSpan.textContent = title || ''; } } updateCurrentChapter(); // Controls centered const controlsWrapper = el('div', { cls: 'cm-skip-buttons' }); const leftSteps = [-10, -5, -1]; const rightSteps = [1, 5, 10]; const playPauseBtn = el('button', { cls: 'cm-playpause', 'aria-label': 'Play/Pause' }); const updatePlayPauseIcon = () => { while (playPauseBtn.firstChild) playPauseBtn.removeChild(playPauseBtn.firstChild); const isPlaying = video && !video.paused; playPauseBtn.appendChild(isPlaying ? ICON_PAUSE.cloneNode(true) : ICON_PLAY.cloneNode(true)); }; updatePlayPauseIcon(); playPauseBtn.addEventListener('click', () => { if (!video) return; if (video.paused) video.play(); else video.pause(); updatePlayPauseIcon(); updateCurrentChapter(); }); if (video) { video.addEventListener('play', () => { updatePlayPauseIcon(); updateCurrentChapter(); }); video.addEventListener('pause', () => { updatePlayPauseIcon(); updateCurrentChapter(); }); } leftSteps.forEach(step => { const btn = el('button', { cls: 'cm-skip-btn', txt: `${step}` }); btn.addEventListener('click', () => { const v = getVideo(); if (v) { let newTime = v.currentTime + step; newTime = Math.min(Math.max(newTime, 0), v.duration); v.currentTime = newTime; tcEl.textContent = formatTime(newTime); updateCurrentChapter(); } }); controlsWrapper.appendChild(btn); }); controlsWrapper.appendChild(playPauseBtn); rightSteps.forEach(step => { const btn = el('button', { cls: 'cm-skip-btn', txt: `+${step}` }); btn.addEventListener('click', () => { const v = getVideo(); if (v) { let newTime = v.currentTime + step; newTime = Math.min(Math.max(newTime, 0), v.duration); v.currentTime = newTime; tcEl.textContent = formatTime(newTime); updateCurrentChapter(); } }); controlsWrapper.appendChild(btn); }); panel.appendChild(el('div', { cls: 'cm-current' }, el('div', { cls: 'cm-current-info' }, el('div', { cls: 'cm-current-label', txt: 'Current timecode' }), tcEl, currentChapterContainer ), controlsWrapper )); // Live update let tcInterval = setInterval(() => { const v = getVideo(); if (v) { tcEl.textContent = formatTime(v.currentTime); updateCurrentChapter(); } }, 100); // Input row const inputWrapper = el('div', { cls: 'cm-input-wrapper' }); const inputField = el('input', { type: 'text', placeholder: 'Chapter title...', autocomplete: 'off' }); const addButton = el('button', { cls: 'cm-input-add' }, ICON_PLUS.cloneNode(true)); addButton.addEventListener('click', () => { const title = inputField.value.trim(); if (!title) return; chapters.push({ time: video ? video.currentTime : 0, title }); inputField.value = ''; refreshList(); inputField.focus(); }); inputField.addEventListener('keydown', e => { if (e.key === 'Enter') addButton.click(); if (e.key === 'Escape') closePanel(); }); inputWrapper.appendChild(inputField); inputWrapper.appendChild(addButton); const quickAddBtn = el('button', { cls: 'cm-quick-btn' }); const plusIconSmall = ICON_PLUS.cloneNode(true); plusIconSmall.style.width = '14px'; plusIconSmall.style.height = '14px'; quickAddBtn.appendChild(plusIconSmall); quickAddBtn.appendChild(document.createTextNode(' Fast Chapter')); quickAddBtn.addEventListener('click', addQuickChapter); panel.appendChild(el('div', { cls: 'cm-input-row' }, el('div', { cls: 'cm-section-label', txt: 'New chapter' }), inputWrapper, quickAddBtn )); // List listCountEl = el('span', { cls: 'cm-list-count' }); clearBtn = el('button', { cls: 'cm-clear-btn', txt: 'Clear all' }); listEl = el('div', { cls: 'cm-list' }); clearBtn.addEventListener('click', () => { chapters = []; refreshList(); }); const listSection = el('div', { cls: 'cm-list-section' }, el('div', { cls: 'cm-list-header-row' }, listCountEl, clearBtn), listEl ); panel.appendChild(listSection); // Footer: copy + simple Ko-fi link (no custom icon) copyBtn = el('button', { cls: 'cm-copy-btn', txt: 'Copy timecodes' }); copyBtn.addEventListener('click', () => { navigator.clipboard.writeText(buildExportText()).then(() => { copyBtn.textContent = '✓ Copied!'; setTimeout(() => { copyBtn.textContent = 'Copy timecodes'; }, 2200); }); }); const creditLine = el('div', { cls: 'cm-footer-credit' }, el('span', { style: 'display:inline-flex; align-items:center; gap:4px;' }, ICON_COPYRIGHT.cloneNode(true), 'VooDoo / The Coven'), el('a', { href: 'https://ko-fi.com/X7X73E5ZX', target: '_blank' }, 'Support me on Ko-fi') ); panel.appendChild(el('div', { cls: 'cm-footer' }, copyBtn, creditLine)); refreshList(); document.body.appendChild(panel); requestAnimationFrame(() => requestAnimationFrame(() => panel.classList.add('open'))); setTimeout(() => inputField.focus(), 230); panel._cleanup = () => { clearInterval(tcInterval); if (video) { video.removeEventListener('play', updatePlayPauseIcon); video.removeEventListener('pause', updatePlayPauseIcon); } if (segmentContainer) segmentContainer.remove(); if (window._cmProgressCleanup) window._cmProgressCleanup(); }; } // --- Info modal (clean) --- function showInfoModal() { const overlay = el('div', { cls: 'cm-modal-overlay' }); const modal = el('div', { cls: 'cm-modal' }, el('div', { cls: 'cm-modal-header' }, el('h3', { txt: 'YouTube Chapter Marker' })), el('div', { cls: 'cm-modal-body' }, el('div', { cls: 'cm-modal-section' }, el('h4', { txt: 'About this script' }), el('p', { txt: 'This script allows you to create, edit, import and manage custom chapter markers for any YouTube video. All chapters are stored locally per video.' }) ), el('div', { cls: 'cm-modal-section' }, el('h4', { txt: 'How to use' }), el('ul', {}, el('li', { txt: 'Add a chapter: type a title and press the + button or hit Enter.' }), el('li', { txt: 'Quick add: use "+ Fast Chapter" to add auto‑numbered chapters.' }), el('li', { txt: 'Edit: click the pencil icon next to any chapter (modify timecode or title).' }), el('li', { txt: 'Delete: click the trash icon or use "Clear all".' }), el('li', { txt: 'Import: click the import icon (top bar) to fetch chapters from video description.' }), el('li', { txt: 'Export: click "Copy timecodes" to get a formatted list ready for YouTube description.' }), el('li', { txt: 'Navigate: use the skip buttons (±1, ±5, ±10) and play/pause.' }), el('li', { txt: 'Visual markers: colored segments on the progress bar show chapter zones. Hover to see the chapter name.' }) ) ) ), el('div', { cls: 'cm-modal-footer' }, el('button', { cls: 'cm-modal-close', txt: 'Got it' })) ); overlay.appendChild(modal); document.body.appendChild(overlay); const closeModal = () => overlay.remove(); modal.querySelector('.cm-modal-close').addEventListener('click', closeModal); overlay.addEventListener('click', (e) => { if (e.target === overlay) closeModal(); }); } function closePanel() { if (!panel) return; if (panel._cleanup) panel._cleanup(); panel.classList.remove('open'); setTimeout(() => { if (panel) { panel.remove(); panel = null; } }, 230); } function togglePanel() { if (panel) closePanel(); else buildPanel(); } // --- Inject YouTube button --- function injectButton() { if (document.getElementById('yt-chapter-btn')) return true; const rightControls = document.querySelector('.ytp-right-controls'); if (!rightControls) return false; const tooltip = el('span', { cls: 'yt-cm-tooltip', txt: 'Chapters' }); const btn = el('button', { id: 'yt-chapter-btn', cls: 'ytp-button', 'aria-label': 'Chapters' }, createLucideIcon('M14.4 6 14 4H5v17h2v-7h5.6l.4 2h7V6z', 24), tooltip); btn.addEventListener('click', togglePanel); const fullscreen = rightControls.querySelector('.ytp-fullscreen-button'); if (fullscreen && fullscreen.parentNode) fullscreen.parentNode.insertBefore(btn, fullscreen); else { const rr = rightControls.querySelector('.ytp-right-controls-right'); (rr || rightControls).prepend(btn); } return true; } function startInjectionLoop() { if (injectInterval) clearInterval(injectInterval); let attempts = 0; injectInterval = setInterval(() => { attempts++; if (injectButton() || attempts > 60) clearInterval(injectInterval); }, 500); } // --- SPA observer --- let lastUrl = location.href; new MutationObserver(() => { if (location.href !== lastUrl) { lastUrl = location.href; chapters = []; closePanel(); document.getElementById('yt-chapter-btn')?.remove(); if (location.pathname === '/watch') startInjectionLoop(); } if (location.pathname === '/watch' && !document.getElementById('yt-chapter-btn')) injectButton(); }).observe(document.body, { childList: true, subtree: true }); if (location.pathname === '/watch') startInjectionLoop(); })();