javascript Copy code
// ==UserScript==
// @name Web Crawler with UI
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Automated web crawler with UI for displaying results
// @author YourName
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Create UI Elements
const uiContainer = document.createElement('div');
uiContainer.style.position = 'fixed';
uiContainer.style.top = '10px';
uiContainer.style.right = '10px';
uiContainer.style.width = '400px';
uiContainer.style.height = '600px';
uiContainer.style.backgroundColor = 'white';
uiContainer.style.border = '1px solid #ccc';
uiContainer.style.padding = '10px';
uiContainer.style.zIndex = '10000';
uiContainer.style.overflow = 'auto';
uiContainer.style.boxShadow = '0 0 10px rgba(0,0,0,0.5)';
// Title
const title = document.createElement('h3');
title.innerText = 'Web Crawler';
uiContainer.appendChild(title);
// Controls
const startBtn = document.createElement('button');
startBtn.innerText = 'Start';
startBtn.style.marginRight = '5px';
uiContainer.appendChild(startBtn);
const stopBtn = document.createElement('button');
stopBtn.innerText = 'Stop';
stopBtn.style.marginRight = '5px';
uiContainer.appendChild(stopBtn);
const resetBtn = document.createElement('button');
resetBtn.innerText = 'Reset';
uiContainer.appendChild(resetBtn);
// Progress Bar
const progressContainer = document.createElement('div');
progressContainer.style.width = '100%';
progressContainer.style.backgroundColor = '#f3f3f3';
progressContainer.style.border = '1px solid #ccc';
progressContainer.style.marginTop = '10px';
progressContainer.style.position = 'relative';
progressContainer.style.height = '20px';
const progressBar = document.createElement('div');
progressBar.style.width = '0%';
progressBar.style.height = '100%';
progressBar.style.backgroundColor = '#4caf50';
progressContainer.appendChild(progressBar);
const progressText = document.createElement('span');
progressText.style.position = 'absolute';
progressText.style.top = '0';
progressText.style.left = '50%';
progressText.style.transform = 'translateX(-50%)';
progressText.style.fontSize = '12px';
progressText.style.lineHeight = '20px';
progressText.innerText = '0%';
progressContainer.appendChild(progressText);
uiContainer.appendChild(progressContainer);
// Results Table
const table = document.createElement('table');
table.style.width = '100%';
table.style.borderCollapse = 'collapse';
table.style.marginTop = '10px';
const thead = document.createElement('thead');
const headerRow = document.createElement('tr');
const headers = ['Site Name', 'Address', 'Description'];
headers.forEach(text => {
const th = document.createElement('th');
th.innerText = text;
th.style.border = '1px solid #ddd';
th.style.padding = '8px';
th.style.backgroundColor = '#f2f2f2';
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
const tbody = document.createElement('tbody');
table.appendChild(tbody);
uiContainer.appendChild(table);
document.body.appendChild(uiContainer);
// Variables
let isRunning = false;
let results = [];
let currentPage = 1;
let totalPages = 1;
let observer;
// Functions
function updateProgress(percent) {
progressBar.style.width = percent + '%';
progressText.innerText = percent + '%';
}
function addRow(data) {
const row = document.createElement('tr');
data.forEach(text => {
const td = document.createElement('td');
td.innerText = text;
td.style.border = '1px solid #ddd';
td.style.padding = '8px';
row.appendChild(td);
});
tbody.appendChild(row);
}
function resetUI() {
results = [];
currentPage = 1;
totalPages = 1;
tbody.innerHTML = '';
updateProgress(0);
}
function stopCrawling() {
isRunning = false;
if (observer) observer.disconnect();
}
async function crawlPage() {
if (!isRunning) return;
// Extract data from current page
// TODO: Update selectors based on the target website
const items = document.querySelectorAll('.search-result-item'); // Placeholder selector
items.forEach(item => {
const name = item.querySelector('.site-name')?.innerText || 'N/A';
const address = item.querySelector('.site-address')?.innerText || 'N/A';
const description = item.querySelector('.site-description')?.innerText || 'N/A';
results.push({ name, address, description });
addRow([name, address, description]);
});
// Update progress
const percent = Math.min(100, Math.round((currentPage / totalPages) * 100));
updateProgress(percent);
if (currentPage >= totalPages) {
isRunning = false;
alert('Crawling completed!');
return;
}
// Navigate to next page
currentPage++;
// TODO: Update pagination navigation based on the target website
const nextPageBtn = document.querySelector('.pagination-next'); // Placeholder selector
if (nextPageBtn) {
nextPageBtn.click();
} else {
isRunning = false;
alert('No more pages.');
}
}
function startCrawling() {
if (isRunning) return;
isRunning = true;
resetUI();
// Determine total pages
// TODO: Update selector based on the target website
const totalPagesElem = document.querySelector('.pagination-total'); // Placeholder selector
totalPages = totalPagesElem ? parseInt(totalPagesElem.innerText) : 1;
crawlPage();
// Observe for page changes
observer = new MutationObserver(() => {
if (!isRunning) return;
setTimeout(crawlPage, 1000); // Wait for page to load
});
observer.observe(document.body, { childList: true, subtree: true });
}
// Event Listeners
startBtn.addEventListener('click', () => {
startCrawling();
});
stopBtn.addEventListener('click', () => {
stopCrawling();
});
resetBtn.addEventListener('click', () => {
stopCrawling();
resetUI();
});
// Monitor search input
// TODO: Update selector based on the target website
const searchInput = document.querySelector('input[type="search"], input[type="text"]');
if (searchInput) {
searchInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
setTimeout(() => {
startCrawling();
}, 1000); // Wait for search results to load
}
});
}
})();