frontend/src/functions/youtubeDownloader.js
// This function runs when one of the download buttons is clicked.

This function runs when one of the download buttons is clicked.
This function runs when one of the download buttons is clicked .
async function buttonClicked(url, whichButton) {
  const linkBox = document.getElementById("link");
  if (linkBox.value === "") {
    showAlert("Trying to download something without pasting the URL? You silly billy.", "warning");
    return;
  }
  showAlert("Initialising...", "warning");
  const firstFormData = new FormData();
  firstFormData.append("button_clicked", "yes");
  const requestProgressPath = await fetch("/api/yt", {
    method: "POST",
    body: firstFormData,
  });
  if (!requestProgressPath.ok) {
    const error = await requestProgressPath.text();
    showAlert(error, "danger");
    console.log(error);
    return;
  } else {
    let progressFilePath = await requestProgressPath.text();
    progressFilePath = `api/${progressFilePath}`;
    console.log(`https://free-av-tools.com/${progressFilePath}`);
    const secondFormData = new FormData();
    secondFormData.append("link", url);
    secondFormData.append("button_clicked", whichButton);
    shouldLog = true;
    showDownloadProgress(progressFilePath);
    const secondRequest = await fetch("/api/yt", {
      method: "POST",
      body: secondFormData,
    });
    shouldLog = false;
    if (secondRequest.status === 200) {
      const response = await secondRequest.text();
      console.log(response);
      const anchorTag = document.createElement("a");
      anchorTag.setAttribute("download", "");
      anchorTag.href = response;
      anchorTag.click();
      await sleep(1000);
      showAlert("Your browser should have started downloading the file.", "success");
    } else {
      const error = await secondRequest.text();
      showAlert(error, "danger");
      return;
    }
  }
}
async function buttonClicked ( url , whichButton ) { const linkBox = document . getElementById ( "link" ) ; if ( linkBox . value === "" ) { showAlert ( "Trying to download something without pasting the URL? You silly billy." , "warning" ) ; return ; } showAlert ( "Initialising..." , "warning" ) ; const firstFormData = new FormData ( ) ; firstFormData . append ( "button_clicked" , "yes" ) ; const requestProgressPath = await fetch ( "/api/yt" , { method : "POST" , body : firstFormData , } ) ; if ( ! requestProgressPath . ok ) { const error = await requestProgressPath . text ( ) ; showAlert ( error , "danger" ) ; console . log ( error ) ; return ; } else { let progressFilePath = await requestProgressPath . text ( ) ; progressFilePath = ` ${ progressFilePath } ` ; console . log ( ` ${ progressFilePath } ` ) ; const secondFormData = new FormData ( ) ; secondFormData . append ( "link" , url ) ; secondFormData . append ( "button_clicked" , whichButton ) ; shouldLog = true ; showDownloadProgress ( progressFilePath ) ; const secondRequest = await fetch ( "/api/yt" , { method : "POST" , body : secondFormData , } ) ; shouldLog = false ; if ( secondRequest . status === 200 ) { const response = await secondRequest . text ( ) ; console . log ( response ) ; const anchorTag = document . createElement ( "a" ) ; anchorTag . setAttribute ( "download" , "" ) ; anchorTag . href = response ; anchorTag . click ( ) ; await sleep ( 1000 ) ; showAlert ( "Your browser should have started downloading the file." , "success" ) ; } else { const error = await secondRequest . text ( ) ; showAlert ( error , "danger" ) ; return ; } } }
This function runs when one of the download buttons is clicked.
This function runs when one of the download buttons is clicked .
// 1st POST request to get the path of the progress file.
 // 2nd POST request to get the download link.
 // Sometimes the alert below didn't show up, adding a delay seems to fix this.
