document.addEventListener('DOMContentLoaded', () => { const webview = document.getElementById('webview'); const urlInput = document.querySelector('input[type="text"]'); const backButton = document.querySelector('button:nth-of-type(1)'); const forwardButton = document.querySelector('button:nth-of-type(2)'); const refreshButton = document.querySelector('button:nth-of-type(3)'); const statusText = document.getElementById('loading-status'); const zoomText = document.getElementById('zoom-level'); let currentZoom = 100; // Navigation controls backButton.addEventListener('click', () => { webview.contentWindow.history.back(); }); forwardButton.addEventListener('click', () => { webview.contentWindow.history.forward(); }); refreshButton.addEventListener('click', () => { webview.src = webview.src; }); // URL navigation urlInput.addEventListener('keydown', (e) => { if (e.key === 'Enter') { let url = urlInput.value.trim(); statusText.textContent = 'Loading...'; statusText.classList.add('animate-pulse'); // Add protocol if missing if (!url.startsWith('http://') && !url.startsWith('https://')) { url = 'https://' + url; } try { // Create URL object to validate new URL(url); webview.src = url; } catch (error) { // If URL is invalid, try with www prefix if (!url.includes('www.')) { try { url = url.replace('://', '://www.'); new URL(url); webview.src = url; } catch (e) { // If still invalid, try Google search if (!url.startsWith('http')) { webview.src = `https://www.google.com/search?q=${encodeURIComponent(urlInput.value)}`; } else { statusText.textContent = 'Invalid URL'; statusText.classList.remove('animate-pulse'); } } } else { statusText.textContent = 'Invalid URL'; statusText.classList.remove('animate-pulse'); } } } }); // Update UI based on webview events webview.addEventListener('loadstart', () => { statusText.textContent = 'Loading...'; statusText.classList.add('animate-pulse'); }); webview.addEventListener('load', () => { statusText.textContent = 'Done'; statusText.classList.remove('animate-pulse'); urlInput.value = webview.src; }); webview.addEventListener('loaderror', () => { statusText.textContent = 'Failed to load'; statusText.classList.remove('animate-pulse'); // Try falling back to http if https failed if (webview.src.startsWith('https://')) { const httpUrl = webview.src.replace('https://', 'http://'); webview.src = httpUrl; } else if (!webview.src.includes('google.com/search')) { // If still failing, try Google search webview.src = `https://www.google.com/search?q=${encodeURIComponent(urlInput.value)}`; } }); // Update webview URL when navigation happens inside iframe webview.addEventListener('load', () => { try { urlInput.value = webview.src; statusText.textContent = 'Done'; statusText.classList.remove('animate-pulse'); // Update security indicator const securityIndicator = document.querySelector('#page-status i'); if (webview.src.startsWith('https://')) { securityIndicator.classList.remove('text-red-500'); securityIndicator.classList.add('text-green-500'); securityIndicator.setAttribute('data-feather', 'shield'); feather.replace(); } else { securityIndicator.classList.remove('text-green-500'); securityIndicator.classList.add('text-red-500'); securityIndicator.setAttribute('data-feather', 'alert-triangle'); feather.replace(); } } catch (e) { // Cross-origin error, can't access iframe src console.log('Cross-origin iframe, cannot inspect URL'); } }); // Zoom functionality document.addEventListener('keydown', (e) => { if (e.ctrlKey) { if (e.key === '+' && currentZoom < 200) { currentZoom += 10; updateZoom(); e.preventDefault(); } else if (e.key === '-' && currentZoom > 50) { currentZoom -= 10; updateZoom(); e.preventDefault(); } else if (e.key === '0') { currentZoom = 100; updateZoom(); e.preventDefault(); } } }); function updateZoom() { webview.style.transform = `scale(${currentZoom / 100})`; webview.style.transformOrigin = 'top left'; zoomText.textContent = `${currentZoom}%`; } // Dark mode toggle (will be used by navbar component) window.toggleDarkMode = function() { document.documentElement.classList.toggle('dark'); }; });