MjSal
MjSal

Reputation: 1

javascript download button instead of right click

I'm trying to make a video downloader for instagram but whenever I fetch the video url it does not download I have to right click and save. is there a way I could make a download button instead of right click to save?

here my code

$("#getMedia").click(getMedia);
var render = document.querySelector("#media");

function createMedia(data, type) {
    var media = document.createElement(type);
    media.id = "instagramMedia";
    media.src = data.content;
    media.setAttribute("class", "");
    media.width = $(render).width();
    
    if (type === "video") {
        media.controls = true;
        media.autoplay = true;      
    } 
    render.innerHTML = "";
    var downloadMsg = document.createElement("p");
    downloadMsg.setAttribute("class", "bg-success text-info");
    downloadMsg.innerText = "Right Click on the Media below to get Save Option!";

    render.appendChild(downloadMsg);
    render.appendChild(media);
}

function getMedia() {
    var url = $("#postUrl").val();
    if (url) {
        $.get(url, function (data) {
            render.innerHTML = data;
            var mediaWaitTimer = setTimeout(function () {
                var video = document.querySelector('meta[property="og:video"]');
                if (video) {
                    createMedia(video, "video");
                } else {
                    var img = document.querySelector('meta[property="og:image"]');
                    if (img) {
                        createMedia(img, "img");
                    } else {
                        document.body.innerHTML = body;
                        alert("Error extracting Instagram image / video.");
                    };
                }
                clearTimeout(mediaWaitTimer);
            }, 200);

        });

    } else {
        document.querySelector("#media").setAttribute("placeholder", "Invalid Address, Please Enter Proper Insagram Link");

    }
}

Upvotes: 0

Views: 208

Answers (1)

Shivansh Seth
Shivansh Seth

Reputation: 361

Set download attribute for your clickable html element probably in your createMedia function.

<a href="myvideo.mp4" download>Download</a>

for reference visit : https://www.w3schools.com/tags/att_a_download.asp

Upvotes: 1

Related Questions