// ==UserScript== // @name 云班课高效助手 // @namespace http://tampermonkey.net/ // @version 1.03 // @description 基于其他脚本修改(@name 蓝墨云班课(Moso Tech)资源下载;@author xfl03),实现 模拟批量点击资源,批量下载资源,提高效率。【只是出于个人原因开发,所以只适配chrome,其他浏览器能用,但具体操作可能会有一点不同】(就是个js小白为了逃避"老湿"的"骚操作(出于某种利益,发了700+ 带经验值的资源)",而做出回应) // @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 $('
    \
    \
    \ 当前模式: \ 未选择 |\ ( 选择模式后,请按照提示操作,否则会出错)\ \
    \ \
    \
    \
    \ 模拟批量点击/下载\ (范围:以资源总数值作为范围最大值)\ ( 点击对应按钮,将打开较多页面,请耐心等待其自动关闭。可在“控制台”里查看运行日志)\ \
    \ \
    \
    \
    \
    \ 模拟全部点击(耗时较长)\ (范围:所有资源)\ ( 点击后,将会自动打开较多页面,请耐心等待其自动关闭。可在“控制台(F12 -> console)”里查看运行日志)\ \
    \ \
    \
    ').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', '#confirm', function() { var list = document.getElementsByClassName("res-row-open-enable"); 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{ tempUrl = list[i].getAttribute("data-href"); win = window.open(tempUrl); sleep(100);//睡眠,是为了确保每个资源都被正常获取 win.close(); succNum++; console.log(tempUrl); console.log("该条未成功执行 ;URL : "+ list[i].getAttribute("data-href")); }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(""); }); // 模拟正序点击全部资源 $(document).on('click', '.forward', function() { 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 + " 次!" ); }); // 模拟倒序点击全部资源 $(document).on('click', '.reverse', function() { 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 + " 次!" ); }); // 指定下标区间,进行批量下载(用于资源量较大,有漏掉的情况) $(document).on('click', '#downloadSrc', function() { var list = document.getElementsByClassName("res-row-open-enable"); 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{ tempUrl = list[i].getAttribute("data-href"); // 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(""); }); })();