javascript - javascript下载按钮而不是右键单击
问题描述
我正在尝试为 instagram 制作视频下载器,但每当我获取视频网址时,它不会下载,我必须右键单击并保存。有没有办法可以制作下载按钮而不是右键单击保存?
这是我的代码
$("#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");
}
}
解决方案
可能在您的函数中为您的可点击 html 元素设置download
属性。createMedia
<a href="myvideo.mp4" download>Download</a>
供参考访问:https://www.w3schools.com/tags/att_a_download.asp
推荐阅读
- python - 将 STDOUT 和 STDERR 重定向到 python logger 和 jupyter notebook
- xamarin - 与 listview 绑定的 observableCollection 的 Xamarin 返回索引
- flask - 用于搜索引擎的 Solr apache 服务器和 python 烧瓶
- php - for 循环内的多重比较不会破坏 php 代码。为什么?
- botframework - window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory(...).then 不是函数
- go - 无法从 GoLand 运行 go 测试(Intellij Idea):编译失败
- go - 如何在golang中查找mp3文件的长度?
- python - 如何在 QtVideoWigdet 中加载路径
- reactjs - 如何显示对象内部的列表响应-react js
- javascript - 使用 d3.js 树形图块进行文本响应