超星 To Csv

将你的超星学习通里的作业数据、随堂练习导出成为 Csv 文件,方便导入 Anki 背题

  1. // ==UserScript==
  2. // @name 超星 To Csv
  3. // @version 0.2.0
  4. // @description 将你的超星学习通里的作业数据、随堂练习导出成为 Csv 文件,方便导入 Anki 背题
  5. // @author Nexmoe
  6. // @github https://github.com/nexmoe/chaoxing2csv
  7. // @namespace https://nexmoe.com/
  8. // @match *://*.chaoxing.com/*
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. const getPage = () => {
  13. if (document.querySelector(".detailsHead")) {
  14. return "homework";
  15. } else if (document.querySelector(".top-box")) {
  16. return "practice";
  17. } else if (document.querySelector(".CeYan")) {
  18. return "ceyan";
  19. } else {
  20. return "invalid";
  21. }
  22. }
  23.  
  24. const page = getPage();
  25.  
  26. const chaoxing2csv = {
  27. typeList: [
  28. "单选题",
  29. "多选题",
  30. "判断题",
  31. ],
  32. typeToKey: {
  33. "(单选题)": "select",
  34. "(多选题)": "select",
  35. "(判断题)": "select",
  36. },
  37. homework: {
  38. type: ".mark_name .colorShallow",
  39. title: ".mark_title",
  40. textarea: ".detailsHead",
  41. list: ".questionLi",
  42. select: {
  43. question: ".mark_name",
  44. options: ".mark_letter",
  45. options2: ".stem_answer",
  46. answer: ".colorGreen",
  47. answer2: ".check_answer"
  48. },
  49. },
  50. practice: {
  51. type: ".topic-title .topic-type",
  52. title: ".textHeightauto",
  53. textarea: ".top-box",
  54. list: ".question-list",
  55. select: {
  56. question: ".topic-title",
  57. options: ".topic-options",
  58. answer: ".color-green"
  59. },
  60. },
  61. ceyan: {
  62. title: ".ZyTop h3",
  63. textarea: ".ZyTop",
  64. list: ".TiMu",
  65. }
  66. }
  67.  
  68. const generateTextArea = (data) => {
  69. let node = document.createElement("textarea");
  70. node.style = `
  71. width: 100%;
  72. resize: none;
  73. padding: 12px;
  74. background-color: #171717;
  75. color: #fff;
  76. box-sizing: border-box;
  77. height: 70px;`;
  78. let textnode = document.createTextNode(data);
  79. node.appendChild(textnode);
  80.  
  81. document.querySelector(chaoxing2csv[page].textarea).appendChild(node);
  82. }
  83.  
  84. function ConvertToCSV(objArray) {
  85. var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
  86. var str = '';
  87.  
  88. for (var i = 0; i < array.length; i++) {
  89. var line = '';
  90. for (var index in array[i]) {
  91. if (line != '') line += ','
  92.  
  93. line += array[i][index];
  94. }
  95.  
  96. str += line + '\r\n';
  97. }
  98.  
  99. return str;
  100. }
  101.  
  102. let saveFile = (data) => {
  103. const blob = new Blob([data], { type: 'text/csv;charset=utf-8;' });
  104. const Url = URL.createObjectURL(blob);
  105. let link = document.createElement('a');
  106. link.href = Url;
  107. link.appendChild(document.createTextNode('下载题目 CSV 数据'));
  108.  
  109. link.download = document.querySelector(chaoxing2csv[page].title).innerHTML + '.csv'; //文件名字
  110. document.querySelector(chaoxing2csv[page].textarea).appendChild(link);
  111. }
  112.  
  113. let optionsFilter = (dom, i = 0, strings = "") => {
  114. for (i = 0; i < dom.children.length; i++) {
  115. if (i < (dom.children.length - 1)) {
  116. strings = strings + dom.children[i].innerText + "||";
  117. } else {
  118. strings = strings + dom.children[i].innerText;
  119. }
  120. }
  121. return strings.replace(
  122. new RegExp(",", "gm"),
  123. ","
  124. ).replace(
  125. new RegExp("\n", "gm"),
  126. ""
  127. );
  128. }
  129.  
  130. let answersFilter = (dom, i = 0, strings = "") => {
  131. try {
  132. answer = dom.innerText.replace("正确答案: ", "").replace(
  133. new RegExp(",", "gm"),
  134. ","
  135. ).replace(
  136. new RegExp(" ", "gm"),
  137. ""
  138. ).replace(
  139. new RegExp("对", "gm"),
  140. "A"
  141. ).replace(
  142. new RegExp("错", "gm"),
  143. "B"
  144. );
  145.  
  146. for (i = 0; i < answer.length; i++) {
  147. if (i < (answer.length - 1)) {
  148. strings = strings + (answer.charCodeAt(i) - 64) + "||"
  149. } else {
  150. strings = strings + (answer.charCodeAt(i) - 64)
  151. }
  152.  
  153. }
  154. } catch (e) {
  155. strings = dom.innerHTML;
  156. console.error(e)
  157. }
  158.  
  159. return strings;
  160. }
  161.  
  162. let questionsFilter = (dom, answers, i = 0, strings = "") => {
  163. let question;
  164. try {
  165. question = dom.innerHTML.replace(
  166. new RegExp(",", "gm"),
  167. ","
  168. ).replace(
  169. new RegExp("\n", "gm"),
  170. ""
  171. ).replace(
  172. new RegExp("http://", "gm"),
  173. "https://"
  174. );
  175. answer = answers.innerText.replace("正确答案: ", "").replace(
  176. new RegExp(",", "gm"),
  177. ","
  178. ).replace(
  179. new RegExp(" ", "gm"),
  180. ""
  181. ).replace(
  182. new RegExp("对", "gm"),
  183. "A"
  184. ).replace(
  185. new RegExp("错", "gm"),
  186. "B"
  187. );
  188.  
  189. for (i = 0; i < answer.split('\n').length; i++) {
  190. strings = strings + "{{c" + (i + 1) + "::}}"
  191. }
  192. } catch (e) {
  193. console.error(e)
  194. }
  195.  
  196. return question + strings;
  197. }
  198.  
  199. let getData = (i, data = []) => {
  200. let questionList = document.querySelectorAll(chaoxing2csv[page].list);
  201.  
  202. for (let child of questionList) {
  203. let type, question, options, answer, questItem;
  204. type = child.querySelector(chaoxing2csv[page].type).innerText.replace(" ", "");
  205. for (let i = 0; i < chaoxing2csv.typeList.length; i++) {
  206. if (!type.includes(chaoxing2csv.typeList[i])) {
  207. console.log("不支持:", type)
  208. continue;
  209. } else {
  210. type = "select";
  211. }
  212. }
  213.  
  214. const qDom = child.querySelector(chaoxing2csv[page][type].question);
  215. const oDom = child.querySelector(chaoxing2csv[page][type].options) || child.querySelector(chaoxing2csv[page][type].options2);
  216. const aDom = child.querySelector(chaoxing2csv[page][type].answer) || child.querySelector(chaoxing2csv[page][type].answer2);
  217. try {
  218. question = questionsFilter(qDom, aDom);
  219. } catch (e) {
  220. console.error(e)
  221. }
  222. try {
  223. options = optionsFilter(oDom);
  224. } catch (e) {
  225. console.error(e)
  226. }
  227. try {
  228. answer = answersFilter(aDom);
  229. } catch (e) {
  230. console.error(e)
  231. }
  232. questItem = {
  233. question,
  234. options,
  235. answer,
  236. id: String(new Date().getTime()) + String(Math.floor(Math.random() * 100000)),
  237. type,
  238. }
  239. console.log(questItem)
  240. data.push(questItem)
  241. }
  242.  
  243. return data;
  244. }
  245.  
  246. (() => {
  247. for (let child of document.querySelectorAll(".questionLi *")) {
  248. child.style = "";
  249. delete child.style;
  250. }
  251. setTimeout(() => {
  252. console.log(page)
  253. if (page !== "invalid") {
  254. const data = JSON.stringify(getData());
  255. generateTextArea(ConvertToCSV(data));
  256. saveFile(ConvertToCSV(data));
  257. }
  258. }, 1000)
  259. })();