diff --git a/libiary/Download_CCTV_Video.js b/libiary/Download_CCTV_Video.js index 41e3ca0..9d67ea8 100755 --- a/libiary/Download_CCTV_Video.js +++ b/libiary/Download_CCTV_Video.js @@ -2,7 +2,7 @@ // @name 央视网视频下载 // @description Download videos from cctv.com with one click // @namespace https://iteay.top/ -// @version 1.1 +// @version 1.2 // @author BlackTeay // @match https://*.cctv.com/* // @updateURL https://git.jlntv.work/blackteay/tampermonkey-script/raw/branch/master/Download_CCTV_Video.js @@ -14,24 +14,24 @@ // @license MIT // ==/UserScript== (function () { - function findVideoEle(src) { + function findVideoEle(src, title) { 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"; console.log(downloadIdAttrName) if ($("#" + downloadIdAttrName).length <= 0) { // 【下载】按钮 - let downloadJQ = $("下载视频"); + let downloadJQ = $("下载视频"); downloadJQ.css({ 'background-color': 'rgba(255,130,0, 1)', "color": "#fff", @@ -39,14 +39,14 @@ 'cursor': 'pointer', "border-radius": "5px", "padding": "5px 10px", - "display":"inline-block", - "margin-top":"1rem", - "margin-left":"0.5rem" + "display": "inline-block", + "margin-top": "1rem", + "margin-left": "0.5rem" }); downloadJQ.attr("id", downloadIdAttrName); downloadJQ.attr("href", videoSrc); downloadJQ.attr("target", "_balnk"); - downloadJQ.attr("download","true") + downloadJQ.attr("download", "true") videoJQ.parent().after(downloadJQ); // console.log(videoJQ.attr("id") +" 添加下载按钮:" + downloadIdAttrName +", src=" + videoJQ.attr("src")); @@ -57,9 +57,47 @@ event.preventDefault(); 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) if (url.pathname == '/api/getHttpVideoInfo.do') { - var result=JSON.parse(xhr.response); - var videoURL=result.video.chapters4[0]["url"]; + var result = JSON.parse(xhr.response); + var title = result.title + var videoURL = result.video.chapters4[0]["url"]; console.log(videoURL); - findVideoEle(videoURL); + findVideoEle(videoURL, title); } }