// ==UserScript== // @name 云班课高效助手 // @namespace http://tampermonkey.net/ // @version 1.22 // @description 添加下载按钮,可以按栏缩小范围进行模拟批量点击资源,批量下载资源,提高效率。【基于其他脚本修改(@name 蓝墨云班课(Moso Tech)资源下载;@author xfl03)。】【注意:执行完毕后需刷新页面】【只是出于个人原因开发,只做了chrome适配,其他浏览器可用,但具体操作会有一点不同】 // @author bellamy.n.h // @match https://www.mosoteach.cn/web/index.php* // @grant none // @downloadURL none // ==/UserScript== (function() { 'use strict'; /** * 睡眠函数 * @param numberMillis -- 要睡眠的毫秒数 */ function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMillis; while (true) { now = new Date(); if (now.getTime() > exitTime) return; } } /** *文件下载函数 */ function download(name, href) { var a = document.createElement("a"), //创建a标签 e = document.createEvent("MouseEvents"); //创建鼠标事件对象 e.initEvent("click", false, false); //初始化事件对象 a.href = href; //设置下载地址 a.download = name; //设置下载文件名 a.dispatchEvent(e); //给指定的元素,执行事件click事件 } // css const styleTag = ` `; $(styleTag).appendTo('head'); //为每个资源添加下载按钮 $(".res-row-open-enable").each(function() { if ($(this).find(".download-res-button").length > 0) return;//如果已经存在下载按钮(例如mp3),则不再添加 $(this).find("ul").html('
  • 下载
  • ' + $(this).find("ul").html()); // $(this).find("ul").html('
  • 正序点击
  • ' + $(this).find("ul").html()); // $(this).find("ul").html('
  • 倒序点击
  • ' + $(this).find("ul").html()); }); //单个资源下载 $(document).on('click', '.download-ress', function() { var resHref = $(this).parents(".res-row-open-enable").attr('data-href'); window.open(resHref); }); // 模拟点击 part $('
    \
    \
    \
    \
    \ 功能区 \ Powered by \ 云班课高效助手\ \
    \
    \
    \ \ \ \
    \
    \
    \
    \
    \ 资源区 \
    \
    \ ').insertAfter("#res-view-way"); // 初始化 $("#module-1,#module-2").css("display","none"); $("#confirm, #downloadSrc, #mode-click, #mode-download").css("display","inline"); // change mode $(document).on('click','#mode-click',function(){ $("#module-1, #module-2").css("display","block"); // 等价于 // document.getElementById("module-1").style.display="block"; // document.getElementById("module-2").style.display="block"; // document.getElementById('confirm').style.display = document.getElementById('confirm').style.display=="inline"?"inline":"none"; $("#downloadSrc, #mode-download").css("display","none"); // $("#mode-click").css({"background-color":"#0BD","color":"#fff"}); $("#modeName").text("模拟点击"); alert("操作提醒:\n"+"务必操作,否则请不要向下执行任何操作!!!\n" + "\n" + "(以下只是 chrome 浏览器操作步骤)" + "\n" + " 1. 新建 Tab 页\n"+" -->\n"+" 2. 地址栏输入: chrome://settings/?search=downloads\n" +" -->\n" + " 3. 打开 “下载前询问每个文件的保存位置” 右侧按钮"); }); $(document).on('click','#mode-download',function(){ document.getElementById("module-1").style.display="block"; $("#module-2, #confirm, #mode-click").css("display","none"); // $("#mode-download").css({"background-color":"#0BD","color":"#fff"}); $("#modeName").text("批量下载"); alert("操作提醒:\n"+"务必操作,否则请不要向下执行任何操作!!!\n" + "\n" + "(以下只是 chrome 浏览器操作步骤)" + "\n" + " 1. 新建 Tab 页\n"+" -->\n"+" 2. 地址栏输入:chrome://settings/?search=downloads\n" +" -->\n" + " 3. 关闭 “下载前询问每个文件的保存位置” 右侧按钮"); }); $(document).on('click','#reset',function(){ $("#module-1,#module-2").css("display","none"); $("#confirm, #downloadSrc, #mode-click, #mode-download").css("display","inline"); // $("#mode-download, #mode-click").css({"background-color":"#fff","color":"#000"}); $("#modeName").text("未选择"); }); // 刷新 $(document).on('click','#refresh',function(){location.reload()}) // 给分栏添加 id 易于按栏操作 $(".res-row-box").each(function(i,e){$(this).attr('id','id_' + i)}); var chosenID = ".res-row-box"; $(document).on('click','#choose',function(){ switch($("#bar_index").val()){ case "1":chosenID = "#id_0";break; case "2":chosenID = "#id_1";break; case "3":chosenID = "#id_2";break; case "4":chosenID = "#id_3";break; case "5":chosenID = "#id_4";break; case "6":chosenID = "#id_5";break; case "7":chosenID = "#id_6";break; case "8":chosenID = "#id_7";break; case "9":chosenID = "#id_8";break; case "10":chosenID = "#id_9";break; case "11":chosenID = "#id_10";break; case "12":chosenID = "#id_11";break; case "13":chosenID = "#id_12";break; case "14":chosenID = "#id_13";break; case "15":chosenID = "#id_14";break; case "16":chosenID = "#id_15";break; case "17":chosenID = "#id_16";break; case "18":chosenID = "#id_17";break; case "19":chosenID = "#id_18";break; case "20":chosenID = "#id_19";break; default:chosenID = ".res-row-box"; } var barID = $("#bar_index").val(); var barID_str = (barID > 0 && barID < 21) ? barID : "全选"; alert("小可爱,你已将要操作的资源栏修改为: "+ barID_str); $("#barID").text(barID_str); console.log(chosenID + "映射值 <-- 输入值" + $("#bar_index").val()); }) // 指定下标区间,进行模拟点击(用于资源量较大,有漏掉的情况) $(document).on('click', '#confirm', function() { var conf_str = false; conf_str = confirm("小可爱,你即将执行“模拟点击”操作!!!" + "\n\n"+"根据选择资源数量的不同,会打开相应数量的页面,如果数量较多,请不要惊慌,因为这些页面会自动关闭的哦!!!"+ "\n\n" + "你是否按照上一个提示,进行了相应的操作?" + "\n\n" + "如果是,你是否要开始执行本次操作?"); if(conf_str){ // 以下五个等价,实现相同功能,但写法是逐步优化 // var list = document.getElementsByClassName("res-row-open-enable"); // var list = $(".res-row-open-enable"); // var list = $(".hide-div").children(); // var list = $(".res-row-box").children(".hide-div").children(); var list = $(chosenID).children(".hide-div").children(); var succNum = 0; var failNum = 0; var tempUrl; var win; var startIndex = $("#head").val(); var endIndex = $("#tail").val(); var actualStartIndex = startIndex < list.length ? startIndex : list.length;//输入值超出资源总数的值,则将输入值置为总数的值 var actualEndIndex = endIndex < list.length ? endIndex : list.length;//输入值超出资源总数的值,则将输入值置为总数的值 for (var i = actualStartIndex-1; i < actualEndIndex; i++){ try{ try{ tempUrl = list[i].getAttribute("data-href"); }catch(e){ console.log(e.name + ": " + e.message ); alert("输入的资源栏数不在该页资源栏数的范围内,无法执行,请重新选择资源栏"); break; } win = window.open(tempUrl); sleep(100);//睡眠,是为了确保每个资源都被正常获取 win.close(); succNum++; console.log(tempUrl); }catch(e){ console.log(e.name + ": " + e.message ); console.log("该条未成功执行 ;URL : "+ list[i].getAttribute("data-href")); failNum++; continue; } } // location.reload(); console.log("共检索到 "+ list.length + "条; 成功执行 " + succNum + " 次! 失败 " + failNum + " 次! 操作范围:从第 " + actualStartIndex + " 条 至 第 " + actualEndIndex +" 条。"); $(".indexNum").val(""); alert("操作完成,请小可爱刷新页面查看结果!!!"); }else{ alert("已取消操作!"); } }); // 模拟正序点击全部资源 $(document).on('click', '.forward', function() { var conf_str = false; conf_str = confirm("小可爱,你即将执行“正序点击全部资源”操作,如果资源量较大(> 1000),耗时就会较久,打开的页面也会较多哦!不过都会自动关闭的哦!!!" + "\n\n" + "小可爱,资源较多时,还请三思啊!!!" + "\n\n" + "你是否要执行?"); if(conf_str){ var list = document.getElementsByClassName("res-row-open-enable"); var succNum = 0; var failNum = 0; var tempUrl; var win; for (var i = 0; i < list.length; i++){ try{ tempUrl = list[i].getAttribute("data-href"); win = window.open(tempUrl); sleep(100);//睡眠,是为了确保每个资源都被正常获取 win.close(); succNum++; console.log(tempUrl); }catch(e){ console.log(e.name + ": " + e.message ); console.log("该条未成功执行 ;URL : "+ list[i].getAttribute("data-href")); failNum++; continue; } } console.log("正序点击: 共检索到 "+ list.length + "条; 成功执行 " + succNum + " 次! 失败 " + failNum + " 次!" ); alert("操作完成,请小可爱刷新页面查看结果!!!"); }else{ alert("已取消操作!"); } }); // 模拟倒序点击全部资源 $(document).on('click', '.reverse', function() { var conf_str = false; conf_str = confirm("小可爱,你即将执行“倒序点击全部资源”操作,如果资源量较大(> 1000),耗时就会较久,打开的页面也会较多哦!不过都会自动关闭的哦!!!" + "\n\n" + "小可爱,资源较多时,还请三思啊!!!" + "\n\n" + "你是否要执行?"); if(conf_str){ var list = document.getElementsByClassName("res-row-open-enable"); var succNum = 0; var failNum = 0; var tempUrl; var win; for (var i = list.length - 1; i >= 0; i--){ try{ tempUrl = list[i].getAttribute("data-href"); win = window.open(tempUrl); sleep(100);//睡眠,是为了确保每个资源都被正常获取 win.close(); succNum++; console.log(tempUrl); }catch(e){ console.log(e.name + ": " + e.message ); console.log("该条未成功执行 ;URL : "+ list[i].getAttribute("data-href")); failNum++; continue; } } console.log("倒序点击: 共检索到 "+ list.length + "条; 成功执行 " + succNum + " 次! 失败 " + failNum + " 次!" ); alert("操作完成,请小可爱刷新页面查看结果!!!"); }else{ alert("已取消操作!"); } }); // 指定下标区间,进行批量下载 $(document).on('click', '#downloadSrc', function() { var conf_str = false; conf_str = confirm("小可爱,你即将执行“批量下载”操作!!!"+"\n\n"+"根据选择资源数量的不同,会打开相应数量的页面,如果数量较多,请不要惊慌,因为这些页面会自动关闭的哦!!!" + "\n\n" + "你是否按照上一个提示,进行了相应的操作?" + "\n\n" + "如果是,你是否要开始执行本次操作?"); if(conf_str){ // var list = document.getElementsByClassName("res-row-open-enable"); var list = $(chosenID).children(".hide-div").children(); var succNum = 0; var failNum = 0; var tempUrl; var win; var startIndex = $("#head").val(); var endIndex = $("#tail").val(); var actualStartIndex = startIndex < list.length ? startIndex : list.length;//输入值超出资源总数的值,则将输入值置为总数的值 var actualEndIndex = endIndex < list.length ? endIndex : list.length;//输入值超出资源总数的值,则将输入值置为总数的值 for (var i = actualStartIndex-1; i < actualEndIndex; i++){ try{ try{ tempUrl = list[i].getAttribute("data-href"); }catch(e){ console.log(e.name + ": " + e.message ); alert("输入的资源栏数不在该页资源栏数的范围内,无法执行,请重新选择资源栏"); break; } // download('第' + i+1 + '个文件', tempUrl); win = window.open(tempUrl); succNum++; console.log(tempUrl); }catch(e){ console.log(e.name + ": " + e.message ); console.log("该条未成功执行 ;URL : "+ list[i].getAttribute("data-href")); failNum++; continue; } } console.log("共检索到 "+ list.length + "条; 成功执行 " + succNum + " 次! 失败 " + failNum + " 次! 操作范围:从第 " + actualStartIndex + " 条 至 第 " + actualEndIndex +" 条。"); $(".indexNum").val(""); alert("操作完成,请小可爱刷新页面查看结果!!!"); }else{ alert("已取消操作!"); } }); })();