// 데이터 초기화 버튼 이벤트 추가
$('#reset-data').on('click', resetData);
// 크롤링 초기화 버튼 추가
$('#crawler-content #tab-settings').append(`<button id="initialize-crawling" title="크롤링을 초기화합니다." style="background-color: #2196F3; color: white; margin-top: 10px;">🔄 크롤링 초기화</button>`);
// 크롤링 초기화 버튼 이벤트 추가
$('#initialize-crawling').on('click', initializeCrawling);
// 크롤링 초기화 함수 추가
function initializeCrawling() {
if (confirm("크롤링을 초기화하시겠습니까? 모든 진행 중인 크롤링이 중지되고 상태가 초기화됩니다.")) {
isCrawling = false;
GM_setValue('isCrawling', isCrawling);
currentPage = 1;
GM_setValue('currentPage', currentPage);
totalPages = settings.maxPages;
GM_setValue('totalPages', totalPages);
currentDepth = 0;
GM_setValue('currentDepth', currentDepth);
progress = 0;
GM_setValue('progress', progress);
visitedUrls = [];
GM_setValue('visitedUrls', visitedUrls);
requestQueue = [];
activeRequests = 0;
data = [];
GM_setValue('data', data);
logs = [];
GM_setValue('logs', logs);
updateLogsUI();
updateDataUI();
setButtonStates();
log('크롤링이 초기화되었습니다.', 'success');
}
}
(function() {
'use strict';
// 기본 설정
const defaultSettings = {
// 기존 설정 유지
/* ... 기존 defaultSettings 코드 ... */
};
// 현재 설정 로드
const settings = {
// 기존 설정 로드 유지
/* ... 기존 settings 로드 코드 ... */
};
// 크롤링 상태 복원
let visitedUrls = GM_getValue('visitedUrls', []);
let logs = GM_getValue('logs', []);
let data = GM_getValue('data', []);
let isCrawling = GM_getValue('isCrawling', false);
let currentPage = GM_getValue('currentPage', 1);
let totalPages = GM_getValue('totalPages', settings.maxPages);
let progress = GM_getValue('progress', 0);
let currentDepth = GM_getValue('currentDepth', 0);
let activeRequests = 0;
let requestQueue = [];
// UI 생성
function createUI() {
const style = /* 기존 스타일 유지 / / ... 기존 스타일 코드 ... */ ;
(′head′).append(<style>{style});
const container = (‘<divid="crawler−container"><divid="crawler−tabs"><divclass="crawler−tab{currentTab() === 'logs' ? 'active' : ''}" data-tab="logs" title="로그 보기">📜 로그
📊 데이터
⚙️ 설정
진행률: progress.toFixed(2){currentPage}/totalPages페이지)<{isCrawling ? 'disabled' : ''}>▶ 시작
<button id="stop-crawling" title="크롤링을 중지합니다." isCrawling?′′:′d{settings.exportFormats.csv ? <button id="download-csv" title="수집된 데이터를 CSV 파일로 다운로드합니다.">📥 CSV</button> : ''}
settings.exp{settings.exportFormats.xml ? <button id="download-xml" title="수집된 데이터를 XML 파일로 다운로드합니다.">📥 XML</button> : ''}
{settings.exportFormats.xls ? `<button id="download-xls" title="수집된 데이터를 XLS 파일로 다운로드합니다.">📥 XLS</button>` : ''}
<!-- 데이터 초기화 버튼 추가 -->
<button id="reset-data" title="수집된 데이터와 로그를 초기화합니다." style="background-color: #f44336; color: white;">🗑️ 데이터 초기화</button>
</div>
<div id="log-messages" style="height: 250px; overflow-y: auto; background: #f9f9f9; padding: 10px; border: 1px solid #ccc; border-radius: 4px;">로그 내용이 여기에 표시됩니다.</div>
</div>
<div id="tab-data" style="display: {currentTab() === 'data' ? 'block' : 'none'};">
// 탭 전환 이벤트
('.crawler-tab').on('click', function() {
const tab = (this).data('tab');
(′.crawler−tab′).removeClass(′active′);(this).addClass('active');
('#crawler-content > div').hide();
(#tab-${tab}).show();
saveCurrentTab(tab);
});
// 단축키 이벤트
(document).on('keydown', function(e) {
if (e.ctrlKey) {
switch (e.key) {
case '1':
('.crawler-tab[data-tab="logs"]').click();
break;
case '2':
(′.crawler−tab[data−tab="data"]′).click();break;case′3′:('.crawler-tab[data-tab="settings"]').click();
break;
case 's':
e.preventDefault();
saveSettings();
break;
case 'p':
e.preventDefault();
if (!isCrawling) startCrawling();
break;
case 'q':
e.preventDefault();
if (isCrawling) stopCrawling();
break;
case 'd':
e.preventDefault();
downloadCSV();
break;
case 'j':
e.preventDefault();
downloadJSON();
break;
case 'x':
e.preventDefault();
downloadXML();
break;
case 'l':
e.preventDefault();
downloadXLS();
break;
}
}
});
// 설정 저장 버튼 이벤트
('#save-settings').on('click', saveSettings);
('#reset-settings').on('click', resetSettings);
// 추가/제거 사용자 에이전트 버튼 이벤트
$('#add-user-agent').on('click', function() {
const newUA = prompt("새 사용자 에이전트를 입력하세요:");
if (newUA) {
settings.userAgents.push(newUA);
GM_setValue('userAgents', settings.userAgents);
updateUserAgentSelect();
log('새 사용자 에이전트가 추가되었습니다.', 'success');
}
});
// 사용자 지정 키워드 텍스트 생성 함수
function getCustomKeywordsText() {
const customKeywords = settings.customKeywords || {};
return Object.entries(customKeywords).map(([category, kws]) => category:{kws.join(',')}).join('\n');
}
// 사용자 지정 키워드 저장 및 로드 함수
function parseCustomKeywords(text) {
const lines = text.split('\n').map(line => line.trim()).filter(line => line);
const customKeywords = {};
lines.forEach(line => {
const [category, kws] = line.split(':');
if (category && kws) {
customKeywords[category.trim()] = kws.split(',').map(kw => kw.trim()).filter(kw => kw);
}
});
return customKeywords;
}
for (const [category, kws] of Object.entries(customKeywords)) {
if (standardKeywords[category]) {
standardKeywords[category] = [...new Set([...standardKeywords[category], ...kws])];
} else {
standardKeywords[category] = kws;
}
}
return standardKeywords;
}
// 로깅 함수
function log(message, type = 'info') {
const timestamp = new Date().toLocaleString();
logs.push({ timestamp, message, type });
GM_setValue('logs', logs);
updateLogsUI();
if (settings.advancedLogging) {
// type을 콘솔 메서드로 매핑
switch(type) {
case 'info':
console.info([${timestamp}] [INFO] ${message});
break;
case 'warning':
console.warn([${timestamp}] [WARNING] ${message});
break;
case 'error':
console.error([${timestamp}] [ERROR] ${message});
break;
case 'success':
console.log(%c[${timestamp}] [SUCCESS] ${message}, 'color: green; font-weight: bold;');
break;
default:
console.log([${timestamp}] [${type.toUpperCase()}] ${message});
}
}
}
// 색상 생성 함수
function generateColors(count) {
const colors = [];
for(let i=0; i<count; i++) {
const hue = i * (360 / count);
colors.push(hsl(${hue}, 70%, 50%));
}
return colors;
}
// HTML 이스케이프 함수
function escapeHTML(str) {
if (!str) return '';
return str.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// CSV, JSON, XML, XLS 다운로드 함수
function downloadCSV() {
if (data.length === 0) {
log('다운로드할 데이터가 없습니다.', 'warning');
return;
}
const headers = [];
if (settings.dataFields.name) headers.push("사이트 이름");
if (settings.dataFields.url) headers.push("주소");
if (settings.dataFields.description) headers.push("설명");
if (settings.dataFields.html) headers.push("📄 HTML 내용");
if (settings.dataFields.category) headers.push("분류");
if (settings.dataFields.quality) headers.push("품질점수");
if (settings.dataFields.language) headers.push("언어");
if (settings.dataFields.wordCount) headers.push("단어 수");
if (settings.dataFields.metaTags) headers.push("메타 태그");
if (settings.dataFields.headings) headers.push("헤딩");
const csvRows = [
headers.join(",")
];
data.forEach(d => {
const row = [];
if (settings.dataFields.name) row.push("${escapeCSV(d.name)}");
if (settings.dataFields.url) row.push("${escapeCSV(d.url)}");
if (settings.dataFields.description) row.push("${escapeCSV(d.description)}");
if (settings.dataFields.html) row.push("${escapeCSV(d.html)}");
if (settings.dataFields.category) row.push("${escapeCSV(d.category)}");
if (settings.dataFields.quality) row.push("${d.quality}");
if (settings.dataFields.language) row.push("${escapeCSV(d.language)}");
if (settings.dataFields.wordCount) row.push("${d.wordCount}");
if (settings.dataFields.metaTags) row.push("${escapeCSV(JSON.stringify(d.metaTags))}");
if (settings.dataFields.headings) row.push("${escapeCSV(d.headings.join('; '))}");
csvRows.push(row.join(","));
});
function downloadXLS() {
if (data.length === 0) {
log('다운로드할 데이터가 없습니다.', 'warning');
return;
}
const worksheet = XLSX.utils.json_to_sheet(data.map(d => {
const entry = {};
if (settings.dataFields.name) entry['사이트 이름'] = d.name;
if (settings.dataFields.url) entry['주소'] = d.url;
if (settings.dataFields.description) entry['설명'] = d.description;
if (settings.dataFields.html) entry['📄 HTML 내용'] = d.html;
if (settings.dataFields.category) entry['분류'] = d.category;
if (settings.dataFields.quality) entry['품질점수'] = d.quality;
if (settings.dataFields.language) entry['언어'] = d.language;
if (settings.dataFields.wordCount) entry['단어 수'] = d.wordCount;
if (settings.dataFields.metaTags) entry['메타 태그'] = JSON.stringify(d.metaTags);
if (settings.dataFields.headings) entry['헤딩'] = d.headings.join('; ');
return entry;
}));
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Data");
XLSX.writeFile(workbook, "data.xlsx");
log('XLS 파일이 다운로드되었습니다.', 'success');
}
// XML 이스케이프 함수
function escapeXML(str) {
if (!str) return '';
return str.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// CSV 이스케이프 함수
function escapeCSV(str) {
if (!str) return '';
return str.replace(/"/g, '""');
}
// 테이블 필터링 함수
function filterTable(keyword) {
('#data-table-body tr').each(function() {
const rowText = (this).text().toLowerCase();
if (rowText.includes(keyword.toLowerCase())) {
(this).show();
} else {
(this).hide();
}
});
}
// 설정 저장 함수
function saveSettings() {
const delay = parseInt($('#delay-input').val(), 10);
settings.delay = isNaN(delay) ? defaultSettings.delay : delay;
// 데이터 시각화 설정
settings.dataVisualization = $('#data-visualization-checkbox').is(':checked');
GM_setValue('dataVisualization', settings.dataVisualization);
// CAPTCHA 처리 설정
settings.enableCaptchas = $('#enable-captchas-checkbox').is(':checked');
GM_setValue('enableCaptchas', settings.enableCaptchas);
// 내보내기 형식 설정
settings.exportFormats.csv = ('#export-csv').is(':checked');
settings.exportFormats.json = ('#export-json').is(':checked');
settings.exportFormats.xml = ('#export-xml').is(':checked');
settings.exportFormats.xls = ('#export-xls').is(':checked');
// 고급 로깅 설정
settings.advancedLogging = $('#advanced-logging-checkbox').is(':checked');
GM_setValue('advancedLogging', settings.advancedLogging);
// 사용자 지정 키워드 분류 저장
const customKeywordsText = $('#custom-keywords-input').val();
const customKeywords = parseCustomKeywords(customKeywordsText);
settings.customKeywords = customKeywords;
GM_setValue('customKeywords', customKeywords);
// 스케줄링 설정이 활성화된 경우 스케줄링 시작
if (settings.schedule.enabled) {
scheduleCrawling();
}
// UI 업데이트
updateDataUI();
if (settings.dataVisualization) {
renderChart();
}
}
// 설정 초기화 함수
function resetSettings() {
if (confirm("기본 설정으로 초기화하시겠습니까?")) {
// 기본 설정으로 되돌리기
GM_deleteValue('delay');
GM_deleteValue('delayRandom');
GM_deleteValue('maxPages');
GM_deleteValue('userAgents');
GM_deleteValue('userAgentIndex');
GM_deleteValue('blacklist');
GM_deleteValue('whitelist');
GM_deleteValue('contentTypes');
GM_deleteValue('timeout');
GM_deleteValue('includeKeywords');
GM_deleteValue('excludeKeywords');
GM_deleteValue('regexInclude');
GM_deleteValue('regexExclude');
GM_deleteValue('language');
GM_deleteValue('minWordCount');
GM_deleteValue('maxWordCount');
GM_deleteValue('maxDepth');
GM_deleteValue('proxy');
GM_deleteValue('dataFields');
GM_deleteValue('retryAttempts');
GM_deleteValue('retryDelay');
GM_deleteValue('schedule');
GM_deleteValue('searchEngines');
GM_deleteValue('exportFormats');
GM_deleteValue('respectRobots');
GM_deleteValue('maxConcurrentRequests');
GM_deleteValue('dataVisualization');
GM_deleteValue('advancedLogging');
GM_deleteValue('enableCaptchas');
GM_deleteValue('customKeywords');
code
// 기본 설정 적용
Object.assign(settings, defaultSettings);
GM_setValue('userAgents', settings.userAgents);
GM_setValue('userAgentIndex', settings.userAgentIndex);
GM_setValue('contentTypes', settings.contentTypes);
GM_setValue('dataFields', settings.dataFields);
GM_setValue('exportFormats', settings.exportFormats);
GM_setValue('advancedLogging', settings.advancedLogging);
GM_setValue('enableCaptchas', settings.enableCaptchas);
// UI 업데이트
$('input, textarea, select').each(function() {
const id = $(this).attr('id');
if (settings.hasOwnProperty(id)) {
$(this).val(settings[id]);
}
});
// 사용자 지정 키워드 초기화
$('#custom-keywords-input').val('');
updateUserAgentSelect();
updateDataUI();
renderChart();
log('기본 설정으로 초기화되었습니다.', 'success');
}
}
// 데이터 초기화 함수
function resetData() {
if (confirm("수집된 데이터와 로그를 초기화하시겠습니까? 이 작업은 되돌릴 수 없습니다.")) {
// 데이터 삭제
GM_setValue('data', []);
GM_setValue('logs', []);
GM_setValue('visitedUrls', []);
GM_setValue('progress', 0);
GM_setValue('currentPage', 1);
GM_setValue('currentDepth', 0);
GM_setValue('isCrawling', false);
// 크롤링 시작 함수
async function startCrawling() {
if (isCrawling) {
log('이미 크롤링이 진행 중입니다.', 'warning');
return;
}
// 활성화된 검색 엔진 확인
if (!isAnySearchEngineEnabled()) {
log('사용할 검색 엔진이 하나도 선택되지 않았습니다.', 'error');
return;
}
isCrawling = true;
GM_setValue('isCrawling', isCrawling);
log('크롤링을 시작합니다.', 'info');
setButtonStates();
currentPage = GM_getValue('currentPage', 1);
totalPages = GM_getValue('totalPages', settings.maxPages);
GM_setValue('totalPages', totalPages);
currentDepth = GM_getValue('currentDepth', 0);
await crawlPage(currentDepth);
}
// 크롤링 중지 함수
function stopCrawling() {
if (!isCrawling) {
log('크롤링이 진행 중이지 않습니다.', 'warning');
return;
}
isCrawling = false;
GM_setValue('isCrawling', isCrawling);
log('크롤링이 중지되었습니다.', 'warning');
setButtonStates();
}
// 버튼 상태 설정 함수
function setButtonStates() {
if (isCrawling) {
('#start-crawling').prop('disabled', true);
('#stop-crawling').prop('disabled', false);
} else {
('#start-crawling').prop('disabled', false);
('#stop-crawling').prop('disabled', true);
}
}
// 크롤링 함수
async function crawlPage(currentDepthLevel) {
try {
while (isCrawling && currentPage <= totalPages && currentDepthLevel <= settings.maxDepth) {
log(페이지 currentPage크롤링시작.(깊이:{currentDepthLevel}), 'info');
code
// robots.txt 준수 체크
if (settings.respectRobots) {
const robotsAllowed = await checkRobotsTxt(window.location.href);
if (!robotsAllowed) {
log(`robots.txt에 의해 크롤링이 금지된 페이지: ${window.location.href}`, 'warning');
break;
}
}
const searchResults = extractSearchResults();
if (!searchResults) {
log('검색 결과를 찾을 수 없습니다.', 'error');
break;
}
for (let result of searchResults) {
if (!isCrawling) break;
const name = result.name;
const link = result.url;
const description = result.description;
// 중복 검사
if (visitedUrls.includes(link)) {
log(`중복된 URL 발견: ${link}`, 'warning');
continue;
}
// 블랙리스트/화이트리스트 검사
if (settings.whitelist.length > 0 && !settings.whitelist.some(domain => link.includes(domain))) {
log(`화이트리스트에 없는 도메인: ${link}`, 'warning');
continue;
}
if (settings.blacklist.some(domain => link.includes(domain))) {
log(`블랙리스트에 있는 도메인: ${link}`, 'warning');
continue;
}
// 언어 필터링 검사
if (settings.language.length > 0) {
const pageLanguage = await getPageLanguage(link);
if (!settings.language.includes(pageLanguage)) {
log(`선택된 언어에 맞지 않는 페이지: ${link} (${pageLanguage})`, 'warning');
continue;
}
}
// 키워드 포함/제외 검사
if (settings.includeKeywords.length > 0 && !settings.includeKeywords.some(kw => link.includes(kw) || description.includes(kw))) {
log(`포함 키워드가 없는 URL: ${link}`, 'warning');
continue;
}
if (settings.excludeKeywords.some(kw => link.includes(kw) || description.includes(kw))) {
log(`제외 키워드가 포함된 URL: ${link}`, 'warning');
continue;
}
// 정규 표현식 필터링
if (settings.regexInclude.length > 0) {
const matchesInclude = settings.regexInclude.some(pattern => new RegExp(pattern).test(link) || new RegExp(pattern).test(description));
if (!matchesInclude) {
log(`정규 표현식 포함 조건에 맞지 않는 URL: ${link}`, 'warning');
continue;
}
}
if (settings.regexExclude.length > 0) {
const matchesExclude = settings.regexExclude.some(pattern => new RegExp(pattern).test(link) || new RegExp(pattern).test(description));
if (matchesExclude) {
log(`정규 표현식 제외 조건에 맞는 URL: ${link}`, 'warning');
continue;
}
}
// 큐에 추가
requestQueue.push({ url: link, depth: currentDepthLevel });
// 최대 동시 요청 도달 시 대기
if (activeRequests >= settings.maxConcurrentRequests) {
break;
}
}
// 큐 처리 시작
processQueue();
// 진행률 업데이트
progress = (currentPage / totalPages) * 100;
GM_setValue('progress', progress);
updateLogsUI();
// 다음 페이지로 이동
if (currentPage < totalPages && isCrawling) {
const nextPageUrl = getNextPageUrl();
if (nextPageUrl) {
currentPage++;
GM_setValue('currentPage', currentPage);
currentDepth++;
GM_setValue('currentDepth', currentDepth);
log(`다음 페이지로 이동: ${currentPage}`, 'info');
await dynamicDelay();
window.location.href = nextPageUrl;
return; // 페이지 이동 후 크롤링 이어서 하기 위해 함수 종료
} else {
log('다음 페이지 링크를 찾을 수 없습니다.', 'error');
break;
}
} else {
break;
}
}
// 큐가 비어있고 모든 요청이 완료된 경우 크롤링 완료
if (isCrawling && currentPage > totalPages && activeRequests === 0 && requestQueue.length === 0) {
log('크롤링이 완료되었습니다.', 'success');
isCrawling = false;
GM_setValue('isCrawling', isCrawling);
setButtonStates();
GM_setValue('progress', 100);
$('.progress-bar-fill').css('width', `100%`);
$('.status-text').text(`진행률: 100% (${currentPage}/${totalPages} 페이지)`);
}
} catch (error) {
log(크롤링 중 오류 발생: ${error.message}, 'error');
} finally {
// 크롤링이 아직 진행 중인 경우 state를 유지
if (!isCrawling) {
// 크롤링이 중지된 경우에만 상태를 초기화
GM_setValue('isCrawling', isCrawling);
setButtonStates();
log('크롤링이 중지되었습니다.', 'info');
}
// 페이지 이동 시에는 상태를 유지하고, 다음 페이지에서 계속 크롤링
}
}
// 동시 요청 제어 함수
async function processQueue() {
while (requestQueue.length > 0 && activeRequests < settings.maxConcurrentRequests) {
const { url, depth } = requestQueue.shift();
activeRequests++;
try {
await handleRequest(url, depth);
} catch (error) {
log(요청 처리 중 오류 발생: url−{error.message}, 'error');
} finally {
activeRequests--;
processQueue();
}
}
}
async function handleRequest(url, depthLevel) {
if (!isCrawling) return;
// 중복 검사
if (visitedUrls.includes(url)) {
log(중복된 URL 발견: ${url}, 'warning');
return;
}
// 블랙리스트/화이트리스트 검사
if (settings.whitelist.length > 0 && !settings.whitelist.some(domain => url.includes(domain))) {
log(화이트리스트에 없는 도메인: ${url}, 'warning');
return;
}
if (settings.blacklist.some(domain => url.includes(domain))) {
log(블랙리스트에 있는 도메인: ${url}, 'warning');
return;
}
// 언어 필터링 검사
if (settings.language.length > 0) {
const pageLanguage = await getPageLanguage(url);
if (!settings.language.includes(pageLanguage)) {
log(선택된 언어에 맞지 않는 페이지: ${url} (${pageLanguage}), 'warning');
return;
}
}
// 키워드 포함/제외 검사
if (settings.includeKeywords.length > 0 && !settings.includeKeywords.some(kw => url.includes(kw))) {
log(포함 키워드가 없는 URL: ${url}, 'warning');
return;
}
if (settings.excludeKeywords.some(kw => url.includes(kw))) {
log(제외 키워드가 포함된 URL: ${url}, 'warning');
return;
}
// 정규 표현식 필터링
if (settings.regexInclude.length > 0) {
const matchesInclude = settings.regexInclude.some(pattern => new RegExp(pattern).test(url));
if (!matchesInclude) {
log(정규 표현식 포함 조건에 맞지 않는 URL: ${url}, 'warning');
return;
}
}
if (settings.regexExclude.length > 0) {
const matchesExclude = settings.regexExclude.some(pattern => new RegExp(pattern).test(url));
if (matchesExclude) {
log(정규 표현식 제외 조건에 맞는 URL: ${url}, 'warning');
return;
}
}
// 데이터 수집
try {
const response = await fetchWithRetry(url, settings.retryAttempts, settings.retryDelay);
const contentType = getContentType(response);
if (!isContentTypeAllowed(contentType)) {
log(허용되지 않은 콘텐츠 유형(${contentType})의 URL: ${url}, 'warning');
return;
}
code
const pageHtml = response.responseText;
const parser = new DOMParser();
const pageDoc = parser.parseFromString(pageHtml, 'text/html');
const textContent = pageDoc.body ? pageDoc.body.innerText : '';
// CAPTCHA 감지 및 처리
if (settings.enableCaptchas && detectCaptcha(pageDoc)) {
await handleCaptcha(url);
return;
}
// 단어 수 검사
const wordCount = textContent.split(/\s+/).length;
if (wordCount < settings.minWordCount) {
log(`단어 수가 최소 기준 (${settings.minWordCount})에 미치지 못하는 URL: ${url} (${wordCount} 단어)`, 'warning');
return;
}
if (wordCount > settings.maxWordCount) {
log(`단어 수가 최대 기준 (${settings.maxWordCount})을 초과하는 URL: ${url} (${wordCount} 단어)`, 'warning');
return;
}
// 콘텐츠 품질 평가
const qualityScore = evaluateContentQuality(textContent);
// 언어 추출
const language = pageDoc.documentElement.lang || 'unknown';
// 키워드 분류
const category = classifyKeywords(textContent);
// 메타 태그 및 헤딩 추출
const metaTags = extractMetaTags(pageDoc);
const headings = extractHeadings(pageDoc);
// 조건에 맞는 경우에만 데이터 추가
if (category !== '기타' || qualityScore > 100) { // 예시 조건
const dataEntry = {};
if (settings.dataFields.name) dataEntry.name = extractName(pageDoc) || 'No Title';
if (settings.dataFields.url) dataEntry.url = url;
if (settings.dataFields.description) dataEntry.description = extractDescription(pageDoc) || 'No Description';
if (settings.dataFields.html) dataEntry.html = pageHtml;
if (settings.dataFields.category) dataEntry.category = category;
if (settings.dataFields.quality) dataEntry.quality = qualityScore;
if (settings.dataFields.language) dataEntry.language = language;
if (settings.dataFields.wordCount) dataEntry.wordCount = wordCount;
if (settings.dataFields.metaTags) dataEntry.metaTags = metaTags;
if (settings.dataFields.headings) dataEntry.headings = headings;
data.push(dataEntry);
GM_setValue('data', data);
updateDataUI();
log(`데이터 수집 완료: ${url}`, 'success');
// 데이터 시각화 업데이트
if (settings.dataVisualization) {
renderChart();
}
} else {
log(`조건에 맞지 않아 데이터에 추가되지 않은 URL: ${url}`, 'info');
}
// 링크 추적을 위한 추가 로직 (심층 크롤링)
if (depthLevel < settings.maxDepth) {
const additionalLinks = extractLinks(pageDoc);
for (let additionalLink of additionalLinks) {
if (!visitedUrls.includes(additionalLink) && isValidURL(additionalLink)) {
requestQueue.push({ url: additionalLink, depth: depthLevel + 1 });
}
}
}
} catch (error) {
log(데이터 수집 실패: ${url} - ${error.message}, 'error');
// 실패한 경우 data에 추가하지 않음
}
// 활성화된 검색 엔진이 있는지 확인
function isAnySearchEngineEnabled() {
return Object.values(settings.searchEngines).some(enabled => enabled);
}
// 사용자 지정 키워드 분류 통합
function classifyKeywords(text) {
const standardKeywords = combineKeywords();
for (const [category, kws] of Object.entries(standardKeywords)) {
if (kws.some(kw => text.includes(kw))) {
return category;
}
}
return '기타';
}
// 검색 결과 추출 함수 (각 검색 엔진에 맞게 수정됨)
function extractSearchResults() {
let results = [];
// 구글 검색 결과 추출 (업데이트된 셀렉터)
if (window.location.hostname.includes('google.com') && settings.searchEngines.google) {
const items = document.querySelectorAll('div.g, div[data-hveid]');
items.forEach(item => {
const nameElement = item.querySelector('h3');
const linkElement = item.querySelector('a');
let descElement = item.querySelector('.aCOpRe');
if (!descElement) {
descElement = item.querySelector('.VwiC3b');
}
}
// 기타 검색 엔진에 대한 추가 로직 필요
else {
log('지원되지 않거나 비활성화된 검색 엔진입니다.', 'warning');
}
return results.length > 0 ? results : null;
}
// 다음 페이지 URL 얻는 함수 (각 검색 엔진에 맞게 수정됨)
function getNextPageUrl() {
let nextPageUrl = null;
// 구글 다음 페이지 링크
if (window.location.hostname.includes('google.com') && settings.searchEngines.google) {
const nextLink = document.querySelector('a#pnnext');
if (nextLink) nextPageUrl = nextLink.href;
}
// 빙 다음 페이지 링크
else if (window.location.hostname.includes('bing.com') && settings.searchEngines.bing) {
const nextLink = document.querySelector('a.sb_pagN');
if (nextLink) nextPageUrl = nextLink.href;
}
// 야후 다음 페이지 링크
else if (window.location.hostname.includes('search.yahoo.com') && settings.searchEngines.yahoo) {
const nextLink = document.querySelector('a.next');
if (nextLink) nextPageUrl = nextLink.href;
}
// 네이버 다음 페이지 링크
else if (window.location.hostname.includes('naver.com') && settings.searchEngines.naver) {
const nextLink = document.querySelector('.sc_page_inner a.btn_next');
if (nextLink) nextPageUrl = nextLink.href;
}
// 다음 다음 페이지 링크
else if (window.location.hostname.includes('daum.net') && settings.searchEngines.daum) {
const nextLink = document.querySelector('.paging a.btn_next');
if (nextLink) nextPageUrl = nextLink.href;
}
// DuckDuckGo 다음 페이지 링크 (예시)
else if (window.location.hostname.includes('duckduckgo.com') && settings.searchEngines.duckduckgo) {
const nextLink = document.querySelector('a.js-next-page');
if (nextLink) nextPageUrl = nextLink.href;
}
// Baidu 다음 페이지 링크 (예시)
else if (window.location.hostname.includes('baidu.com') && settings.searchEngines.baidu) {
const nextLink = document.querySelector('.n');
if (nextLink) nextPageUrl = nextLink.href;
}
// Yandex 다음 페이지 링크 (예시)
else if (window.location.hostname.includes('yandex.com') && settings.searchEngines.yandex) {
const nextLink = document.querySelector('.pager__item_kind_next a');
if (nextLink) nextPageUrl = nextLink.href;
}
// Ask.com 다음 페이지 링크 (추가된 검색 엔진)
else if (window.location.hostname.includes('ask.com') && settings.searchEngines.ask) {
const nextLink = document.querySelector('.pagination a.next');
if (nextLink) nextPageUrl = nextLink.href;
}
// Ecosia 다음 페이지 링크 (추가된 검색 엔진)
else if (window.location.hostname.includes('ecosia.org') && settings.searchEngines.ecosia) {
const nextLink = document.querySelector('.pagination a.next');
if (nextLink) nextPageUrl = nextLink.href;
}
// Startpage 다음 페이지 링크 (추가된 검색 엔진)
else if (window.location.hostname.includes('startpage.com') && settings.searchEngines.startpage) {
const nextLink = document.querySelector('.pagination a.next');
if (nextLink) nextPageUrl = nextLink.href;
}
// Searx 다음 페이지 링크 (추가된 검색 엔진)
else if (window.location.hostname.includes('searx') && settings.searchEngines.searx) {
const nextLink = document.querySelector('.pagination a.next');
if (nextLink) nextPageUrl = nextLink.href;
}
// Dogpile 다음 페이지 링크 (추가된 검색 엔진)
else if (window.location.hostname.includes('dogpile.com') && settings.searchEngines.dogpile) {
const nextLink = document.querySelector('.pagination a.next');
if (nextLink) nextPageUrl = nextLink.href;
}
// Bing Images 다음 페이지 링크 (추가된 검색 엔진)
else if (window.location.hostname.includes('bing.com') && settings.searchEngines.bingimages) {
const nextLink = document.querySelector('a.sb_pagN');
if (nextLink) nextPageUrl = nextLink.href;
}
// 기타 검색 엔진에 대한 추가 로직 필요
log(다음 페이지 URL: ${nextPageUrl || '없음'}, 'info');
return nextPageUrl;
}
// GM_xmlhttpRequest를 Promise로 래핑
function GM_xmlhttpRequestPromise(url) {
return new Promise((resolve, reject) => {
let requestUrl = url;
if (settings.proxy) {
// 프록시 요청을 위한 URL 구성
requestUrl = settings.proxy?url={encodeURIComponent(url)};
}
// 현재 사용자 에이전트 얻는 함수
function getCurrentUserAgent() {
return settings.userAgents[settings.userAgentIndex % settings.userAgents.length];
}
// 콘텐츠 유형 얻는 함수
function getContentType(response) {
const contentTypeHeader = response.responseHeaders.split('\n').find(header => header.toLowerCase().startsWith('content-type'));
return contentTypeHeader ? contentTypeHeader.split(':')[1].trim().split(';')[0] : '';
}
// 콘텐츠 유형 허용 여부 검사
function isContentTypeAllowed(contentType) {
if (settings.contentTypes.include.length > 0 && !settings.contentTypes.include.includes(contentType)) {
return false;
}
if (settings.contentTypes.exclude.length > 0 && settings.contentTypes.exclude.includes(contentType)) {
return false;
}
return true;
}
// 콘텐츠 품질 평가 함수
function evaluateContentQuality(text) {
const wordCount = text.split(/\s+/).length;
return wordCount;
}
// 키워드 분류 함수 (사용자 지정 키워드 포함)
function classifyKeywords(text) {
const standardKeywords = combineKeywords();
for (const [category, kws] of Object.entries(standardKeywords)) {
if (kws.some(kw => text.includes(kw))) {
return category;
}
}
return '기타';
}
parse(text) {
const lines = text.split('\n');
let userAgents = [];
for (let line of lines) {
line = line.trim();
if (line.startsWith('#') || line === '') continue;
const parts = line.split(':');
if (parts.length < 2) continue;
const key = parts[0].trim().toLowerCase();
const value = parts.slice(1).join(':').trim();
if (key === 'user-agent') {
userAgents = [value.toLowerCase()];
} else if (key === 'disallow') {
this.rules.push({ userAgents: [...userAgents], disallow: value });
} else if (key === 'allow') {
this.rules.push({ userAgents: [...userAgents], allow: value });
}
}
}
isAllowed(url, ua) {
const path = new URL(url).pathname;
let allowed = true;
for (let rule of this.rules) {
if (rule.userAgents.includes('*') || rule.userAgents.includes(ua.toLowerCase())) {
if (rule.disallow && path.startsWith(rule.disallow)) {
allowed = false;
}
if (rule.allow && path.startsWith(rule.allow)) {
allowed = true;
}
}
}
return allowed;
}
}
// 딜레이 함수
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// 설정 초기화 함수 이미 구현됨
// 모달 창 기능 이미 구현됨
// 시작 및 중지 버튼 기능 이미 구현됨
// 기타 함수 이미 포함됨
function init() {
createUI();
if (isCrawling) {
log('크롤링이 이전에 중단된 상태에서 다시 시작됩니다.', 'info');
setButtonStates();
crawlPage(currentDepth); // 페이지 로드 후 크롤링 재개
} else {
log('크롤러가 초기화되었습니다.', 'info');
}
}
// 페이지 로드 시 초기화
window.addEventListener('load', init);
// 추가적인 기능: HTML 내용 수집 및 텍스트 추출, 데이터 테이블에 표시, 모달 창으로 HTML 보기
// 이미 기존 스크립트에서 구현됨
})(); 전체 코드에 크롤링 초기화 하는 기능 추가해줘 일단 최대한 아무말없이 써봐. 그냥 코드만 써. ㅇㅇ 그리고 니가 써도 되는 용량이 최대가 될때 파트 1 으로 끝어 알겠지?