// Content script for YouTube pages (function() { 'use strict'; // Function to get video information function getVideoInfo() { const titleElement = document.querySelector('h1.ytd-watch-metadata yt-formatted-string') || document.querySelector('h1.ytd-video-primary-info-renderer .title') || document.querySelector('#title h1') || document.querySelector('h1'); const title = titleElement ? titleElement.textContent.trim() : document.title; const currentUrl = window.location.href; const cleanUrl = getCleanVideoUrl(currentUrl); return { title: title, url: cleanUrl, videoId: getVideoId(currentUrl) }; } // Extract video ID from URL function getVideoId(url) { const match = url.match(/[?&]v=([^&]+)/); return match ? match[1] : null; } // Clean YouTube URL to only include the video ID function getCleanVideoUrl(url) { const videoId = getVideoId(url); if (videoId) { return `https://www.youtube.com/watch?v=${videoId}`; } return url; // Return original if we can't extract video ID } // Listen for messages from popup chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.action === 'getVideoInfo') { const videoInfo = getVideoInfo(); sendResponse(videoInfo); } return true; // Keep message channel open for async response }); // Optional: Monitor for navigation changes on YouTube (SPA) let lastUrl = location.href; new MutationObserver(() => { const url = location.href; if (url !== lastUrl) { lastUrl = url; // URL changed, video info might have changed console.log('YouTube navigation detected'); } }).observe(document, { subtree: true, childList: true }); })();