以下是用于下载按钮的功能,因为文件类型为MP4,所以新版本的浏览器通过在默认窗口中打开视频并播放来对下载按钮做出反应。我需要一个可以覆盖此操作并立即强制下载的JS脚本,因此“另存为”功能会立即打开。
// Function to generate the Download button
VideoPlayer.prototype.initDownload = function () {
var downloadBtn = $("button.download"),
downloadToolTipCls = "download_tooltip",
sources = {},
downloadToopTip,
sourcesLength = 0,
sourcesKeys;
// Add each source
if (typeof (firstSrc = this.$video.attr("src")) !== "undefined") {
sources[firstSrc] = this.$video.attr("type");
}
this.$video.find("source").each(function () {
var $this = $(this);
sources[$this.attr("src")] = $this.attr("type");
});
sourcesKeys = Object.keys(sources);
sourcesLength = sourcesKeys.length;
if (sourcesLength === 1) {
downloadBtn.wrap("<a href=\"" + sourcesKeys[0] + "\" download=\"video.title.mp4\" />");
}
};