下载并保存

This commit is contained in:
BlackTeay 2023-03-28 15:05:35 +08:00
parent f80589cb71
commit 452eba1191

View File

@ -2,7 +2,7 @@
// @name 央视网视频下载 // @name 央视网视频下载
// @description Download videos from cctv.com with one click // @description Download videos from cctv.com with one click
// @namespace https://iteay.top/ // @namespace https://iteay.top/
// @version 1.1 // @version 1.2
// @author BlackTeay // @author BlackTeay
// @match https://*.cctv.com/* // @match https://*.cctv.com/*
// @updateURL https://git.jlntv.work/blackteay/tampermonkey-script/raw/branch/master/Download_CCTV_Video.js // @updateURL https://git.jlntv.work/blackteay/tampermonkey-script/raw/branch/master/Download_CCTV_Video.js
@ -14,24 +14,24 @@
// @license MIT // @license MIT
// ==/UserScript== // ==/UserScript==
(function () { (function () {
function findVideoEle(src) { function findVideoEle(src, title) {
let videoJQ = $("#video"); let videoJQ = $("#video");
// 向视频旁边添加【下载】按钮 // 向视频旁边添加【下载】按钮
appendDownloadEle(videoJQ, src); appendDownloadEle(videoJQ, title, src);
} }
/** /**
* 向视频上边添加下载按钮 * 向视频上边添加下载按钮
*/ */
function appendDownloadEle(videoJQ, videoSrc) { function appendDownloadEle(videoJQ, title, videoSrc) {
let downloadIdAttrName = videoJQ.attr("id") + "_download"; let downloadIdAttrName = videoJQ.attr("id") + "_download";
console.log(downloadIdAttrName) console.log(downloadIdAttrName)
if ($("#" + downloadIdAttrName).length <= 0) { if ($("#" + downloadIdAttrName).length <= 0) {
// 【下载】按钮 // 【下载】按钮
let downloadJQ = $("<a>下载视频</a>"); let downloadJQ = $("<a>下载视频<span id='progress'></span></a>");
downloadJQ.css({ downloadJQ.css({
'background-color': 'rgba(255,130,0, 1)', 'background-color': 'rgba(255,130,0, 1)',
"color": "#fff", "color": "#fff",
@ -39,14 +39,14 @@
'cursor': 'pointer', 'cursor': 'pointer',
"border-radius": "5px", "border-radius": "5px",
"padding": "5px 10px", "padding": "5px 10px",
"display":"inline-block", "display": "inline-block",
"margin-top":"1rem", "margin-top": "1rem",
"margin-left":"0.5rem" "margin-left": "0.5rem"
}); });
downloadJQ.attr("id", downloadIdAttrName); downloadJQ.attr("id", downloadIdAttrName);
downloadJQ.attr("href", videoSrc); downloadJQ.attr("href", videoSrc);
downloadJQ.attr("target", "_balnk"); downloadJQ.attr("target", "_balnk");
downloadJQ.attr("download","true") downloadJQ.attr("download", "true")
videoJQ.parent().after(downloadJQ); videoJQ.parent().after(downloadJQ);
// console.log(videoJQ.attr("id") +" 添加下载按钮:" + downloadIdAttrName +", src=" + videoJQ.attr("src")); // console.log(videoJQ.attr("id") +" 添加下载按钮:" + downloadIdAttrName +", src=" + videoJQ.attr("src"));
@ -57,9 +57,47 @@
event.preventDefault(); event.preventDefault();
if ($(this).attr("href")) { if ($(this).attr("href")) {
// 下载视频 // 下载视频
download($(this).attr("href")); console.log("下载视频", title, videoSrc)
const downloadUrl = videoSrc;
const xhr = new XMLHttpRequest();
xhr.open('GET', downloadUrl, true);
xhr.responseType = 'blob';
xhr.onreadystatechange = function(){
console.log("READYSTATE"+ xhr.readyState);
if(this.status == 200 && this.readyState == 4){
const downloadLink = document.createElement('a');
downloadLink.href = window.URL.createObjectURL(new Blob([xhr.response]));
downloadLink.download = title + '.mp4';
document.body.appendChild(downloadLink);
downloadLink.click();
downloadLink.remove();
}else if(this.status == 404){
console.log("网页不存在");
}
}
xhr.onprogress = (event) => {
if (event.lengthComputable) {
const percent = Math.floor((event.loaded / event.total) * 100);
console.log(`${percent}%`)
$("#progress").html(`${percent}%`)
}
}
xhr.onerror = () => {
console.error('网络或服务器错误');
}
xhr.send();
//download(videoSrc,title,"video/mp4");
// 打开一个新的标签页面: 新标签页获取页面焦点,新标签页面关闭后,焦点重新回到源页面 // 打开一个新的标签页面: 新标签页获取页面焦点,新标签页面关闭后,焦点重新回到源页面
GM_openInTab($(this).attr("href"), { active: true, setParent: true }); //GM_openInTab($(this).attr("href"), { active: true, setParent: true });
} }
}); });
@ -92,10 +130,11 @@
var url = new URL(xhr.responseURL) var url = new URL(xhr.responseURL)
if (url.pathname == '/api/getHttpVideoInfo.do') { if (url.pathname == '/api/getHttpVideoInfo.do') {
var result=JSON.parse(xhr.response); var result = JSON.parse(xhr.response);
var videoURL=result.video.chapters4[0]["url"]; var title = result.title
var videoURL = result.video.chapters4[0]["url"];
console.log(videoURL); console.log(videoURL);
findVideoEle(videoURL); findVideoEle(videoURL, title);
} }
} }