Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const uploadInput = document.getElementById('pdf-upload');
+ const content = document.getElementById('content');
+ let words = [];
+ let currentPosition = 0;
+ let lastScrollPosition = 0;
+
+ uploadInput.addEventListener('change', async (e) => {
+ const file = e.target.files[0];
+ if (file && file.type === 'application/pdf') {
+ try {
+ const text = await readPdfFile(file);
+ words = text.split(/\s+/).filter(word => word.length > 0);
+ initializeContent();
+ } catch (error) {
+ alert('Error reading PDF file. Please try another file.');
+ console.error(error);
+ }
+ }
+ });
+
+ function initializeContent() {
+ content.innerHTML = words
+ .map(word => `${word}`)
+ .join(' ');
+
+ const spans = content.getElementsByTagName('span');
+ for (let span of spans) {
+ span.classList.remove('visible');
+ }
+
+ currentPosition = 0;
+ updateVisibility();
+ }
+
+ async function readPdfFile(file) {
+ // Note: In a real implementation, you would need to use a PDF.js or similar
+ // Here we're just returning placeholder text for demonstration
+ return new Promise((resolve) => {
+ setTimeout(() => {
+ resolve("This is placeholder text for demonstration. In a real implementation, you would need to use a PDF parsing library to extract the actual text content from the PDF file.");
+ }, 100);
+ });
+ }
+
+ window.addEventListener('scroll', () => {
+ const scrollDelta = window.scrollY - lastScrollPosition;
+ lastScrollPosition = window.scrollY;
+
+ if (words.length === 0) return;
+
+ const scrollSensitivity = 0.5;
+ const wordChange = Math.round(scrollDelta * scrollSensitivity);
+
+ currentPosition = Math.max(0, Math.min(currentPosition + wordChange, words.length - 1));
+ updateVisibility();
+ });
+
+ function updateVisibility() {
+ const spans = content.getElementsByTagName('span');
+ for (let i = 0; i < spans.length; i++) {
+ if (i <= currentPosition) {
+ spans[i].classList.add('visible');
+ } else {
+ spans[i].classList.remove('visible');
+ }
+ }
+ }
+
+ // Drag and drop support
+ const uploadLabel = document.querySelector('.upload-label');
+
+ uploadLabel.addEventListener('dragover', (e) => {
+ e.preventDefault();
+ uploadLabel.style.background = 'rgba(74, 144, 226, 0.2)';
+ });
+
+ uploadLabel.addEventListener('dragleave', () => {
+ uploadLabel.style.background = 'white';
+ });
+
+ uploadLabel.addEventListener('drop', (e) => {
+ e.preventDefault();
+ uploadLabel.style.background = 'white';
+
+ const file = e.dataTransfer.files[0];
+ if (file && file.type === 'application/pdf') {
+ uploadInput.files = e.dataTransfer.files;
+ uploadInput.dispatchEvent(new Event('change'));
+ } else {
+ alert('Please upload a PDF file.');
+ }
+ });
+ });