// ==UserScript==
// @name         종합 웹 크롤러 8.6
// @namespace    http://tampermonkey.net/
// @version      8.6
// @description  고급 기능을 갖춘 웹 크롤러로 검색 결과를 실시간으로 수집하고 표시 (대폭 개선된 버전)
// @author       YourName
// @match        *://www.google.com/search*
// @match        *://www.bing.com/search*
// @match        *://search.daum.net/search*
// @match        *://search.naver.com/search*
// @match        *://search.yahoo.com/search*
// @match        *://duckduckgo.com/*
// @match        *://www.startpage.com/do/search*
// @match        *://www.ecosia.org/search*
// @match        *://www.qwant.com/*
// @match        *://ahmia.fi/*
// @match        *://torchsearch.de/*
// @match        *://notracker.org/*
// @match        *://onion.city/search/*
// @match        *://darksearch.io/*
// @match        *://torsearch.ml/*
// @match        *://phobos.onion/*
// @match        *://seamonkey.onion/*
// @match        *://kilos.onion/*
// @match        *://haystak5nqiidfld.onion/*
// @match        *://bigsearch3kwz4abe.onion/*
// @match        *://snaptorrents5nfyh4.onion/*
// @match        *://theoneiroi3amnhv3.onion/*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_notification
// @connect      *
// @require      https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js
// @require      https://cdn.jsdelivr.net/npm/chart.js
// ==/UserScript==

(function() {
    'use strict';

    // 스타일 추가
    GM_addStyle(`
        #crawler-container {
            position: fixed;
            top: 10px;
            right: 10px;
            background-color: #ffffff;
            border: 2px solid #4CAF50;
            padding: 10px;
            z-index: 10000;
            width: 400px;
            max-height: 80vh;
            overflow: hidden;
            box-shadow: 0 6px 20px rgba(0,0,0,0.3);
            border-radius: 8px;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            font-size: 13px;
            display: flex;
            flex-direction: column;
            transition: all 0.3s ease;
        }
        #crawler-container header {
            cursor: move;
            background-color: #4CAF50;
            color: white;
            padding: 6px 10px;
            border-radius: 6px 6px 0 0;
            text-align: center;
            font-size: 18px;
            font-weight: bold;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        #crawler-container header .title {
            flex-grow: 1;
        }
        #crawler-container header .close-btn {
            cursor: pointer;
            font-size: 20px;
            color: #ff6666;
            margin-left: 8px;
        }
        #crawler-container .tabs {
            display: flex;
            border-bottom: 2px solid #ccc;
            margin-top: 8px;
        }
        #crawler-container .tab {
            flex: 1;
            padding: 8px;
            text-align: center;
            cursor: pointer;
            background-color: #e8e8e8;
            border-right: 1px solid #bbb;
            font-weight: bold;
            transition: background-color 0.3s, color 0.3s;
        }
        #crawler-container .tab:last-child {
            border-right: none;
        }
        #crawler-container .tab.active {
            background-color: #ffffff;
            border-bottom: 3px solid #4CAF50;
            color: #4CAF50;
        }
        #crawler-container .tab-content {
            display: none;
            padding: 10px;
            overflow-y: auto;
            flex-grow: 1;
        }
        #crawler-container .tab-content.active {
            display: block;
        }
        #crawler-container button {
            padding: 8px 14px;
            margin: 4px 2px;
            border: none;
            border-radius: 4px;
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
            transition: background-color 0.3s;
            font-size: 13px;
        }
        #crawler-container button:hover {
            background-color: #45a049;
        }
        #crawler-container button.export-btn {
            background-color: #2196F3;
        }
        #crawler-container button.export-btn:hover {
            background-color: #0b7dda;
        }
        #crawler-container button.pause-btn {
            background-color: #FF9800;
        }
        #crawler-container button.pause-btn:hover {
            background-color: #e68a00;
        }
        #crawler-container .options,
        #crawler-container .filters,
        #crawler-container .exclude-domains,
        #crawler-container .advanced-options,
        #crawler-container .controls,
        #crawler-container .search-engines {
            margin-bottom: 15px;
        }
        #crawler-container .options label,
        #crawler-container .advanced-options label,
        #crawler-container .controls label,
        #crawler-container .search-engines label {
            margin-right: 15px;
            display: inline-flex;
            align-items: center;
            font-weight: normal;
        }
        #crawler-container .options input[type="checkbox"],
        #crawler-container .advanced-options input[type="checkbox"],
        #crawler-container .controls input[type="number"],
        #crawler-container .search-engines select {
            margin-right: 6px;
        }
        #crawler-container input[type="text"],
        #crawler-container textarea,
        #crawler-container input[type="number"],
        #crawler-container select {
            width: 100%;
            padding: 8px;
            margin: 4px 0 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 13px;
        }
        #crawler-container table {
            border-collapse: collapse;
            width: 100%;
            margin-top: 10px;
            font-size: 13px;
        }
        #crawler-container th, #crawler-container td {
            padding: 8px;
            border: 1px solid #ddd;
            text-align: left;
            vertical-align: top;
        }
        #crawler-container th {
            background-color: #4CAF50;
            color: white;
            position: sticky;
            top: 0;
            z-index: 1;
            cursor: pointer;
        }
        #crawler-container .log {
            max-height: 200px;
            overflow-y: auto;
            background-color: #e6f7ff;
            padding: 10px;
            border: 1px solid #91d5ff;
            border-radius: 4px;
            font-size: 13px;
            white-space: pre-wrap;
        }
        #crawler-container .controls,
        #crawler-container .options,
        #crawler-container .advanced-options,
        #crawler-container .notification-area,
        #crawler-container .search-engines {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
        }
        #crawler-container .filters label,
        #crawler-container .exclude-domains label {
            display: block;
            margin-bottom: 6px;
            font-weight: bold;
        }
        #crawler-container progress {
            width: 80%;
            height: 20px;
            margin-right: 10px;
            border-radius: 4px;
        }
        #crawler-container .status {
            margin-top: 10px;
            font-weight: bold;
            font-size: 14px;
        }
        #crawler-container .chart-container {
            position: relative;
            height: 250px;
            width: 100%;
            margin-top: 20px;
        }
        #crawler-container .notification-area {
            flex-direction: row;
            align-items: center;
            width: 100%;
        }
        @media (max-width: 500px) {
            #crawler-container {
                width: 90%;
                right: 5%;
                top: 5px;
            }
            #crawler-container .tabs {
                flex-wrap: wrap;
            }
            #crawler-container .controls,
            #crawler-container .options,
            #crawler-container .advanced-options,
            #crawler-container .notification-area,
            #crawler-container .search-engines {
                flex-direction: column;
                align-items: flex-start;
            }
            #crawler-container .search-engines select {
                width: 100%;
            }
            #crawler-container progress {
                width: 100%;
            }
        }
    `);

    // 크롤러 컨테이너 생성 및 추가
    const container = document.createElement('div');
    container.id = 'crawler-container';
    container.innerHTML = `
        <header>
            <span class="title">🔥 웹 크롤러 8.6</span>
            <span class="close-btn" title="닫기">&times;</span>
        </header>
        <div class="tabs">
            <div class="tab active" data-tab="settings">설정</div>
            <div class="tab" data-tab="results">결과</div>
            <div class="tab" data-tab="logs">로그</div>
            <div class="tab" data-tab="analytics">분석</div>
            <div class="tab" data-tab="about">정보</div>
        </div>
        <div class="tab-content active" id="settings">
            <div class="notification-area">
                <button id="startBtn" title="크롤링 시작">▶️ 시작</button>
                <button id="pauseBtn" class="pause-btn" title="크롤링 일시정지/재개">⏸️ 일시정지</button>
                <button id="stopBtn" title="크롤링 중지">⏹️ 중지</button>
                <button id="resetBtn" title="크롤링 초기화">🔄 초기화</button>
                <button id="exportBtn" class="export-btn" title="결과 내보내기">💾 내보내기</button>
                <button id="clearLogsBtn" title="로그 초기화">🧹 로그 초기화</button>
            </div>
            <div class="search-engines">
                <label for="searchEngineSelect">검색 엔진 선택:</label>
                <select id="searchEngineSelect">
                    <option value="google" selected>Google</option>
                    <option value="bing">Bing</option>
                    <option value="daum">Daum</option>
                    <option value="naver">Naver</option>
                    <option value="yahoo">Yahoo</option>
                    <option value="duckduckgo">DuckDuckGo</option>
                    <option value="startpage">StartPage</option>
                    <option value="ecosia">Ecosia</option>
                    <option value="qwant">Qwant</option>
                    <option value="ahmia">Ahmia (Tor)</option>
                    <option value="torch">Torch (Tor)</option>
                    <option value="notracker">NOTRACKER (Tor)</option>
                    <option value="onioncity">Onion.City (Tor)</option>
                    <option value="darksearch">DarkSearch.io (Tor)</option>
                    <option value="torsearch">TorSearch.ml (Tor)</option>
                    <option value="phobos">Phobos (Tor)</option>
                    <option value="seamonkey">SeaMonkey (Tor)</option>
                    <option value="kilos">Kilos (Tor)</option>
                    <option value="haystak">Haystak (Tor)</option>
                    <option value="bigsearch">BigSearch (Tor)</option>
                    <option value="snaptorrents">SnapTorrents (Tor)</option>
                    <option value="theoneiroi">TheOneiroi (Tor)</option>
                </select>
                <button id="goToEngineBtn" title="선택한 검색 엔진으로 이동">🔗 실행 검색 엔진 열기</button>
            </div>
            <div class="options">
                <label><input type="checkbox" id="toggleHTML" checked> HTML 가져오기</label>
                <label><input type="checkbox" id="toggleImages" checked> 이미지 가져오기</label>
                <label><input type="checkbox" id="toggleAds" checked> 광고 제외</label>
            </div>
            <div class="filters">
                <label for="filterInput">필터 단어 (정규식 또는 단어, 쉼표로 구분):</label>
                <input type="text" id="filterInput" placeholder="예: 기부, 광고, /promo/i">
            </div>
            <div class="exclude-domains">
                <label for="excludeDomainsInput">제외할 도메인 (쉼표로 구분):</label>
                <input type="text" id="excludeDomainsInput" placeholder="예: facebook.com, youtube.com">
            </div>
            <div class="advanced-options">
                <label for="duplicateThreshold">중복 허용 횟수:</label>
                <input type="number" id="duplicateThreshold" value="2" min="1" style="width: 70px;">
                <label for="retryAttempts">재시도 횟수:</label>
                <input type="number" id="retryAttempts" value="3" min="1" style="width: 70px;">
                <label for="retryInterval">재시도 간격 (ms):</label>
                <input type="number" id="retryInterval" value="500" min="100" style="width: 90px;">
            </div>
            <div style="margin-top:10px;">
                <label for="progressBar">진행률:</label>
                <progress id="progressBar" value="0" max="100"></progress>
                <span id="progressText">0%</span>
            </div>
            <div class="status">
                현재 페이지: <span id="currentPage">0</span> /
                총 페이지: <span id="totalPages">0</span>
                <br>
                방문된 사이트: <span id="visitedCount">0</span>
                <br>
                경과 시간: <span id="elapsedTime">00:00:00</span>
                <br>
                예상 남은 시간: <span id="remainingTime">00:00:00</span>
            </div>
        </div>
        <div class="tab-content" id="results">
            <div style="margin-top:10px; max-height: 350px; overflow: auto;">
                <table id="resultsTable">
                    <thead>
                        <tr>
                            <th data-sort="0" onclick="sortTable(0)">사이트 이름 &#x25B2;&#x25BC;</th>
                            <th data-sort="1" onclick="sortTable(1)">주소 &#x25B2;&#x25BC;</th>
                            <th data-sort="2" onclick="sortTable(2)">설명 &#x25B2;&#x25BC;</th>
                            <th>이미지</th>
                            <th>HTML 링크</th>
                            <th>WHOIS 정보</th>
                            <th data-sort="6" onclick="sortTable(6)">페이지 로딩 시간 (ms) &#x25B2;&#x25BC;</th>
                        </tr>
                    </thead>
                    <tbody id="resultsBody">
                    </tbody>
                </table>
            </div>
            <div style="margin-top:10px;">
                <input type="text" id="searchResultsInput" placeholder="결과 검색...">
            </div>
        </div>
        <div class="tab-content" id="logs">
            <div class="log" id="log">
                <strong>로그:</strong>
            </div>
        </div>
        <div class="tab-content" id="analytics">
            <div class="chart-container">
                <canvas id="crawlChart"></canvas>
            </div>
            <div class="chart-container">
                <canvas id="loadTimeChart"></canvas>
            </div>
            <div class="chart-container">
                <canvas id="activityChart"></canvas>
            </div>
        </div>
        <div class="tab-content" id="about">
            <h2>웹 크롤러 8.6 정보</h2>
            <p>종합 웹 크롤러는 다양한 검색 엔진에서 실시간으로 검색 결과를 수집하고 분석할 수 있는 도구입니다. 사용자의 편의성과 데이터 가독성을 고려하여 한 단계 더욱 대폭 개선된 버전입니다.</p>
            <ul>
                <li>지원하는 검색 엔진: Google, Bing, Daum, Naver, Yahoo, DuckDuckGo, StartPage, Ecosia, Qwant, Ahmia (Tor), Torch (Tor), NOTRACKER (Tor), Onion.City (Tor), DarkSearch.io (Tor), TorSearch.ml (Tor), Phobos (Tor), SeaMonkey (Tor), Kilos (Tor), Haystak (Tor), BigSearch (Tor), SnapTorrents (Tor), TheOneiroi (Tor)</li>
                <li>실시간 검색어 감지 및 크롤링</li>
                <li>멀티 쿼리 지원: 한 번에 여러 검색어로 크롤링 가능</li>
                <li>고급 필터링 옵션 제공</li>
                <li>결과를 XLSX, CSV, JSON 형식으로 내보내기</li>
                <li>시각화된 데이터 차트 제공: 방문 횟수, 페이지 로딩 시간, 활동 패턴 등</li>
                <li>사용자 친화적인 인터페이스 및 반응형 디자인</li>
                <li>결과 테이블 정렬 및 검색 기능 강화</li>
                <li>더 자세한 로그 제공: 정보, 경고, 오류 분류</li>
                <li>WHOIS 정보 연동 기능</li>
                <li>다양한 알림 방식 지원</li>
                <li>자동 스크롤 및 실시간 업데이트</li>
                <li>고급 에러 핸들링 및 재시도 로직 강화</li>
                <li>로그 초기화 기능 추가</li>
                <li>결과 검색 기능 강화</li>
                <li>추가 분석 탭에서 다양한 그래프 제공</li>
                <li>데이터 내보내기 시 중복 제거 및 정렬 기능 강화</li>
            </ul>
            <p><strong>버전:</strong> 8.6</p>
            <p><strong>제작자:</strong> YourName</p>
            <p><strong>문의:</strong> your.email@example.com</p>
        </div>
    `;
    document.body.appendChild(container);

    // 검색 엔진 URL 매핑
    const searchEngineURLs = {
        'google': 'https://www.google.com',
        'bing': 'https://www.bing.com',
        'daum': 'https://www.daum.net',
        'naver': 'https://www.naver.com',
        'yahoo': 'https://search.yahoo.com',
        'duckduckgo': 'https://www.duckduckgo.com',
        'startpage': 'https://www.startpage.com',
        'ecosia': 'https://www.ecosia.org',
        'qwant': 'https://www.qwant.com',
        'ahmia': 'http://ahmia.fi',
        'torch': 'http://torchsearch.de',
        'notracker': 'http://notracker.org',
        'onioncity': 'http://onion.city',
        'darksearch': 'http://darksearch.io',
        'torsearch': 'http://torsearch.ml',
        'phobos': 'http://phobos.onion',
        'seamonkey': 'http://seamonkey.onion',
        'kilos': 'http://kilos.onion',
        'haystak': 'http://haystak5nqiidfld.onion',
        'bigsearch': 'http://bigsearch3kwz4abe.onion',
        'snaptorrents': 'http://snaptorrents5nfyh4.onion',
        'theoneiroi': 'http://theoneiroi3amnhv3.onion',
    };

    // 드래그 기능
    (function makeDraggable(elmnt) {
        const header = elmnt.querySelector('header');
        const closeBtn = elmnt.querySelector('.close-btn');
        let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
        header.onmousedown = dragMouseDown;

        closeBtn.onclick = () => {
            elmnt.style.display = 'none';
            log('🗑 크롤러 창을 닫았습니다.');
        }

        function dragMouseDown(e) {
            e = e || window.event;
            e.preventDefault();
            pos3 = e.clientX;
            pos4 = e.clientY;
            document.onmouseup = closeDragElement;
            document.onmousemove = elementDrag;
        }

        function elementDrag(e) {
            e = e || window.event;
            e.preventDefault();
            pos1 = pos3 - e.clientX;
            pos2 = pos4 - e.clientY;
            pos3 = e.clientX;
            pos4 = e.clientY;
            elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
            elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
        }

        function closeDragElement() {
            document.onmouseup = null;
            document.onmousemove = null;
        }
    })(container);

    // 탭 설정
    (function setupTabs() {
        const tabs = container.querySelectorAll('.tab');
        const tabContents = container.querySelectorAll('.tab-content');

        tabs.forEach(tab => {
            tab.addEventListener('click', () => {
                tabs.forEach(t => t.classList.remove('active'));
                tab.classList.add('active');

                const target = tab.getAttribute('data-tab');
                tabContents.forEach(content => {
                    if(content.id === target){
                        content.classList.add('active');
                    } else {
                        content.classList.remove('active');
                    }
                });
            });
        });
    })();

    // 변수 초기화
    let isRunning = false;
    let isPaused = false;
    let currentPage = 1;
    let totalPages = 0;
    let results = [];
    let searchQueries = [];
    let currentQueryIndex = 0;
    let searchEngine = 'google';
    let visitedLinks = new Map();
    let logMessages = [];
    let filterPatterns = [];
    let excludeDomains = [];
    let duplicateThreshold = 2;
    let retryAttempts = 3;
    let retryInterval = 500;
    let startTime = null;
    let elapsedInterval = null;
    let crawlChart = null;
    let loadTimeChart = null;
    let activityChart = null;
    let crawlData = {
        labels: [],
        datasets: [{
            label: '방문 횟수',
            data: [],
            backgroundColor: 'rgba(75, 192, 192, 0.6)',
            borderColor: 'rgba(75, 192, 192, 1)',
            borderWidth: 1
        },
        {
            label: '평균 페이지 로딩 시간 (ms)',
            data: [],
            backgroundColor: 'rgba(153, 102, 255, 0.6)',
            borderColor: 'rgba(153, 102, 255, 1)',
            borderWidth: 1
        }]
    };
    let loadTimeData = {
        labels: [],
        datasets: [{
            label: '페이지 로딩 시간 (ms)',
            data: [],
            backgroundColor: 'rgba(255, 99, 132, 0.6)',
            borderColor: 'rgba(255,99,132,1)',
            borderWidth: 1
        }]
    };
    let activityData = {
        labels: [],
        datasets: [{
            label: '활동도',
            data: [],
            backgroundColor: [
                'rgba(54, 162, 235, 0.6)',
                'rgba(255, 206, 86, 0.6)',
                'rgba(75, 192, 192, 0.6)',
                'rgba(153, 102, 255, 0.6)',
                'rgba(255, 159, 64, 0.6)',
                'rgba(199, 199, 199, 0.6)',
                'rgba(83, 102, 255, 0.6)',
                'rgba(255, 99, 132, 0.6)',
                'rgba(54, 162, 235, 0.6)',
                'rgba(255, 206, 86, 0.6)'
            ],
            borderColor: [
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)',
                'rgba(199, 199, 199, 1)',
                'rgba(83, 102, 255, 1)',
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)'
            ],
            borderWidth: 1
        }]
    };

    // 요소 참조
    const startBtn = document.getElementById('startBtn');
    const pauseBtn = document.getElementById('pauseBtn');
    const stopBtn = document.getElementById('stopBtn');
    const resetBtn = document.getElementById('resetBtn');
    const exportBtn = document.getElementById('exportBtn');
    const clearLogsBtn = document.getElementById('clearLogsBtn');
    const progressBar = document.getElementById('progressBar');
    const progressText = document.getElementById('progressText');
    const resultsBody = document.getElementById('resultsBody');
    const logContainer = document.getElementById('log');
    const currentPageElem = document.getElementById('currentPage');
    const totalPagesElem = document.getElementById('totalPages');
    const toggleHTML = document.getElementById('toggleHTML');
    const toggleImages = document.getElementById('toggleImages');
    const toggleAds = document.getElementById('toggleAds');
    const filterInput = document.getElementById('filterInput');
    const excludeDomainsInput = document.getElementById('excludeDomainsInput');
    const duplicateThresholdInput = document.getElementById('duplicateThreshold');
    const retryAttemptsInput = document.getElementById('retryAttempts');
    const retryIntervalInput = document.getElementById('retryInterval');
    const visitedCountElem = document.getElementById('visitedCount');
    const elapsedTimeElem = document.getElementById('elapsedTime');
    const remainingTimeElem = document.getElementById('remainingTime');
    const crawlChartCtx = document.getElementById('crawlChart').getContext('2d');
    const loadTimeChartCtx = document.getElementById('loadTimeChart').getContext('2d');
    const activityChartCtx = document.getElementById('activityChart').getContext('2d');
    const searchEngineSelect = document.getElementById('searchEngineSelect');
    const searchResultsInput = document.getElementById('searchResultsInput');
    const goToEngineBtn = document.getElementById('goToEngineBtn');

    // 이벤트 리스너 등록
    startBtn.addEventListener('click', startCrawling);
    pauseBtn.addEventListener('click', togglePause);
    stopBtn.addEventListener('click', stopCrawling);
    resetBtn.addEventListener('click', resetCrawling);
    exportBtn.addEventListener('click', exportResults);
    clearLogsBtn.addEventListener('click', clearLogs);
    filterInput.addEventListener('input', updateFilterPatterns);
    excludeDomainsInput.addEventListener('input', updateExcludeDomains);
    duplicateThresholdInput.addEventListener('change', updateDuplicateThreshold);
    retryAttemptsInput.addEventListener('change', updateRetryAttempts);
    retryIntervalInput.addEventListener('change', updateRetryInterval);
    searchEngineSelect.addEventListener('change', updateSearchEngine);
    searchResultsInput.addEventListener('input', filterResults);
    goToEngineBtn.addEventListener('click', goToSelectedEngine);

    window.addEventListener('beforeunload', () => {
        resetCrawling();
    });

    // 로그 및 크롤 데이터 로드
    loadLogs();
    loadCrawlData();
    loadSettings();

    // 차트 초기화 함수
    function initializeChart() {
        crawlChart = new Chart(crawlChartCtx, {
            type: 'bar',
            data: crawlData,
            options: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    y: {
                        beginAtZero: true,
                        ticks: {
                            precision:0
                        }
                    }
                },
                plugins: {
                    legend: {
                        display: true,
                        position: 'top'
                    },
                    tooltip: {
                        enabled: true
                    }
                }
            }
        });

        loadTimeChart = new Chart(loadTimeChartCtx, {
            type: 'line',
            data: loadTimeData,
            options: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    y: {
                        beginAtZero: true,
                        ticks: {
                            precision:0
                        }
                    }
                },
                plugins: {
                    legend: {
                        display: true,
                        position: 'top'
                    },
                    tooltip: {
                        enabled: true
                    }
                }
            }
        });

        activityChart = new Chart(activityChartCtx, {
            type: 'pie',
            data: activityData,
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        display: true,
                        position: 'bottom'
                    },
                    tooltip: {
                        enabled: true
                    }
                }
            }
        });
    }
    initializeChart();

    // 설정 로드
    function loadSettings() {
        const storedSearchEngine = GM_getValue('searchEngine', 'google');
        searchEngine = storedSearchEngine;
        searchEngineSelect.value = searchEngine;

        const storedToggleHTML = GM_getValue('toggleHTML', true);
        toggleHTML.checked = storedToggleHTML;

        const storedToggleImages = GM_getValue('toggleImages', true);
        toggleImages.checked = storedToggleImages;

        const storedToggleAds = GM_getValue('toggleAds', true);
        toggleAds.checked = storedToggleAds;

        const storedDuplicateThreshold = GM_getValue('duplicateThreshold', 2);
        duplicateThreshold = storedDuplicateThreshold;
        duplicateThresholdInput.value = duplicateThreshold;

        const storedRetryAttempts = GM_getValue('retryAttempts', 3);
        retryAttempts = storedRetryAttempts;
        retryAttemptsInput.value = retryAttempts;

        const storedRetryInterval = GM_getValue('retryInterval', 500);
        retryInterval = storedRetryInterval;
        retryIntervalInput.value = retryInterval;

        const storedFilterInput = GM_getValue('filterInput', '');
        filterInput.value = storedFilterInput;
        updateFilterPatterns();

        const storedExcludeDomainsInput = GM_getValue('excludeDomainsInput', '');
        excludeDomainsInput.value = storedExcludeDomainsInput;
        updateExcludeDomains();

        const storedSearchQueries = GM_getValue('searchQueries', '');
        if (storedSearchQueries) {
            searchQueries = storedSearchQueries.split(',').map(q => q.trim()).filter(q => q.length > 0);
            if (searchQueries.length > 0) {
                searchResultsInput.value = searchQueries.join(', ');
            }
        }
    }

    // 설정 저장 함수
    function saveSettings() {
        GM_setValue('searchEngine', searchEngine);
        GM_setValue('toggleHTML', toggleHTML.checked);
        GM_setValue('toggleImages', toggleImages.checked);
        GM_setValue('toggleAds', toggleAds.checked);
        GM_setValue('duplicateThreshold', duplicateThreshold);
        GM_setValue('retryAttempts', retryAttempts);
        GM_setValue('retryInterval', retryInterval);
        GM_setValue('filterInput', filterInput.value.trim());
        GM_setValue('excludeDomainsInput', excludeDomainsInput.value.trim());
        GM_setValue('searchQueries', searchQueries.join(', '));
    }

    // 검색 엔진 변경 시 처리
    function updateSearchEngine() {
        searchEngine = searchEngineSelect.value;
        GM_setValue('searchEngine', searchEngine);
        log(`🔄 검색 엔진 변경: ${capitalizeFirstLetter(searchEngine)}`);
    }

    // 다양한 설정 변경 시 설정 저장
    toggleHTML.addEventListener('change', saveSettings);
    toggleImages.addEventListener('change', saveSettings);
    toggleAds.addEventListener('change', saveSettings);
    filterInput.addEventListener('change', saveSettings);
    excludeDomainsInput.addEventListener('change', saveSettings);

    // 로그 로드
    function loadLogs() {
        const storedLogs = GM_getValue('crawlerLogs', []);
        if (storedLogs.length > 0) {
            logMessages = JSON.parse(storedLogs);
            logContainer.innerHTML = `<strong>로그:</strong><br>` + logMessages.join('<br>');
            logContainer.scrollTop = logContainer.scrollHeight;
        }
    }

    // 필터 패턴 업데이트
    function updateFilterPatterns() {
        const input = filterInput.value.trim();
        filterPatterns = input ? input.split(',').map(word => {
            word = word.trim();
            if (word.startsWith('/') && word.endsWith('/')) {
                try {
                    return new RegExp(word.slice(1, -1), 'i');
                } catch (e) {
                    log(`⚠️ 유효하지 않은 정규식: ${word}`);
                    return null;
                }
            } else {
                return new RegExp(word, 'i');
            }
        }).filter(regex => regex !== null) : [];
        log(`🛠 필터 패턴 업데이트: [${filterPatterns.map(r => r.toString()).join(', ')}]`);
        saveSettings();
    }

    // 제외 도메인 업데이트
    function updateExcludeDomains() {
        const input = excludeDomainsInput.value.trim();
        excludeDomains = input ? input.split(',').map(domain => domain.trim().toLowerCase()) : [];
        log(`🚫 제외 도메인 업데이트: [${excludeDomains.join(', ')}]`);
        saveSettings();
    }

    // 중복 허용 횟수 업데이트
    function updateDuplicateThreshold() {
        const value = parseInt(duplicateThresholdInput.value);
        duplicateThreshold = isNaN(value) || value < 1 ? 2 : value;
        log(`🔄 중복 허용 횟수 설정: ${duplicateThreshold}`);
        saveSettings();
    }

    // 재시도 횟수 업데이트
    function updateRetryAttempts() {
        const value = parseInt(retryAttemptsInput.value);
        retryAttempts = isNaN(value) || value < 1 ? 3 : value;
        log(`🔄 재시도 횟수 설정: ${retryAttempts}`);
        saveSettings();
    }

    // 재시도 간격 업데이트
    function updateRetryInterval() {
        const value = parseInt(retryIntervalInput.value);
        retryInterval = isNaN(value) || value < 100 ? 500 : value;
        log(`🔄 재시도 간격 설정: ${retryInterval}ms`);
        saveSettings();
    }

    // 크롤링 시작 함수
    async function startCrawling() {
        if (isRunning) {
            alert('이미 크롤링이 진행 중입니다.');
            return;
        }
        getSearchQueriesAndEngine();
        if (searchQueries.length === 0) {
            alert('현재 페이지에서 검색어를 찾을 수 없거나 입력된 검색어가 없습니다.');
            log('⚠️ 검색어 미입력 또는 인식 실패.');
            return;
        }
        isRunning = true;
        isPaused = false;
        currentQueryIndex = 0;
        results = [];
        visitedLinks.clear();
        resultsBody.innerHTML = '';
        visitedCountElem.innerText = '0';
        elapsedTimeElem.innerText = '00:00:00';
        remainingTimeElem.innerText = '00:00:00';
        logMessages = [];
        logContainer.innerHTML = `<strong>로그:</strong><br>`;
        GM_setValue('crawlerLogs', JSON.stringify(logMessages));
        log('✅ 크롤링을 시작합니다.');
        updateDuplicateThreshold();
        updateRetryAttempts();
        updateRetryInterval();
        await crawlNextQuery();
        startTime = Date.now();
        elapsedInterval = setInterval(updateElapsedTime, 1000);
    }

    // 다음 쿼리 크롤링 함수
    async function crawlNextQuery() {
        if (currentQueryIndex >= searchQueries.length || !isRunning) {
            finalizeCrawling();
            return;
        }
        searchQuery = searchQueries[currentQueryIndex];
        currentPage = 1;
        totalPages = 0;
        log(`🔍 현재 검색어: "${searchQuery}"`);
        await crawlPage();
    }

    // 일시정지/재개 함수
    function togglePause() {
        if (!isRunning) {
            alert('일시정지/재개할 크롤링이 없습니다.');
            return;
        }
        isPaused = !isPaused;
        if (isPaused) {
            log('⏸️ 크롤링을 일시정지하였습니다.');
            pauseBtn.innerText = '▶️ 재개';
            pauseBtn.title = '크롤링 재개';
        } else {
            log('▶️ 크롤링을 재개하였습니다.');
            pauseBtn.innerText = '⏸️ 일시정지';
            pauseBtn.title = '크롤링 일시정지';
            crawlPage();
        }
    }

    // 크롤링 중지 함수
    function stopCrawling() {
        if (!isRunning) {
            alert('중지할 크롤링이 없습니다.');
            return;
        }
        isRunning = false;
        isPaused = false;
        pauseBtn.innerText = '⏸️ 일시정지';
        pauseBtn.title = '크롤링 일시정지';
        clearInterval(elapsedInterval);
        updateProgress();
        log('🚫 크롤링을 중지하였습니다.');
        GM_notification({
            text: '크롤링이 중지되었습니다.',
            title: '웹 크롤러 8.6',
            timeout: 3000
        });
    }

    // 크롤링 초기화 함수
    function resetCrawling() {
        stopCrawling();
        currentPage = 1;
        totalPages = 0;
        results = [];
        visitedLinks.clear();
        resultsBody.innerHTML = '';
        visitedCountElem.innerText = '0';
        progressBar.value = 0;
        progressText.innerText = '0%';
        elapsedTimeElem.innerText = '00:00:00';
        remainingTimeElem.innerText = '00:00:00';
        logMessages = [];
        logContainer.innerHTML = `<strong>로그:</strong><br>`;
        GM_setValue('crawlerLogs', JSON.stringify(logMessages));
        crawlData = {
            labels: [],
            datasets: [{
                label: '방문 횟수',
                data: [],
                backgroundColor: 'rgba(75, 192, 192, 0.6)',
                borderColor: 'rgba(75, 192, 192, 1)',
                borderWidth: 1
            },
            {
                label: '평균 페이지 로딩 시간 (ms)',
                data: [],
                backgroundColor: 'rgba(153, 102, 255, 0.6)',
                borderColor: 'rgba(153, 102, 255, 1)',
                borderWidth: 1
            }]
        };
        loadTimeData = {
            labels: [],
            datasets: [{
                label: '페이지 로딩 시간 (ms)',
                data: [],
                backgroundColor: 'rgba(255, 99, 132, 0.6)',
                borderColor: 'rgba(255,99,132,1)',
                borderWidth: 1
            }]
        };
        activityData = {
            labels: [],
            datasets: [{
                label: '활동도',
                data: [],
                backgroundColor: [
                    'rgba(54, 162, 235, 0.6)',
                    'rgba(255, 206, 86, 0.6)',
                    'rgba(75, 192, 192, 0.6)',
                    'rgba(153, 102, 255, 0.6)',
                    'rgba(255, 159, 64, 0.6)',
                    'rgba(199, 199, 199, 0.6)',
                    'rgba(83, 102, 255, 0.6)',
                    'rgba(255, 99, 132, 0.6)',
                    'rgba(54, 162, 235, 0.6)',
                    'rgba(255, 206, 86, 0.6)'
                ],
                borderColor: [
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)',
                    'rgba(199, 199, 199, 1)',
                    'rgba(83, 102, 255, 1)',
                    'rgba(255, 99, 132, 1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)'
                ],
                borderWidth: 1
            }]
        };
        if (crawlChart) {
            crawlChart.destroy();
        }
        if (loadTimeChart) {
            loadTimeChart.destroy();
        }
        if (activityChart) {
            activityChart.destroy();
        }
        initializeChart();
        log('🔄 크롤링을 초기화하였습니다.');
    }

    // 크롤링 페이지 함수
    async function crawlPage() {
        if (!isRunning || isPaused) {
            return;
        }

        let url = constructURL(currentPage);
        if (!url) {
            log('❌ 지원하지 않는 검색 엔진입니다.');
            alert('지원하지 않는 검색 엔진입니다.');
            isRunning = false;
            return;
        }

        log(`📄 페이지 ${currentPage} 크롤링 중: ${url}`);

        try {
            const response = await GM_xmlhttpRequestPromise({
                method: "GET",
                url: url,
                headers: {
                    "User-Agent": navigator.userAgent,
                    "Accept": "text/html"
                },
            });

            if (response.status !== 200) {
                log(`❌ 페이지 ${currentPage} 로드 실패: 상태 ${response.status}`);
                handleCrawlError(currentPage, retryAttempts);
                return;
            }

            const parser = new DOMParser();
            const doc = parser.parseFromString(response.responseText, 'text/html');
            let items = extractItems(doc);
            if (currentPage === 1) {
                totalPages = determineTotalPages(doc);
                totalPagesElem.innerText = totalPages;
            }

            log(`📊 페이지 ${currentPage}에서 ${items.length}개 항목 발견.`);

            for (let item of items) {
                await processItem(item);
            }

            updateChart();
            updateLoadTimeChart();
            updateActivityChart();
            updateProgress();
            currentPage++;
            currentPageElem.innerText = currentPage;
            if (currentPage <= totalPages && isRunning && !isPaused) {
                crawlPage();
            } else {
                currentQueryIndex++;
                crawlNextQuery();
            }

        } catch (error) {
            log(`❌ 페이지 ${currentPage} 요청 중 오류 발생: ${error}`);
            handleCrawlError(currentPage, retryAttempts);
        }
    }

    // GM_xmlhttpRequest를 Promise로 래핑
    function GM_xmlhttpRequestPromise(details) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                ...details,
                onload: resolve,
                onerror: reject
            });
        });
    }

    // 크롤링 완료 시 처리 함수
    function finalizeCrawling() {
        isRunning = false;
        clearInterval(elapsedInterval);
        log('✅ 크롤링이 완료되었습니다.');
        GM_notification({
            text: '크롤링이 완료되었습니다.',
            title: '웹 크롤러 8.6',
            timeout: 3000
        });
    }

    // 크롤링 오류 처리 함수
    function handleCrawlError(page, retriesLeft) {
        if (retriesLeft > 0 && isRunning) {
            log(`🔄 페이지 ${page} 재시도 (${retriesLeft}회 남음).`);
            setTimeout(() => {
                crawlPage();
            }, retryInterval);
        } else {
            log(`❌ 페이지 ${page} 로드 실패: 최대 재시도 횟수 초과.`);
            currentPage++;
            currentPageElem.innerText = currentPage;
            if (currentPage <= totalPages && isRunning && !isPaused) {
                crawlPage();
            } else {
                currentQueryIndex++;
                crawlNextQuery();
            }
        }
    }

    // URL 구성 함수
    function constructURL(page) {
        switch(searchEngine) {
            case 'google':
                return `https://www.google.com/search?q=${encodeURIComponent(searchQuery)}&start=${(page-1)*10}`;
            case 'bing':
                return `https://www.bing.com/search?q=${encodeURIComponent(searchQuery)}&first=${(page-1)*10 +1}`;
            case 'daum':
                return `https://search.daum.net/search?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'naver':
                return `https://search.naver.com/search.naver?query=${encodeURIComponent(searchQuery)}&start=${(page-1)*10 +1}`;
            case 'yahoo':
                return `https://search.yahoo.com/search?p=${encodeURIComponent(searchQuery)}&b=${(page-1)*10 +1}`;
            case 'duckduckgo':
                return `https://duckduckgo.com/html/?q=${encodeURIComponent(searchQuery)}&s=${(page-1)*30}`;
            case 'startpage':
                return `https://www.startpage.com/do/search?query=${encodeURIComponent(searchQuery)}&b=${(page-1)*10 +1}`;
            case 'ecosia':
                return `https://www.ecosia.org/search?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'qwant':
                return `https://www.qwant.com/?q=${encodeURIComponent(searchQuery)}&t=web&s=${page}`;
            case 'ahmia':
                return `http://ahmia.fi/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'torch':
                return `http://torchsearch.de/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'notracker':
                return `http://notracker.org/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'onioncity':
                return `http://onion.city/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'darksearch':
                return `http://darksearch.io/search?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'torsearch':
                return `http://torsearch.ml/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'phobos':
                return `http://phobos.onion/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'seamonkey':
                return `http://seamonkey.onion/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'kilos':
                return `http://kilos.onion/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'haystak':
                return `http://haystak5nqiidfld.onion/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'bigsearch':
                return `http://bigsearch3kwz4abe.onion/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'snaptorrents':
                return `http://snaptorrents5nfyh4.onion/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            case 'theoneiroi':
                return `http://theoneiroi3amnhv3.onion/search/?q=${encodeURIComponent(searchQuery)}&page=${page}`;
            default:
                return '';
        }
    }

    // 항목 추출 함수
    function extractItems(doc) {
        let items = [];
        switch(searchEngine) {
            case 'google':
                items = doc.querySelectorAll('div.g');
                break;
            case 'bing':
                items = doc.querySelectorAll('li.b_algo');
                break;
            case 'daum':
                items = doc.querySelectorAll('div.wrap_cont');
                break;
            case 'naver':
                items = doc.querySelectorAll('div.basic1');
                break;
            case 'yahoo':
                items = doc.querySelectorAll('div.compTitle');
                break;
            case 'duckduckgo':
                items = doc.querySelectorAll('div.result');
                break;
            case 'startpage':
                items = doc.querySelectorAll('div.w-gl__result');
                break;
            case 'ecosia':
                items = doc.querySelectorAll('div.result');
                break;
            case 'qwant':
                items = doc.querySelectorAll('div.results div.result');
                break;
            case 'ahmia':
                items = doc.querySelectorAll('div#main div.result');
                break;
            case 'torch':
                items = doc.querySelectorAll('div.result');
                break;
            case 'notracker':
                items = doc.querySelectorAll('div.result');
                break;
            case 'onioncity':
                items = doc.querySelectorAll('div.search-result');
                break;
            case 'darksearch':
                items = doc.querySelectorAll('div.result-item');
                break;
            case 'torsearch':
                items = doc.querySelectorAll('div.search-result');
                break;
            case 'phobos':
                items = doc.querySelectorAll('div.search-result');
                break;
            case 'seamonkey':
                items = doc.querySelectorAll('div.search-result');
                break;
            case 'kilos':
                items = doc.querySelectorAll('div.search-result');
                break;
            case 'haystak':
                items = doc.querySelectorAll('div.result');
                break;
            case 'bigsearch':
                items = doc.querySelectorAll('div.result');
                break;
            case 'snaptorrents':
                items = doc.querySelectorAll('div.result');
                break;
            case 'theoneiroi':
                items = doc.querySelectorAll('div.result');
                break;
            default:
                break;
        }
        return items;
    }

    // 총 페이지 수 결정 함수
    function determineTotalPages(doc) {
        let pages = 1;
        switch(searchEngine) {
            case 'google':
                const googlePager = doc.querySelectorAll('a[href*="start="]');
                pages = Math.min(googlePager.length, 10) || 1;
                break;
            case 'bing':
                const bingPager = doc.querySelectorAll('a.sb_pagS');
                pages = bingPager.length || 1;
                break;
            case 'daum':
                const daumPager = doc.querySelectorAll('a.pagination__link');
                pages = daumPager.length || 1;
                break;
            case 'naver':
                const naverPager = doc.querySelectorAll('a.nav');
                pages = naverPager.length || 1;
                break;
            case 'yahoo':
                const yahooPager = doc.querySelectorAll('a.fz-l.tco-pagination__anchor');
                pages = yahooPager.length || 1;
                break;
            case 'duckduckgo':
                const ddgPager = doc.querySelectorAll('a.result--more__btn');
                pages = ddgPager.length || 1;
                break;
            case 'startpage':
                const spPager = doc.querySelectorAll('a.paginator__link');
                pages = spPager.length || 1;
                break;
            case 'ecosia':
                const ecosiaPager = doc.querySelectorAll('a.pagination__link');
                pages = ecosiaPager.length || 1;
                break;
            case 'qwant':
                const qwantPager = doc.querySelectorAll('a.pagination__link');
                pages = qwantPager.length || 1;
                break;
            case 'ahmia':
                const ahmiaPager = doc.querySelectorAll('a.pagination__link');
                pages = ahmiaPager.length || 1;
                break;
            case 'torch':
                const torchPager = doc.querySelectorAll('a.pagination__link');
                pages = torchPager.length || 1;
                break;
            case 'notracker':
                const ntPager = doc.querySelectorAll('a.pagination__link');
                pages = ntPager.length || 1;
                break;
            case 'onioncity':
                const onionPager = doc.querySelectorAll('ul.pagination li a');
                pages = onionPager.length || 1;
                break;
            case 'darksearch':
                const darkPager = doc.querySelectorAll('a.page-link');
                pages = darkPager.length || 1;
                break;
            case 'torsearch':
                const torsearchPager = doc.querySelectorAll('div.pagination a');
                pages = torsearchPager.length || 1;
                break;
            case 'phobos':
                const phobosPager = doc.querySelectorAll('a.pagination__link');
                pages = phobosPager.length || 1;
                break;
            case 'seamonkey':
                const seamonkeyPager = doc.querySelectorAll('a.pagination__link');
                pages = seamonkeyPager.length || 1;
                break;
            case 'kilos':
                const kilosPager = doc.querySelectorAll('a.pagination__link');
                pages = kilosPager.length || 1;
                break;
            case 'haystak':
                const haystakPager = doc.querySelectorAll('a.pagination__link');
                pages = haystakPager.length || 1;
                break;
            case 'bigsearch':
                const bigsearchPager = doc.querySelectorAll('a.pagination__link');
                pages = bigsearchPager.length || 1;
                break;
            case 'snaptorrents':
                const snaptorrentsPager = doc.querySelectorAll('a.pagination__link');
                pages = snaptorrentsPager.length || 1;
                break;
            case 'theoneiroi':
                const theoneiroiPager = doc.querySelectorAll('a.pagination__link');
                pages = theoneiroiPager.length || 1;
                break;
            default:
                pages = 1;
                break;
        }
        return pages;
    }

    // 항목 처리 함수
    async function processItem(item) {
        let title = 'N/A';
        let link = 'N/A';
        let description = 'N/A';
        let image = 'N/A';
        let htmlLink = 'N/A';
        let whoisInfo = 'N/A';
        let pageLoadTime = 'N/A';

        switch(searchEngine) {
            case 'google':
                const gTitleElem = item.querySelector('h3');
                const gLinkElem = item.querySelector('a');
                const gDescElem = item.querySelector('.IsZvec') || item.querySelector('.VwiC3b');
                if (gTitleElem) title = gTitleElem.innerText;
                if (gLinkElem) link = gLinkElem.href;
                if (gDescElem) description = gDescElem.innerText;

                const gImgElem = item.querySelector('img');
                if (gImgElem && gImgElem.src) {
                    image = `<img src="${gImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'bing':
                const bTitleElem = item.querySelector('h2 a');
                const bDescElem = item.querySelector('p');
                if (bTitleElem) {
                    title = bTitleElem.innerText;
                    link = bTitleElem.href;
                }
                if (bDescElem) description = bDescElem.innerText;

                const bImgElem = item.querySelector('img');
                if (bImgElem && bImgElem.src) {
                    image = `<img src="${bImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'daum':
                const dTitleElem = item.querySelector('a.link_fax');
                const dDescElem = item.querySelector('div.cont_inner');
                if (dTitleElem) {
                    title = dTitleElem.innerText.trim();
                    link = dTitleElem.href;
                }
                if (dDescElem) description = dDescElem.innerText.trim();

                const dImgElem = item.querySelector('img');
                if (dImgElem && dImgElem.src) {
                    image = `<img src="${dImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'naver':
                const nTitleElem = item.querySelector('a.tit');
                const nDescElem = item.querySelector('div.desc');
                if (nTitleElem) {
                    title = nTitleElem.innerText;
                    link = nTitleElem.href;
                }
                if (nDescElem) description = nDescElem.innerText;

                const nImgElem = item.querySelector('img.thumb');
                if (nImgElem && nImgElem.src) {
                    image = `<img src="${nImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'yahoo':
                const yTitleElem = item.querySelector('h3');
                const yLinkElem = item.querySelector('a');
                const yDescElem = item.querySelector('div.compText');
                if (yTitleElem) title = yTitleElem.innerText;
                if (yLinkElem) link = yLinkElem.href;
                if (yDescElem) description = yDescElem.innerText;

                const yImgElem = item.querySelector('img');
                if (yImgElem && yImgElem.src) {
                    image = `<img src="${yImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'duckduckgo':
                const ddgTitleElem = item.querySelector('a.result__a');
                const ddgDescElem = item.querySelector('a.result__snippet');
                if (ddgTitleElem) {
                    title = ddgTitleElem.innerText;
                    link = ddgTitleElem.href;
                }
                if (ddgDescElem) description = ddgDescElem.innerText;

                const ddgImgElem = item.querySelector('img.result__image');
                if (ddgImgElem && ddgImgElem.src) {
                    image = `<img src="${ddgImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'startpage':
                const spTitleElem = item.querySelector('a.w-gl__result-title');
                const spDescElem = item.querySelector('p.w-gl__result-description');
                if (spTitleElem) {
                    title = spTitleElem.innerText;
                    link = spTitleElem.href;
                }
                if (spDescElem) description = spDescElem.innerText;

                const spImgElem = item.querySelector('img.w-gl__result-image');
                if (spImgElem && spImgElem.src) {
                    image = `<img src="${spImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'ecosia':
                const ecosiaTitleElem = item.querySelector('h2.result-title a');
                const ecosiaDescElem = item.querySelector('p.result-description');
                if (ecosiaTitleElem) {
                    title = ecosiaTitleElem.innerText;
                    link = ecosiaTitleElem.href;
                }
                if (ecosiaDescElem) description = ecosiaDescElem.innerText;

                const ecosiaImgElem = item.querySelector('img.result-image');
                if (ecosiaImgElem && ecosiaImgElem.src) {
                    image = `<img src="${ecosiaImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'qwant':
                const qwantTitleElem = item.querySelector('a.result__title');
                const qwantDescElem = item.querySelector('p.result__description');
                if (qwantTitleElem) {
                    title = qwantTitleElem.innerText;
                    link = qwantTitleElem.href;
                }
                if (qwantDescElem) description = qwantDescElem.innerText;

                const qwantImgElem = item.querySelector('img.result__image');
                if (qwantImgElem && qwantImgElem.src) {
                    image = `<img src="${qwantImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'ahmia':
                const ahmiaTitleElem = item.querySelector('h2.result-title a');
                const ahmiaDescElem = item.querySelector('p.result-description');
                if (ahmiaTitleElem) {
                    title = ahmiaTitleElem.innerText;
                    link = ahmiaTitleElem.href;
                }
                if (ahmiaDescElem) description = ahmiaDescElem.innerText;

                const ahmiaImgElem = item.querySelector('img.result-image');
                if (ahmiaImgElem && ahmiaImgElem.src) {
                    image = `<img src="${ahmiaImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'torch':
                const torchTitleElem = item.querySelector('h3.result-title a');
                const torchDescElem = item.querySelector('p.result-description');
                if (torchTitleElem) {
                    title = torchTitleElem.innerText;
                    link = torchTitleElem.href;
                }
                if (torchDescElem) description = torchDescElem.innerText;

                const torchImgElem = item.querySelector('img.result-image');
                if (torchImgElem && torchImgElem.src) {
                    image = `<img src="${torchImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'notracker':
                const ntTitleElem = item.querySelector('h3.result-title a');
                const ntDescElem = item.querySelector('p.result-description');
                if (ntTitleElem) {
                    title = ntTitleElem.innerText;
                    link = ntTitleElem.href;
                }
                if (ntDescElem) description = ntDescElem.innerText;

                const ntImgElem = item.querySelector('img.result-image');
                if (ntImgElem && ntImgElem.src) {
                    image = `<img src="${ntImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'onioncity':
                const onionTitleElem = item.querySelector('h3.search-result-title a');
                const onionDescElem = item.querySelector('p.search-result-description');
                if (onionTitleElem) {
                    title = onionTitleElem.innerText;
                    link = onionTitleElem.href;
                }
                if (onionDescElem) description = onionDescElem.innerText;

                const onionImgElem = item.querySelector('img');
                if (onionImgElem && onionImgElem.src) {
                    image = `<img src="${onionImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'darksearch':
                const darkTitleElem = item.querySelector('h2.result-title a');
                const darkDescElem = item.querySelector('p.result-description');
                if (darkTitleElem) {
                    title = darkTitleElem.innerText;
                    link = darkTitleElem.href;
                }
                if (darkDescElem) description = darkDescElem.innerText;

                const darkImgElem = item.querySelector('img.result-image');
                if (darkImgElem && darkImgElem.src) {
                    image = `<img src="${darkImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'torsearch':
                const torsearchTitleElem = item.querySelector('h3.search-result-title a');
                const torsearchDescElem = item.querySelector('p.search-result-description');
                if (torsearchTitleElem) {
                    title = torsearchTitleElem.innerText;
                    link = torsearchTitleElem.href;
                }
                if (torsearchDescElem) description = torsearchDescElem.innerText;

                const torsearchImgElem = item.querySelector('img');
                if (torsearchImgElem && torsearchImgElem.src) {
                    image = `<img src="${torsearchImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'phobos':
                const phobosTitleElem = item.querySelector('h3.result-title a');
                const phobosDescElem = item.querySelector('p.result-description');
                if (phobosTitleElem) {
                    title = phobosTitleElem.innerText;
                    link = phobosTitleElem.href;
                }
                if (phobosDescElem) description = phobosDescElem.innerText;

                const phobosImgElem = item.querySelector('img.result-image');
                if (phobosImgElem && phobosImgElem.src) {
                    image = `<img src="${phobosImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'seamonkey':
                const seamonkeyTitleElem = item.querySelector('h3.result-title a');
                const seamonkeyDescElem = item.querySelector('p.result-description');
                if (seamonkeyTitleElem) {
                    title = seamonkeyTitleElem.innerText;
                    link = seamonkeyTitleElem.href;
                }
                if (seamonkeyDescElem) description = seamonkeyDescElem.innerText;

                const seamonkeyImgElem = item.querySelector('img.result-image');
                if (seamonkeyImgElem && seamonkeyImgElem.src) {
                    image = `<img src="${seamonkeyImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'kilos':
                const kilosTitleElem = item.querySelector('h3.result-title a');
                const kilosDescElem = item.querySelector('p.result-description');
                if (kilosTitleElem) {
                    title = kilosTitleElem.innerText;
                    link = kilosTitleElem.href;
                }
                if (kilosDescElem) description = kilosDescElem.innerText;

                const kilosImgElem = item.querySelector('img.result-image');
                if (kilosImgElem && kilosImgElem.src) {
                    image = `<img src="${kilosImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'haystak':
                const haystakTitleElem = item.querySelector('h2.result-title a');
                const haystakDescElem = item.querySelector('p.result-description');
                if (haystakTitleElem) {
                    title = haystakTitleElem.innerText;
                    link = haystakTitleElem.href;
                }
                if (haystakDescElem) description = haystakDescElem.innerText;

                const haystakImgElem = item.querySelector('img.result-image');
                if (haystakImgElem && haystakImgElem.src) {
                    image = `<img src="${haystakImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'bigsearch':
                const bigsearchTitleElem = item.querySelector('h2.result-title a');
                const bigsearchDescElem = item.querySelector('p.result-description');
                if (bigsearchTitleElem) {
                    title = bigsearchTitleElem.innerText;
                    link = bigsearchTitleElem.href;
                }
                if (bigsearchDescElem) description = bigsearchDescElem.innerText;

                const bigsearchImgElem = item.querySelector('img.result-image');
                if (bigsearchImgElem && bigsearchImgElem.src) {
                    image = `<img src="${bigsearchImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'snaptorrents':
                const snaptorrentsTitleElem = item.querySelector('h2.result-title a');
                const snaptorrentsDescElem = item.querySelector('p.result-description');
                if (snaptorrentsTitleElem) {
                    title = snaptorrentsTitleElem.innerText;
                    link = snaptorrentsTitleElem.href;
                }
                if (snaptorrentsDescElem) description = snaptorrentsDescElem.innerText;

                const snaptorrentsImgElem = item.querySelector('img.result-image');
                if (snaptorrentsImgElem && snaptorrentsImgElem.src) {
                    image = `<img src="${snaptorrentsImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            case 'theoneiroi':
                const theoneiroiTitleElem = item.querySelector('h2.result-title a');
                const theoneiroiDescElem = item.querySelector('p.result-description');
                if (theoneiroiTitleElem) {
                    title = theoneiroiTitleElem.innerText;
                    link = theoneiroiTitleElem.href;
                }
                if (theoneiroiDescElem) description = theoneiroiDescElem.innerText;

                const theoneiroiImgElem = item.querySelector('img.result-image');
                if (theoneiroiImgElem && theoneiroiImgElem.src) {
                    image = `<img src="${theoneiroiImgElem.src}" alt="이미지" style="max-width: 80px; max-height: 80px;">`;
                }

                if (toggleHTML.checked && link !== 'N/A') {
                    whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(extractDomain(link))}" target="_blank">보기</a>`;
                    htmlLink = `<a href="https://codebeautify.org/htmlviewer/?url=${encodeURIComponent(link)}" target="_blank">보기</a>`;
                }
                break;
            default:
                break;
        }

        // 광고 제외 필터링
        if (toggleAds.checked) {
            const combinedText = `${title} ${description}`.toLowerCase();
            const isAd = combinedText.includes('advertisement') || combinedText.includes('광고') || combinedText.includes('ad ');
            if (isAd) {
                log(`🚫 광고 항목 제외: "${title}"`);
                return;
            }
        }

        // 필터 패턴 적용
        if (filterPatterns.length > 0) {
            const combinedText = `${title} ${description}`.toLowerCase();
            const isFiltered = filterPatterns.some(regex => regex.test(combinedText));
            if (isFiltered) {
                log(`❌ 필터 단어 포함 항목 제외: "${title}"`);
                return;
            }
        }

        // 도메인 추출 및 제외 도메인 적용
        const domain = extractDomain(link);
        if (!domain) return;

        if (excludeDomains.includes(domain.toLowerCase())) {
            log(`🚫 제외 도메인 항목 제외: "${link}"`);
            return;
        }

        // 중복 방문 제한
        let visitCount = visitedLinks.get(domain) || 0;
        if (visitCount >= duplicateThreshold) {
            log(`🔄 중복 방문 제한: "${domain}"`);
            return;
        }

        visitedLinks.set(domain, visitCount + 1);
        visitedCountElem.innerText = visitedLinks.size.toString();

        // 페이지 로딩 시간 측정
        try {
            const startLoad = performance.now();
            await fetch(link, { mode: 'no-cors' });
            const endLoad = performance.now();
            pageLoadTime = Math.round(endLoad - startLoad);
        } catch (error) {
            pageLoadTime = '계산 불가';
        }

        // WHOIS 정보 링크 생성
        if (toggleHTML.checked && link !== 'N/A') {
            whoisInfo = `<a href="https://jsonwhois.com/whois/${encodeURIComponent(domain)}" target="_blank">보기</a>`;
        }

        // 이미지 옵션 적용
        if (!toggleImages.checked) {
            image = '비활성화';
        }

        // 결과 배열에 추가
        results.push({ title, link, description, image, htmlLink, whoisInfo, pageLoadTime });

        // 결과 테이블에 행 추가
        const row = document.createElement('tr');
        row.innerHTML = `
            <td>${escapeHTML(title)}</td>
            <td><a href="${escapeHTML(link)}" target="_blank">${escapeHTML(link)}</a></td>
            <td>${escapeHTML(description)}</td>
            <td>${image}</td>
            <td>${htmlLink}</td>
            <td>${whoisInfo}</td>
            <td>${pageLoadTime}</td>
        `;
        resultsBody.appendChild(row);

        // 업데이트된 로딩 시간을 테이블에 반영
        row.cells[6].innerText = pageLoadTime;
        updateChart();
        updateLoadTimeChart();
        updateActivityChart();
        updateProgress();
    }

    // HTML 이스케프 함수
    function escapeHTML(str) {
        if (!str) return '';
        return str.replace(/&/g, "&amp;")
                  .replace(/</g, "&lt;")
                  .replace(/>/g, "&gt;")
                  .replace(/"/g, "&quot;")
                  .replace(/'/g, "&#039;");
    }

    // 도메인 추출 함수
    function extractDomain(url) {
        try {
            const urlObj = new URL(url);
            return urlObj.hostname.replace('www.', '');
        } catch (e) {
            return null;
        }
    }

    // 경과 시간 업데이트 함수
    function updateElapsedTime() {
        if (!startTime) return;
        const now = Date.now();
        const elapsed = Math.floor((now - startTime) / 1000);
        elapsedTimeElem.innerText = formatTime(elapsed);
        const estimatedTotal = totalPages * ((elapsed > 0) ? (elapsed / (currentPage - 1)) : 0.5);
        const remaining = Math.max(Math.round(estimatedTotal - elapsed), 0);
        remainingTimeElem.innerText = formatTime(remaining);
    }

    // 시간 형식화 함수
    function formatTime(seconds) {
        const hrs = String(Math.floor(seconds / 3600)).padStart(2, '0');
        const mins = String(Math.floor((seconds % 3600) / 60)).padStart(2, '0');
        const secs = String(seconds % 60).padStart(2, '0');
        return `${hrs}:${mins}:${secs}`;
    }

    // 차트 업데이트 함수
    function updateChart() {
        crawlData.labels = Array.from(visitedLinks.keys());
        crawlData.datasets[0].data = Array.from(visitedLinks.values());

        // 평균 페이지 로딩 시간 계산
        crawlData.datasets[1].data = Array.from(visitedLinks.keys()).map(domain => {
            const times = results.filter(r => extractDomain(r.link) === domain && typeof r.pageLoadTime === 'number').map(r => r.pageLoadTime);
            return times.length > 0 ? Math.round(times.reduce((a, b) => a + b, 0) / times.length) : 0;
        });

        if (crawlChart) {
            crawlChart.update();
        }
        GM_setValue('crawlerData', JSON.stringify(crawlData));
    }

    // 추가 차트 업데이트 함수
    function updateLoadTimeChart() {
        loadTimeData.labels = results.map(r => r.title).slice(-20); // 최근 20개
        loadTimeData.datasets[0].data = results.map(r => r.pageLoadTime).slice(-20);
        if (loadTimeChart) {
            loadTimeChart.update();
        }
    }

    // 활동도 차트 업데이트 함수
    function updateActivityChart() {
        const activityCounts = {};
        results.forEach(r => {
            const domain = extractDomain(r.link);
            if (domain) {
                activityCounts[domain] = (activityCounts[domain] || 0) + 1;
            }
        });
        activityData.labels = Object.keys(activityCounts).slice(-10); // 상위 10개 도메인
        activityData.datasets[0].data = Object.values(activityCounts).slice(-10);
        if (activityChart) {
            activityChart.update();
        }
    }

    // 크롤 데이터 로드 함수
    function loadCrawlData() {
        const storedData = GM_getValue('crawlerData', null);
        if (storedData) {
            const parsedData = JSON.parse(storedData);
            crawlData = parsedData;
            if (crawlChart) {
                crawlChart.data = crawlData;
                crawlChart.update();
            }
        }
    }

    // 크롤 데이터 저장 함수
    function saveCrawlData() {
        GM_setValue('crawlerData', JSON.stringify(crawlData));
    }

    // 주기적으로 크롤 데이터 저장
    setInterval(saveCrawlData, 5000);

    // 검색어 및 검색 엔진 감지 함수
    function getSearchQueriesAndEngine() {
        const url = window.location.href;
        searchQueries = [];
        if (url.includes('google.com/search')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'google';
        } else if (url.includes('bing.com/search')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'bing';
        } else if (url.includes('daum.net/search')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'daum';
        } else if (url.includes('search.naver.com/search')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('query') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'naver';
        } else if (url.includes('search.yahoo.com/search')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('p') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'yahoo';
        } else if (url.includes('duckduckgo.com/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'duckduckgo';
        } else if (url.includes('startpage.com/do/search')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('query') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'startpage';
        } else if (url.includes('ecosia.org/search')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'ecosia';
        } else if (url.includes('qwant.com/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'qwant';
        } else if (url.includes('ahmia.fi/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'ahmia';
        } else if (url.includes('torchsearch.de/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'torch';
        } else if (url.includes('notracker.org/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'notracker';
        } else if (url.includes('onion.city/search/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'onioncity';
        } else if (url.includes('darksearch.io/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'darksearch';
        } else if (url.includes('torsearch.ml/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'torsearch';
        } else if (url.includes('phobos.onion/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'phobos';
        } else if (url.includes('seamonkey.onion/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'seamonkey';
        } else if (url.includes('kilos.onion/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'kilos';
        } else if (url.includes('haystak5nqiidfld.onion/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'haystak';
        } else if (url.includes('bigsearch3kwz4abe.onion/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'bigsearch';
        } else if (url.includes('snaptorrents5nfyh4.onion/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'snaptorrents';
        } else if (url.includes('theoneiroi3amnhv3.onion/')) {
            const params = new URLSearchParams(window.location.search);
            const q = params.get('q') || '';
            searchQueries = q.split(';').map(s => s.trim()).filter(s => s.length > 0);
            searchEngine = 'theoneiroi';
        } else {
            searchQueries = [];
            searchEngine = '';
        }
        log(`🔍 검색 엔진: ${capitalizeFirstLetter(searchEngine)}, 검색어들: "${searchQueries.join(', ')}"`);
    }

    // 로그 기록 함수
    function log(message) {
        const time = new Date().toLocaleTimeString();
        const entry = `[${time}] ${message}`;
        logMessages.push(entry);
        logContainer.innerHTML += `${entry}<br>`;
        logContainer.scrollTop = logContainer.scrollHeight;
        GM_setValue('crawlerLogs', JSON.stringify(logMessages));
    }

    // 결과 내보내기 함수 (중복 제거 및 정렬 추가)
    function exportResults() {
        if (results.length === 0) {
            alert('내보낼 결과가 없습니다.');
            return;
        }
        const format = prompt('내보낼 형식을 선택하세요 (xlsx, csv, json):', 'xlsx');
        if (!format) return;
        if (format.toLowerCase() === 'xlsx') {
            const uniqueResults = removeDuplicateResults(results);
            const ws = XLSX.utils.json_to_sheet(uniqueResults);
            const wb = XLSX.utils.book_new();
            XLSX.utils.book_append_sheet(wb, ws, "결과");
            XLSX.writeFile(wb, "크롤링_결과.xlsx");
            log('💾 결과를 "크롤링_결과.xlsx" 파일로 내보냈습니다.');
            GM_notification({
                text: '결과를 "크롤링_결과.xlsx" 파일로 내보냈습니다.',
                title: '웹 크롤러 8.6',
                timeout: 3000
            });
        } else if (format.toLowerCase() === 'csv') {
            const uniqueResults = removeDuplicateResults(results);
            const csv = uniqueResults.map(row => `"${row.title.replace(/"/g, '""')}","${row.link.replace(/"/g, '""')}","${row.description.replace(/"/g, '""')}","${row.image.replace(/"/g, '""')}","${row.htmlLink}","${row.whoisInfo}","${row.pageLoadTime}"`).join('\n');
            const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = '크롤링_결과.csv';
            a.click();
            log('💾 결과를 "크롤링_결과.csv" 파일로 내보냈습니다.');
            GM_notification({
                text: '결과를 "크롤링_결과.csv" 파일로 내보냈습니다.',
                title: '웹 크롤러 8.6',
                timeout: 3000
            });
        } else if (format.toLowerCase() === 'json') {
            const uniqueResults = removeDuplicateResults(results);
            const json = JSON.stringify(uniqueResults, null, 2);
            const blob = new Blob([json], { type: 'application/json;charset=utf-8;' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = '크롤링_결과.json';
            a.click();
            log('💾 결과를 "크롤링_결과.json" 파일로 내보냈습니다.');
            GM_notification({
                text: '결과를 "크롤링_결과.json" 파일로 내보냈습니다.',
                title: '웹 크롤러 8.6',
                timeout: 3000
            });
        } else {
            alert('지원하지 않는 형식입니다.');
        }
    }

    // 중복 결과 제거 함수
    function removeDuplicateResults(resultsArray) {
        const seen = new Set();
        return resultsArray.filter(item => {
            const identifier = item.link;
            if (seen.has(identifier)) {
                return false;
            } else {
                seen.add(identifier);
                return true;
            }
        }).sort((a, b) => a.title.localeCompare(b.title));
    }

    // 결과 테이블 정렬 함수
    function sortTable(n) {
        const table = document.getElementById("resultsTable");
        let switching = true;
        let dir = "asc";
        let switchcount = 0;
        while (switching) {
            switching = false;
            const rows = table.rows;
            for (let i = 1; i < (rows.length - 1); i++) {
                let shouldSwitch = false;
                const x = rows[i].getElementsByTagName("TD")[n];
                const y = rows[i + 1].getElementsByTagName("TD")[n];
                let xContent = x.textContent || x.innerText;
                let yContent = y.textContent || y.innerText;

                // 숫자 비교
                if (n === 6) {
                    xContent = parseInt(xContent);
                    yContent = parseInt(yContent);
                }

                if (dir === "asc") {
                    if (xContent > yContent) {
                        shouldSwitch = true;
                        break;
                    }
                } else if (dir === "desc") {
                    if (xContent < yContent) {
                        shouldSwitch = true;
                        break;
                    }
                }
            }
            if (shouldSwitch) {
                rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
                switching = true;
                switchcount++;
            } else {
                if (switchcount === 0 && dir === "asc") {
                    dir = "desc";
                    switching = true;
                }
            }
        }
    }

    // 사용자 경험 향상을 위한 함수 추가
    function capitalizeFirstLetter(string) {
        if (!string) return '';
        return string.charAt(0).toUpperCase() + string.slice(1);
    }

    // 항목 검색 기능
    function filterResults() {
        const filter = searchResultsInput.value.toLowerCase();
        const rows = resultsBody.getElementsByTagName('tr');
        for (let i = 0; i < rows.length; i++) {
            const cells = rows[i].getElementsByTagName('td');
            let match = false;
            for (let j = 0; j < cells.length - 3; j++) { // 이미지, HTML 링크, WHOIS 제외
                if (cells[j].innerText.toLowerCase().includes(filter)) {
                    match = true;
                    break;
                }
            }
            rows[i].style.display = match ? '' : 'none';
        }
    }

    // 로그 초기화 함수
    function clearLogs() {
        if(confirm('로그를 초기화하시겠습니까?')) {
            logMessages = [];
            logContainer.innerHTML = `<strong>로그:</strong><br>`;
            GM_setValue('crawlerLogs', JSON.stringify(logMessages));
            log('🧹 로그를 초기화하였습니다.');
        }
    }

    // 진행률 업데이트 함수
    function updateProgress() {
        if (totalPages === 0) {
            progressBar.value = 0;
            progressText.innerText = '0%';
            return;
        }
        const progress = Math.min((currentPage - 1) / totalPages * 100, 100);
        progressBar.value = progress;
        progressText.innerText = `${Math.round(progress)}%`;
    }

    // 크롤 데이터를 로드하는 함수는 이미 정의됨

    // 크롤 데이터를 저장하는 함수는 이미 정의됨

    // 검색 다음 쿼리를 크롤링하는 함수는 이미 정의됨

    // 크롤링 시작 함수는 이미 정의됨

    // 'Go to Selected Engine' 버튼 클릭 시 동작 함수
    function goToSelectedEngine() {
        const selectedEngine = searchEngineSelect.value;
        const url = searchEngineURLs[selectedEngine];
        if (url) {
            window.open(url, '_blank');
            log(`🔗 선택한 검색 엔진(${capitalizeFirstLetter(selectedEngine)})으로 이동합니다.`);
        } else {
            alert('선택한 검색 엔진의 URL을 찾을 수 없습니다.');
            log('⚠️ 선택한 검색 엔진의 URL을 찾을 수 없습니다.');
        }
    }
})();모듈화나 코드 최적화 코드 삭제 이런 거는 절대 하지 마셈
 스크립트 시스템 대폭 대폭 개선
 스크립트 기능들도 초 격차로 대폭 대폭 개선
 기능들을 사용자의 편의성과 가독성을 위해 대폭 개선

(크롤링 속도 조절 기능은 없애샘 만약 있으면 없애셈

설정 ㅈㄴ ㅈㄴ ㅈㄴ ㅈㄴ 다양하게 하셈 코드 상으로만 구현되어 있는 기능도 포함 

 + 코드 가독성 향상 (주석 다는 거) 이거 진짜 하지 마셈 

창 좀 작게 하셈

 한 단계 올릴 수준으로 기능들을 대폭 개선하셈 스크립트의 수준을 한 단계 올릴 만큼


그리고 내가 지금 말하는 거에 집중해 전에 말한 거 에 집중하지 말고 알겠지? 전에 한 거는 신경쓰지 마셈

그리고 "기존에 있던 기능들" 성능들 편의성이나 가도성 대폭 대폭 대폭 대폭 초 대폭 초초초초초초초초초초초초 대폭 개선하셈

ui 대폭 대폭 편의성과 가독성을 전부다 가져온 ui 이여야 함

시작 자체가 안 되면 안 됨. 시작 자체는 되야 함 + 이 스크립트의 크롤링의 속도를 제한 시킬 수 있는 게 있다면 없애! 지금 당장! + 무조건 속도는 100ms으로 통일 한다. + ui 대폭 개선

토르 검색 엔진도 되게 검색 엔진 ㅈㄴ 다양하게 하셈