document.addEventListener('DOMContentLoaded', function() { // Authentication state const authState = { facebook: false, instagram: false, facebookToken: null, instagramToken: null }; // Facebook App ID - replace with your actual Facebook App ID const FACEBOOK_APP_ID = 'your-facebook-app-id'; // Instagram App ID - replace with your actual Instagram App ID const INSTAGRAM_APP_ID = 'your-instagram-app-id'; // Initialize UI elements const youtubeUrlInput = document.getElementById('youtube-url'); const fetchBtn = document.getElementById('fetch-btn'); const videoPreviewContainer = document.getElementById('video-preview-container'); const videoPreview = document.getElementById('video-preview'); const videoInfo = document.getElementById('video-info'); const postBtn = document.getElementById('post-btn'); const fbAuthBtn = document.getElementById('fb-auth-btn'); const igAuthBtn = document.getElementById('ig-auth-btn'); const fbAuthStatus = document.getElementById('fb-auth-status'); const igAuthStatus = document.getElementById('ig-auth-status'); // Authentication handlers fbAuthBtn.addEventListener('click', async function() { fbAuthBtn.disabled = true; fbAuthBtn.innerHTML = ' Authenticating...'; feather.replace(); // Open Facebook OAuth dialog const width = 600; const height = 600; const left = (screen.width - width) / 2; const top = (screen.height - height) / 2; const authWindow = window.open( `https://www.facebook.com/v12.0/dialog/oauth?client_id=${FACEBOOK_APP_ID}&redirect_uri=${encodeURIComponent(window.location.href)}&scope=pages_manage_posts,pages_read_engagement&response_type=token`, 'FacebookAuth', `width=${width},height=${height},top=${top},left=${left}` ); // Poll for auth completion const checkAuth = setInterval(() => { try { if (authWindow.closed) { clearInterval(checkAuth); fbAuthBtn.disabled = false; fbAuthBtn.innerHTML = ' Authenticate Facebook'; feather.replace(); } if (authWindow.location.href.includes('access_token=')) { const token = new URL(authWindow.location.href).hash.match(/access_token=([^&]+)/)[1]; authState.facebook = true; authState.facebookToken = token; fbAuthBtn.classList.add('hidden'); fbAuthStatus.classList.remove('hidden'); feather.replace(); authWindow.close(); clearInterval(checkAuth); } } catch (e) { // Cross-origin errors are expected until auth completes } }, 500); }); igAuthBtn.addEventListener('click', async function() { igAuthBtn.disabled = true; igAuthBtn.innerHTML = ' Authenticating...'; feather.replace(); // Open Instagram OAuth dialog const width = 600; const height = 600; const left = (screen.width - width) / 2; const top = (screen.height - height) / 2; const authWindow = window.open( `https://api.instagram.com/oauth/authorize?client_id=${INSTAGRAM_APP_ID}&redirect_uri=${encodeURIComponent(window.location.href)}&scope=user_profile,user_media&response_type=code`, 'InstagramAuth', `width=${width},height=${height},top=${top},left=${left}` ); // Poll for auth completion const checkAuth = setInterval(() => { try { if (authWindow.closed) { clearInterval(checkAuth); igAuthBtn.disabled = false; igAuthBtn.innerHTML = ' Authenticate Instagram'; feather.replace(); } if (authWindow.location.href.includes('code=')) { const code = new URL(authWindow.location.href).searchParams.get('code'); // In a real app, you would exchange this code for a token via your backend authState.instagram = true; authState.instagramToken = code; igAuthBtn.classList.add('hidden'); igAuthStatus.classList.remove('hidden'); feather.replace(); authWindow.close(); clearInterval(checkAuth); } } catch (e) { // Cross-origin errors are expected until auth completes } }, 500); }); // Fetch video details when button is clicked fetchBtn.addEventListener('click', async function() { const url = youtubeUrlInput.value.trim(); if (!url) { alert('Please enter a YouTube URL'); return; } // Validate YouTube URL format const youtubeRegex = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+/; if (!youtubeRegex.test(url)) { alert('Please enter a valid YouTube URL (e.g., https://www.youtube.com/watch?v=...)'); return; } // Show loading state fetchBtn.disabled = true; fetchBtn.innerHTML = ' Fetching...'; feather.replace(); try { // Extract video ID from URL const videoId = extractVideoId(url); if (!videoId) { throw new Error('Could not extract video ID from URL'); } // Display video preview with proper embed URL videoPreview.src = `https://www.youtube.com/embed/${videoId}?autoplay=0&rel=0&showinfo=1`; // Use oEmbed API which doesn't require an API key const response = await fetch(`https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=${videoId}&format=json`); if (!response.ok) { throw new Error('Video not found or private'); } const videoData = await response.json(); const title = videoData.title; const thumbnail = `https://img.youtube.com/vi/${videoId}/hqdefault.jpg`; // For duration we'll need to make a separate call to a public API const durationResponse = await fetch(`https://yt.lemnoslife.com/videos?part=contentDetails&id=${videoId}`); const durationData = await durationResponse.json(); const duration = durationData.items?.[0]?.contentDetails?.duration || 'N/A'; videoInfo.innerHTML = `
Title: ${title}
Duration: ${formatDuration(duration)}