// content.js - 컨텐트 스크립트 console.log('Content script loaded on:', window.location.href); // 페이지에 스타일 추가 const style = document.createElement('style'); style.textContent = ` .my-extension-highlight { background-color: yellow !important; padding: 2px !important; border-radius: 3px !important; } .my-extension-overlay { position: fixed; top: 10px; right: 10px; background: #333; color: white; padding: 10px; border-radius: 5px; z-index: 10000; font-family: Arial, sans-serif; font-size: 14px; } `; document.head.appendChild(style); // 메시지 리스너 chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { console.log('Content script received message:', request); switch (request.action) { case 'changeBackgroundColor': changeBackgroundColor(request.color); sendResponse({ success: true }); break; case 'getPageText': const pageText = document.body.innerText; sendResponse({ success: true, text: pageText }); break; case 'highlightText': highlightText(request.text); sendResponse({ success: true }); break; case 'pageLoaded': handlePageLoaded(request.url); sendResponse({ success: true }); break; case 'processSelection': processSelectedText(request.selectionText); sendResponse({ success: true }); break; default: sendResponse({ success: false, error: 'Unknown action' }); } return true; // 비동기 응답을 위해 true 반환 }); // 배경색 변경 함수 function changeBackgroundColor(color) { document.body.style.transition = 'background-color 0.3s ease'; document.body.style.backgroundColor = color; // 3초 후 원래 색상으로 복구 setTimeout(() => { document.body.style.backgroundColor = ''; }, 3000); } // 텍스트 하이라이트 함수 function highlightText(searchText) { const walker = document.createTreeWalker( document.body, NodeFilter.SHOW_TEXT, null, false ); const textNodes = []; let node; while (node = walker.nextNode()) { if (node.nodeValue.includes(searchText)) { textNodes.push(node); } } textNodes.forEach(textNode => { const parent = textNode.parentNode; const wrapper = document.createElement('span'); wrapper.className = 'my-extension-highlight'; parent.insertBefore(wrapper, textNode); wrapper.appendChild(textNode); }); } // 페이지 로드 처리 함수 function handlePageLoaded(url) { console.log('Page loaded:', url); // 오버레이 메시지 표시 const overlay = document.createElement('div'); overlay.className = 'my-extension-overlay'; overlay.textContent = 'My Extension이 활성화되었습니다!'; document.body.appendChild(overlay); // 3초 후 오버레이 제거 setTimeout(() => { overlay.remove(); }, 3000); } // 선택된 텍스트 처리 함수 function processSelectedText(selectedText) { console.log('Processing selected text:', selectedText); // Background script에 데이터 전송 chrome.runtime.sendMessage({ action: 'saveToStorage', data: { lastSelectedText: selectedText, timestamp: Date.now() } }, (response) => { if (response.success) { console.log('Selected text saved to storage'); } }); } // 페이지 로드 완료 시 자동 실행 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initExtension); } else { initExtension(); } function initExtension() { console.log('Extension initialized on page'); // 페이지별 커스텀 로직 if (window.location.hostname.includes('github.com')) { console.log('GitHub detected - applying GitHub-specific features'); // GitHub 전용 기능 추가 } if (window.location.hostname.includes('stackoverflow.com')) { console.log('Stack Overflow detected - applying SO-specific features'); // Stack Overflow 전용 기능 추가 } } // DOM 변경 감지 (옵션) const observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { if (mutation.type === 'childList') { // DOM 변경 감지 시 실행할 로직 console.log('DOM changed'); } }); }); observer.observe(document.body, { childList: true, subtree: true });