Download ui/script.js from cruelface/crawler: direct link, hf CLI and curl.
- Browser
- Download file 9.62 kB
-
https://huggingface.co/spaces/cruelface/crawler/resolve/main/ui/script.js
- Command line
-
hf download hf://spaces/cruelface/crawler/ui/script.js
-
curl -L -o script.js https://huggingface.co/spaces/cruelface/crawler/resolve/main/ui/script.js
9.62 kB
| let isWebMode = true; | |
| let eventSource = null; | |
| let crawlerResults = []; | |
| let searchResults = []; | |
| // Try to detect PyWebView | |
| if (window.pywebview) { | |
| isWebMode = false; | |
| console.log("Running in Desktop (PyWebView) mode"); | |
| } else { | |
| window.addEventListener('pywebviewready', () => { | |
| isWebMode = false; | |
| console.log("Switched to PyWebView Desktop mode"); | |
| }); | |
| } | |
| function switchTab(tabId) { | |
| document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active')); | |
| document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active')); | |
| event.currentTarget.classList.add('active'); | |
| document.getElementById('tab-' + tabId).classList.add('active'); | |
| } | |
| function startCrawl() { | |
| const url = document.getElementById('url').value.trim(); | |
| const keyword = document.getElementById('keyword').value.trim(); | |
| const mode = document.getElementById('mode').value; | |
| if (!url) { | |
| alert("Please enter a URL."); | |
| return; | |
| } | |
| document.getElementById('btn-run').disabled = true; | |
| document.getElementById('btn-stop').disabled = false; | |
| document.getElementById('crawler-log').innerHTML = ''; | |
| document.getElementById('crawler-matches').innerText = 'Matches found: 0'; | |
| document.getElementById('crawler-progress').style.width = '0%'; | |
| // reset search tab | |
| document.getElementById('search-log').innerHTML = ''; | |
| document.getElementById('search-matches').innerText = 'Matches found: 0'; | |
| document.getElementById('search-badge').classList.add('hidden'); | |
| document.getElementById('search-status').innerText = 'Starting...'; | |
| document.getElementById('btn-save-crawler').disabled = true; | |
| document.getElementById('btn-save-search').disabled = true; | |
| crawlerResults = []; | |
| searchResults = []; | |
| crawlerMatches = 0; | |
| if (isWebMode) { | |
| const queryParams = new URLSearchParams({ url, keyword, mode }).toString(); | |
| eventSource = new EventSource(`/api/crawl?${queryParams}`); | |
| eventSource.addEventListener('status', (e) => { | |
| const msg = JSON.parse(e.data); | |
| window.updateStatus(msg); | |
| }); | |
| eventSource.addEventListener('progress', (e) => { | |
| const val = parseFloat(e.data); | |
| window.updateProgress(val); | |
| }); | |
| eventSource.addEventListener('match', (e) => { | |
| const res = JSON.parse(e.data); | |
| crawlerResults.push(res); | |
| window.addCrawlResult(res); | |
| }); | |
| eventSource.addEventListener('crawler_finish', (e) => { | |
| const total = parseInt(e.data); | |
| window.finishCrawl(total); | |
| }); | |
| eventSource.addEventListener('search_start', (e) => { | |
| window.startSearchUI(); | |
| }); | |
| eventSource.addEventListener('search_finish', (e) => { | |
| const data = JSON.parse(e.data); | |
| searchResults = data.results || []; | |
| window.finishSearch(data.results, data.error, data.is_warning); | |
| if (eventSource) { | |
| eventSource.close(); | |
| eventSource = null; | |
| } | |
| }); | |
| eventSource.onerror = (err) => { | |
| console.error("SSE connection error or closed:", err); | |
| if (eventSource) { | |
| eventSource.close(); | |
| eventSource = null; | |
| } | |
| // If the connection was closed before finish events, trigger final state | |
| document.getElementById('btn-run').disabled = false; | |
| document.getElementById('btn-stop').disabled = true; | |
| }; | |
| } else { | |
| pywebview.api.start_crawl(url, keyword, mode); | |
| } | |
| } | |
| function stopCrawl() { | |
| if (isWebMode) { | |
| if (eventSource) { | |
| eventSource.close(); | |
| eventSource = null; | |
| } | |
| document.getElementById('btn-run').disabled = false; | |
| document.getElementById('btn-stop').disabled = true; | |
| document.getElementById('crawler-status').innerText = 'Stopped'; | |
| } else { | |
| pywebview.api.stop_crawl(); | |
| document.getElementById('btn-run').disabled = false; | |
| document.getElementById('btn-stop').disabled = true; | |
| document.getElementById('crawler-status').innerText = 'Stopped'; | |
| } | |
| } | |
| window.updateStatus = function(msg) { | |
| const log = document.getElementById('crawler-log'); | |
| log.insertAdjacentHTML('beforeend', `<div>${msg}</div>`); | |
| log.scrollTop = log.scrollHeight; | |
| document.getElementById('crawler-status').innerText = 'Running...'; | |
| }; | |
| window.updateProgress = function(val) { | |
| document.getElementById('crawler-progress').style.width = `${val * 100}%`; | |
| }; | |
| let crawlerMatches = 0; | |
| window.addCrawlResult = function(res) { | |
| crawlerMatches++; | |
| document.getElementById('crawler-matches').innerText = `Matches found: ${crawlerMatches}`; | |
| const log = document.getElementById('crawler-log'); | |
| log.insertAdjacentHTML('beforeend', ` | |
| <div class="result-item"> | |
| <div class="result-title">Found Match (${res.keyword})</div> | |
| <a href="${res.matched_url}" target="_blank" class="result-url">${res.matched_url}</a> | |
| <div class="result-snippet">${res.content ? res.content.substring(0, 100) + '...' : ''}</div> | |
| </div> | |
| `); | |
| log.scrollTop = log.scrollHeight; | |
| }; | |
| window.finishCrawl = function(total) { | |
| document.getElementById('btn-run').disabled = false; | |
| document.getElementById('btn-stop').disabled = true; | |
| document.getElementById('crawler-status').innerText = 'Completed'; | |
| document.getElementById('btn-save-crawler').disabled = (crawlerMatches === 0); | |
| }; | |
| window.startSearchUI = function() { | |
| document.getElementById('search-status').innerText = 'Searching DuckDuckGo...'; | |
| }; | |
| window.finishSearch = function(results, error, isWarning) { | |
| const badge = document.getElementById('search-badge'); | |
| badge.classList.remove('hidden', 'success'); | |
| const count = results ? results.length : 0; | |
| if (error || isWarning || count === 0) { | |
| badge.innerText = '0'; | |
| document.getElementById('search-status').innerText = `Completed - 0 result(s)`; | |
| document.getElementById('search-matches').innerText = `Matches found: 0`; | |
| if (error) { | |
| document.getElementById('search-log').innerHTML = `<div style="color:var(--danger)">Error: ${error}</div>`; | |
| } else { | |
| document.getElementById('search-log').innerHTML = `<div>No matches found.</div>`; | |
| } | |
| } else { | |
| badge.innerText = count.toString(); | |
| badge.classList.add('success'); | |
| document.getElementById('search-status').innerText = `Completed - ${count} result(s)`; | |
| document.getElementById('search-matches').innerText = `Matches found: ${count}`; | |
| document.getElementById('btn-save-search').disabled = false; | |
| let html = ''; | |
| results.forEach(r => { | |
| html += ` | |
| <div class="result-item"> | |
| <div class="result-title">${r.title}</div> | |
| <a href="${r.url}" target="_blank" class="result-url">${r.url}</a> | |
| <div class="result-snippet">${r.snippet}</div> | |
| </div>`; | |
| }); | |
| document.getElementById('search-log').innerHTML = html; | |
| } | |
| }; | |
| function saveCrawlerResults() { | |
| if (isWebMode) { | |
| if (crawlerResults.length === 0) return; | |
| let csv = 'source_url,matched_url,keyword,location_type,element,attribute,content_sample,timestamp\n'; | |
| crawlerResults.forEach(r => { | |
| const sample = r.content ? r.content.substring(0, 300).replace(/"/g, '""') : ''; | |
| csv += `"${r.source_url || ''}","${r.matched_url || ''}","${r.keyword || ''}","${r.location_type || ''}","${r.element || ''}","${r.attribute || ''}","${sample}","${r.timestamp || ''}"\n`; | |
| }); | |
| downloadBlob(csv, `crawl_report_${getFormattedTimestamp()}.csv`, 'text/csv'); | |
| } else { | |
| pywebview.api.save_crawl_results().then(res => { | |
| if (res) alert("Results saved successfully!"); | |
| }); | |
| } | |
| } | |
| function saveSearchResults() { | |
| if (isWebMode) { | |
| if (searchResults.length === 0) return; | |
| let csv = 'title,url,snippet\n'; | |
| searchResults.forEach(r => { | |
| const title = (r.title || '').replace(/"/g, '""'); | |
| const snippet = (r.snippet || '').replace(/"/g, '""'); | |
| csv += `"${title}","${r.url || ''}","${snippet}"\n`; | |
| }); | |
| downloadBlob(csv, `websearch_results_${getFormattedTimestamp()}.csv`, 'text/csv'); | |
| } else { | |
| pywebview.api.save_search_results().then(res => { | |
| if (res) alert("Results saved successfully!"); | |
| }); | |
| } | |
| } | |
| // Helpers for Web Mode | |
| function downloadBlob(content, filename, contentType) { | |
| const blob = new Blob([content], { type: contentType + ';charset=utf-8;' }); | |
| const url = URL.createObjectURL(blob); | |
| const link = document.createElement("a"); | |
| link.setAttribute("href", url); | |
| link.setAttribute("download", filename); | |
| link.style.visibility = 'hidden'; | |
| document.body.appendChild(link); | |
| link.click(); | |
| document.body.removeChild(link); | |
| } | |
| function getFormattedTimestamp() { | |
| const now = new Date(); | |
| const pad = (n) => String(n).padStart(2, '0'); | |
| return now.getFullYear() + | |
| pad(now.getMonth() + 1) + | |
| pad(now.getDate()) + '_' + | |
| pad(now.getHours()) + | |
| pad(now.getMinutes()) + | |
| pad(now.getSeconds()); | |
| } | |