javascript Copy code
// ==UserScript==
// @name Web Crawler with Tampermonkey
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Automated web crawler with UI controls and Excel-like data display
// @author Your Name
// @match *://*/*
// @grant GM_addStyle
// @connect *
// ==/UserScript==
(function() {
'use strict';
// Add styles for the UI
GM_addStyle(`
#crawlerControlPanel {
position: fixed;
top: 10px;
right: 10px;
background: #f9f9f9;
border: 1px solid #ccc;
padding: 10px;
z-index: 10000;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
font-family: Arial, sans-serif;
}
#crawlerControlPanel button {
margin: 5px 0;
width: 100%;
padding: 5px;
}
#crawlerProgress {
margin-top: 10px;
width: 100%;
background: #e0e0e0;
border-radius: 3px;
overflow: hidden;
}
#crawlerProgressBar {
width: 0%;
height: 10px;
background: #76c7c0;
}
#crawlerTable {
position: fixed;
top: 50px;
left: 10px;
width: 80%;
height: 80%;
background: #fff;
border: 1px solid #ccc;
overflow: auto;
z-index: 10000;
display: none;
}
#crawlerTable table {
width: 100%;
border-collapse: collapse;
}
#crawlerTable th, #crawlerTable td {
border: 1px solid #ddd;
padding: 8px;
}
#crawlerTable th {
background-color: #f2f2f2;
text-align: left;
}
`);
// Create Control Panel
const controlPanel = document.createElement('div');
controlPanel.id = 'crawlerControlPanel';
controlPanel.innerHTML = `
<button id="crawlerStart">Start</button>
<button id="crawlerStop">Stop</button>
<button id="crawlerReset">Reset</button>
<div id="crawlerProgress">
<div id="crawlerProgressBar"></div>
</div>
`;
document.body.appendChild(controlPanel);
// Create Table for Results
const resultTable = document.createElement('div');
resultTable.id = 'crawlerTable';
resultTable.innerHTML = `
<table>
<thead>
<tr>
<th>Site Name</th>
<th>URL</th>
<th>Description</th>
</tr>
</thead>
<tbody id="crawlerTableBody">
</tbody>
</table>
`;
document.body.appendChild(resultTable);
// Variables
let crawling = false;
let results = [];
let currentPage = 1;
let totalPages = 1;
let totalResults = 0;
// Selectors (Adjust these selectors based on the target website)
const resultSelector = '.search-result'; // Example selector for search results
const titleSelector = '.result-title';
const urlSelector = '.result-url';
const descSelector = '.result-description';
const nextPageSelector = '.next-page';
// Start Button
document.getElementById('crawlerStart').addEventListener('click', () => {
if (!crawling) {
crawling = true;
results = [];
currentPage = 1;
totalPages = 1;
totalResults = 0;
resultTable.style.display = 'none';
crawlPage();
}
});
// Stop Button
document.getElementById('crawlerStop').addEventListener('click', () => {
crawling = false;
});
// Reset Button
document.getElementById('crawlerReset').addEventListener('click', () => {
crawling = false;
results = [];
currentPage = 1;
totalPages = 1;
totalResults = 0;
document.getElementById('crawlerProgressBar').style.width = '0%';
document.getElementById('crawlerTableBody').innerHTML = '';
resultTable.style.display = 'none';
});
// Crawl Function
function crawlPage() {
if (!crawling) return;
// Extract results on the current page
const items = document.querySelectorAll(resultSelector);
items.forEach(item => {
const title = item.querySelector(titleSelector)?.innerText || 'N/A';
const url = item.querySelector(urlSelector)?.href || 'N/A';
const description = item.querySelector(descSelector)?.innerText || 'N/A';
results.push({ title, url, description });
});
// Update progress
totalResults = results.length;
// Assuming totalPages is known or set to a default
// Update progress bar (example: 50% if on page 2 of 4)
const progress = Math.min((currentPage / totalPages) * 100, 100);
document.getElementById('crawlerProgressBar').style.width = `${progress}%`;
// Check for next page
const nextPage = document.querySelector(nextPageSelector);
if (nextPage && crawling) {
currentPage++;
nextPage.click();
setTimeout(crawlPage, 2000); // Wait for the next page to load
} else {
crawling = false;
displayResults();
}
}
// Display Results in Table
function displayResults() {
const tbody = document.getElementById('crawlerTableBody');
tbody.innerHTML = '';
results.forEach(result => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${result.title}</td>
<td><a href="${result.url}" target="_blank">${result.url}</a></td>
<td>${result.description}</td>
`;
tbody.appendChild(row);
});
resultTable.style.display = 'block';
document.getElementById('crawlerProgressBar').style.width = '100%';
}
})();