import { useState, useRef, useEffect } from 'react'; import { S, api } from '../lib.js'; const C = { accent: '#f59e0b', success: '#4ade80', error: '#f87171', muted: '#6b7280', }; export default function PaywallKiller() { const [inputUrl, setInputUrl] = useState(''); const [status, setStatus] = useState('idle'); const [archiveUrl, setArchiveUrl] = useState(''); const [showCaptcha, setShowCaptcha] = useState(false); const [errMsg, setErrMsg] = useState(''); const [pdfLoading, setPdfLoading] = useState(false); const inputRef = useRef(null); const iframeRef = useRef(null); function getTargetUrl() { const url = inputUrl.trim(); return url.startsWith('http') ? url : 'https://' + url; } function handleSearch() { if (!inputUrl.trim()) return; const aUrl = `https://archive.ph/newest/${getTargetUrl()}`; setArchiveUrl(aUrl); setStatus('captcha'); setShowCaptcha(true); setErrMsg(''); } function handleCaptchaDone() { setShowCaptcha(false); setStatus('ready'); } async function handlePdf() { setPdfLoading(true); setErrMsg(''); try { // Cookies für archive.ph aus dem Browser lesen // document.cookie liefert nur same-site cookies – archive.ph cookies // sind third-party und nicht lesbar. Wir übergeben stattdessen // die URL und lassen Puppeteer direkt die Session simulieren: // Puppeteer öffnet zuerst archive.ph root um Cookies zu setzen, // dann direkt die Snapshot-URL. const token = localStorage.getItem('sk_token'); const res = await fetch('/api/tools/paywallkiller/pdf', { method: 'POST', headers: { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}), }, body: JSON.stringify({ archiveUrl }), }); if (!res.ok) { const d = await res.json().catch(() => ({})); throw new Error(d.error || 'PDF-Fehler'); } const blob = await res.blob(); const objUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = objUrl; a.download = 'artikel_' + Date.now() + '.pdf'; document.body.appendChild(a); a.click(); document.body.removeChild(a); setTimeout(() => URL.revokeObjectURL(objUrl), 5000); } catch (err) { setErrMsg(err.message || 'PDF fehlgeschlagen'); } finally { setPdfLoading(false); } } function handleReset() { setInputUrl(''); setStatus('idle'); setArchiveUrl(''); setShowCaptcha(false); setErrMsg(''); setPdfLoading(false); setTimeout(() => inputRef.current?.focus(), 50); } return (