Files
TornActivityTracker/app/static/index.js

76 lines
2.6 KiB
JavaScript

class ScraperApp {
constructor() {
this.form = document.getElementById('scrapingForm');
this.stopButton = document.getElementById('stopButton');
this.startButton = document.getElementById('startButton');
this.activityIndicator = document.getElementById('activity_indicator');
this.init();
}
init() {
this.checkScrapingStatus();
this.addEventListeners();
}
async checkScrapingStatus() {
try {
const response = await fetch('/scraping_status');
const data = await response.json();
if (data.scraping_active) {
this.startButton.disabled = true;
this.stopButton.disabled = false;
this.activityIndicator.classList.remove('text-bg-danger');
this.activityIndicator.classList.add('text-bg-success');
this.activityIndicator.textContent = 'Active';
} else {
this.startButton.disabled = false;
this.stopButton.disabled = true;
this.activityIndicator.classList.remove('text-bg-success');
this.activityIndicator.classList.add('text-bg-danger');
this.activityIndicator.textContent = 'Inactive';
}
} catch (error) {
console.error('Error checking scraping status:', error);
}
}
async startScraping(event) {
event.preventDefault(); // Prevent the default form submission
const formData = new FormData(this.form);
try {
const response = await fetch('/start_scraping', {
method: 'POST',
body: formData
});
const data = await response.json();
if (data.status === "Scraping started") {
this.checkScrapingStatus();
}
} catch (error) {
console.error('Error starting scraping:', error);
}
}
async stopScraping() {
try {
const response = await fetch('/stop_scraping', {
method: 'POST'
});
const data = await response.json();
if (data.status === "Scraping stopped") {
this.checkScrapingStatus();
}
} catch (error) {
console.error('Error stopping scraping:', error);
}
}
addEventListeners() {
this.form.addEventListener('submit', (event) => this.startScraping(event));
this.stopButton.addEventListener('click', () => this.stopScraping());
}
}
// Initialize the application when DOM is fully loaded
document.addEventListener('DOMContentLoaded', () => new ScraperApp());