🏠 Home 

##科学院大学课程网站增强

try to take over the world!


Install this script?
  1. // ==UserScript==
  2. // @name ##科学院大学课程网站增强
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description try to take over the world!
  6. // @author Birkhoff_Lee
  7. // @match https://course.ucas.ac.cn/portal/site/*
  8. // @require http://libs.baidu.com/jquery/1.8.3/jquery.min.js
  9. // @require https://unpkg.com/axios/dist/axios.min.js
  10. // @grant none
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14. // Your code here...
  15. // axios全局设置, 设置带Cookie请求
  16. axios.defaults.withCredentials = true;
  17. window.axios = axios;
  18. // 脚本数据空间
  19. let data = {
  20. hasResourc###pdated: 0,
  21. };
  22. // 获取所有文件的超链接以及索引
  23. const getAllFilesBody = (event) => {
  24. const body = document.getElementsByTagName('tbody')[0];
  25. const selectedFilesList = Array.prototype.slice.call(body.children).map(
  26. (item) => {
  27. if (item.getElementsByTagName('input') &&
  28. item.getElementsByTagName('input')[0] &&
  29. item.getElementsByTagName('input')[0].checked) {
  30. return item;
  31. }
  32. }
  33. ).slice(1);
  34. // 所有可下载文件的链接Array
  35. const ans = new Array();
  36. selectedFilesList.map(
  37. (item) => {
  38. if(item && item.getElementsByTagName('a')[1].attributes.title.textContent.endsWith('文件夹')) {
  39. item = null;
  40. }
  41. return item ? ans.push(
  42. {
  43. url: decodeURI(item.getElementsByTagName('a')[1].href),
  44. filename: item.getElementsByTagName('a')[1].lastElementChild.textContent
  45. }
  46. ) : item
  47. }
  48. )
  49. for (var i in ans) {
  50. downloadWithUri(ans[i].url, ans[i].filename);
  51. }
  52. // 阻止事件冒泡
  53. event && event.stopPropagation();
  54. };
  55. // 通过一个临时的a标签来下载文件
  56. const downloadWithFakeAtag = (href, filename) => {
  57. const a = document.createElement('a');
  58. a.download = filename;
  59. a.href = href;
  60. a.style.display = 'none';
  61. document.body.appendChild(a);
  62. a.click();
  63. a.remove();
  64. };
  65. // 通过文件的url下载文件
  66. const downloadWithUri = (uri, fname = '') => {
  67. console.log('开始下载: ', uri);
  68. window.axios.get(encodeURI(uri), {responseType: 'blob'})
  69. .then((resp) => {
  70. // console.log('文件下载成功啦', resp);
  71. const content = window.URL.createObjectURL(resp.data);
  72. const filename = fname ? fname :decodeURI(resp.config.url).split('/').slice(-1)[0];
  73. downloadWithFakeAtag(content, filename);
  74. })
  75. .catch((error) => {
  76. alert(error);
  77. })
  78. };
  79. // 更新界面视图(包含按钮等元素)
  80. const updateResourcesPage = () => {
  81. const css = document.createElement('style');
  82. css.innerText = `
  83. #birkhoff-download-selected {
  84. display: inline-block;
  85. float: right;
  86. min-width: 10px;
  87. padding: 3px 5px 3px 5px;
  88. height: 100%;
  89. border-radius: 5px;
  90. background-color: pink;
  91. -webkit-user-select: none;
  92. }
  93. #birkhoff-download-selected:hover {
  94. background-color: skyblue;
  95. cursor: pointer;
  96. }
  97. `;
  98. document.getElementsByTagName('head')[0].appendChild(css);
  99. const buttonLocationDiv = document.getElementById('copy-button').parentElement;
  100. const downloadButton = document.createElement('div');
  101. downloadButton.innerHTML = '下载选中项';
  102. downloadButton.id = 'birkhoff-download-selected';
  103. buttonLocationDiv.appendChild(downloadButton);
  104. $('#birkhoff-download-selected').click(() => {
  105. alert("已经发布下载任务, 暂不支持文件夹下的文件下载!");
  106. getAllFilesBody();
  107. })
  108. data.hasResourc###pdated ++;
  109. };
  110. // 检查当前界面是否处于资源界面
  111. const recourseCheck = window.setInterval(() => {
  112. if (document.getElementsByClassName('is-current')[0].firstElementChild.title === '资源') {
  113. // console.log('已经选中资源界面');
  114. if (data.hasResourc###pdated <= 0){
  115. updateResourcesPage();
  116. }
  117. }
  118. }, 1000);
  119. })();