document.addEventListener("DOMContentLoaded", () => {
  chrome.runtime.sendMessage({ action: "getVideoUrl" }, (response) => {
    let videoUrl = response.videoUrl;
    document.getElementById("downloadBtn").addEventListener("click", () => {
      let quality = document.getElementById("quality").value;

      fetch("http://127.0.0.1:7860/receive", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ url: videoUrl, quality })
      })
      .then(res => res.text())
      .then(msg => alert("✅ " + msg))
      .catch(err => alert("❌ Error: " + err));
    });
  });
});
