// ==UserScript== // @name 央视网视频下载 // @description Download videos from cctv.com with one click // @namespace https://iteay.top/ // @version 1.1 // @author BlackTeay // @match https://*.cctv.com/* // @updateURL https://git.jlntv.work/blackteay/tampermonkey-script/raw/branch/master/Download_CCTV_Video.js // @downloadURL https://git.jlntv.work/blackteay/tampermonkey-script/raw/branch/master/Download_CCTV_Video.js // @require http://libs.baidu.com/jquery/2.0.0/jquery.min.js // @require https://cdn.bootcdn.net/ajax/libs/downloadjs/1.4.8/download.min.js // @grant GM_xmlhttpRequest // @grant GM_openInTab // @license MIT // ==/UserScript== (function () { function findVideoEle(src) { let videoJQ = $("#video"); // 向视频旁边添加【下载】按钮 appendDownloadEle(videoJQ, src); } /** * 向视频上边添加【下载】按钮 */ function appendDownloadEle(videoJQ, videoSrc) { let downloadIdAttrName = videoJQ.attr("id") + "_download"; console.log(downloadIdAttrName) if ($("#" + downloadIdAttrName).length <= 0) { // 【下载】按钮 let downloadJQ = $("下载视频"); downloadJQ.css({ 'background-color': 'rgba(255,130,0, 1)', "color": "#fff", 'font-size': '15px', 'cursor': 'pointer', "border-radius": "5px", "padding": "5px 10px", "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") videoJQ.parent().after(downloadJQ); // console.log(videoJQ.attr("id") +" 添加下载按钮:" + downloadIdAttrName +", src=" + videoJQ.attr("src")); // 为下载按钮绑定单击事件下载视频 downloadJQ.on("click", function (event) { // console.log("下载视频:" + $(this).attr("id") + ", href=" + $(this).attr("href")); // 阻止 a 标签默认行为 event.preventDefault(); if ($(this).attr("href")) { // 下载视频 download($(this).attr("href")); // 打开一个新的标签页面: 新标签页获取页面焦点,新标签页面关闭后,焦点重新回到源页面 GM_openInTab($(this).attr("href"), { active: true, setParent: true }); } }); } else { // console.log(videoJQ.attr("id") +" 已存在下载按钮:" + downloadIdAttrName + ", src=" + videoJQ.attr("src")); } } console.log('加载脚本'); /** * 拦截并修改ajax请求 */ window.beforeXMLHttpRequestOpen = function (xhr, options) { //console.log('before open', xhr); // 修改url //options.url = options.url.replace('wd=123', 'wd=456'); // 修改method // options.method = 'POST'; }; /** * 拦截并修改ajax请求 */ window.beforeXMLHttpRequestSend = function (xhr, body) { //console.log('before send', xhr); xhr.onload = () => { if (xhr.status < 200 || xhr.status >= 300) {//非2xx区间状态值走失败 console.log(xhr.statusText) } 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"]; console.log(videoURL); findVideoEle(videoURL); } } // 修改请求头 //xhr.setRequestHeader('key1', 'value1'); }; /** * 重写open方法 * https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/open */ XMLHttpRequest.prototype.myOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function (method, url, async) { // 用对象便于修改参数 var options = { method: method, url: url, async: async, }; if ('function' === typeof window.beforeXMLHttpRequestOpen) { window.beforeXMLHttpRequestOpen(this, options); } this.myOpen(options.method, options.url, options.async); }; /** * 重写send方法 * https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/send */ XMLHttpRequest.prototype.mySend = XMLHttpRequest.prototype.send; XMLHttpRequest.prototype.send = function (body) { if ('function' === typeof window.beforeXMLHttpRequestSend) { window.beforeXMLHttpRequestSend(this, body); } this.mySend(body); }; })();