0

i was trying to create a download button which on clicking takes the text from a particular website/link takes all the text from there and download it on the users system in .txt format. here's what i've been trying and not working out code:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
    <title>You Tube Video Summarizer</title>
</head>
<body>
    <div id="container">
        <img id="img" src="img.jpeg">
        <h2>YouTube Video Summarizer</h2>
        <div class="video">
            <label>Enter your url here</label>
            <br>
            <input type="url" id="url" placeholder="https://www.youtube.com/results?search_query=youtube">
        </div>
        <br>
        <div class="mb-3">
            <label for="exampleFormControlInput1" class="form-label" >Choose summarization algorithm</label>
            <br>
        </div>
        <select class="form-select" aria-label="Default select example">
            <option selected>Default</option>
            <option value="1">NLTK</option>
            <option value="2">spaCy</option>
        </select>
        <br>
        <br>
        <label for="customRange1" class="form-label">Select length of summary</label>
        <br>
        <input type="range" class="form-range" id="customRange1">  
        <br>
        <br>
        <button type="button" class="btn btn-primary" id="summarize">Summarize</button> 
        <br>
        <br>
        <button type="button" id = "download" class="btn btn-primary">Download</button>
    </div>
    <script> 
      function alert(){
         var save = document.getElementById("download");
         var dl  = document.createElement('a');
         dl.setAttribute('href', 'link(https//...', 'data:text;charset=utf-8,' + encoderURIComponent(save));
         dl.setAttribute('download', 'filename.txt');
         dl.click();
      }
      document.addEventListener('DOMContentLoaded', function(){
          document.getElementById('alertButton').addEventListener('click', alert);
      });
     </script>
            
</body>
</html>
  • Does this answer your question? [Force download of 'data:text/plain' URL](https://stackoverflow.com/questions/6468517/force-download-of-datatext-plain-url) – Cesare Polonara May 01 '22 at 11:45
  • @CesarePolonara i did not understand the solutions given there, in my solution i have created a download button and on an onClick function what it requires to do is from a given link it fetches all the text and download in .txt file is what i want but not happening. –  May 01 '22 at 11:53
  • change `document.getElementById('alertButton').addEventListener('click', alert);` to `document.getElementById('download').addEventListener('click', alert);` You have no element with id of *alertButton* . – Cesare Polonara May 01 '22 at 12:04
  • did that the thing is on inspecting and on putting breakpoints this is not at all working @CesarePolonara –  May 01 '22 at 12:09
  • You have a lot of typos and errors in your js code, it's not even clear what you are trying to do by passing a DOM element into the href data. You want to scrape youtube? You need to do that serverside, you can't do that from client... – Cesare Polonara May 01 '22 at 12:20
  • Please go through your code carefully and use your browser dev tools inspect facility to find errors as well. For example, you have a missing comma after 'href' when you are setting the attribute, you have spelled loaded with a double e, there is no alertButton etc. – A Haworth May 01 '22 at 12:20
  • @CesarePolonara right there are errors, working on them, here i was just simply trying to get some text from a weblink and download it in a txt file format thats all –  May 01 '22 at 12:35
  • The alert is also name of a built-in function. You sould use another name. – user17517503 May 01 '22 at 12:49
  • What is the function `encoderURIComponent`? – user17517503 May 01 '22 at 12:51
  • `dl.setAttribute('href', 'link(https//...', 'data:text;charset=utf-8,' + encoderURIComponent(save))` - this line makes `dl.href='link(https://...'` (the last argument will be ignored, the `HTMLElement.setAttribute` function only get 2 parameters). – user17517503 May 01 '22 at 12:56

0 Answers0