ucloud-Evolved

主页作业显示所属课程,使用Office 365预览课件,增加通知显示数量,通知按时间排序,去除悬浮窗,解除复制限制,课件自动下载,批量下载,资源页展示全部下载按钮,更好的页面标题

当前为 2025-04-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ucloud-Evolved
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.30
  5. // @description 主页作业显示所属课程,使用Office 365预览课件,增加通知显示数量,通知按时间排序,去除悬浮窗,解除复制限制,课件自动下载,批量下载,资源页展示全部下载按钮,更好的页面标题
  6. // @author Quarix
  7. // @match https://ucloud.bupt.edu.cn/*
  8. // @match https://ucloud.bupt.edu.cn/uclass/course.html*
  9. // @match https://ucloud.bupt.edu.cn/uclass/*
  10. // @match https://ucloud.bupt.edu.cn/office/*
  11. // @icon https://ucloud.bupt.edu.cn/favicon.ico
  12. // @require https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/nprogress/0.2.0/nprogress.min.js#sha256-XWzSUJ+FIQ38dqC06/48sNRwU1Qh3/afjmJ080SneA8=
  13. // @resource NPROGRESS_CSS https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/nprogress/0.2.0/nprogress.min.css#sha256-pMhcV6/TBDtqH9E9PWKgS+P32PVguLG8IipkPyqMtfY=
  14. // @connect github.com
  15. // @grant GM_getResourceText
  16. // @grant GM_addStyle
  17. // @grant GM_setValue
  18. // @grant GM_getValue
  19. // @grant GM_registerMenuCommand
  20. // @grant GM_xmlhttpRequest
  21. // @grant GM_openInTab
  22. // @run-at document-start
  23. // @license MIT
  24. // ==/UserScript==
  25.  
  26. (function () {
  27. if (location.href.startsWith("https://ucloud.bupt.edu.cn/office/")) {
  28. if (
  29. GM_getValue("preview_autoSwitchOffice", true) ||
  30. GM_getValue("preview_autoSwitchPdf", true) ||
  31. GM_getValue("preview_autoSwitchImg", true)
  32. ) {
  33. const url = new URLSearchParams(location.search).get("furl");
  34. const filename =
  35. new URLSearchParams(location.search).get("fullfilename") || url;
  36. const viewURL = new URL(url);
  37. if (new URLSearchParams(location.search).get("oauthKey")) {
  38. const viewURLsearch = new URLSearchParams(viewURL.search);
  39. viewURLsearch.set(
  40. "oauthKey",
  41. new URLSearchParams(location.search).get("oauthKey")
  42. );
  43. viewURL.search = viewURLsearch.toString();
  44. }
  45. if (
  46. filename.endsWith(".xls") ||
  47. filename.endsWith(".xlsx") ||
  48. filename.endsWith(".doc") ||
  49. filename.endsWith(".docx") ||
  50. filename.endsWith(".ppt") ||
  51. filename.endsWith(".pptx")
  52. ) {
  53. if (!GM_getValue("preview_autoSwitchOffice", true)) {
  54. return;
  55. }
  56. if (window.stop) window.stop();
  57. location.href =
  58. "https://view.officeapps.live.com/op/view.aspx?src=" +
  59. encodeURIComponent(viewURL.toString());
  60. return;
  61. } else if (filename.endsWith(".pdf")) {
  62. if (!GM_getValue("preview_autoSwitchPdf", true)) {
  63. return;
  64. }
  65. if (window.stop) window.stop();
  66. // 使用浏览器内置预览器,转blob避免出现下载动作
  67. fetch(viewURL.toString())
  68. .then((response) => response.blob())
  69. .then((blob) => {
  70. const blobUrl = URL.createObjectURL(blob);
  71. location.href = blobUrl;
  72. })
  73. .catch((err) => console.error("PDF加载失败:", err));
  74. return;
  75. } else if (
  76. filename.endsWith(".jpg") ||
  77. filename.endsWith(".png") ||
  78. filename.endsWith(".jpeg") ||
  79. filename.endsWith(".gif") ||
  80. filename.endsWith(".webp") ||
  81. filename.endsWith(".bmp") ||
  82. filename.endsWith(".tiff") ||
  83. filename.endsWith(".svg")
  84. ) {
  85. if (!GM_getValue("preview_autoSwitchImg", true)) {
  86. return;
  87. }
  88. if (window.stop) window.stop();
  89. function createModernImageViewer(imageUrl) {
  90. const style = document.createElement("style");
  91. style.textContent = `
  92. .modern-image-viewer {
  93. position: fixed;
  94. top: 0;
  95. left: 0;
  96. width: 100%;
  97. height: 100%;
  98. background-color: rgba(0, 0, 0, 0.9);
  99. z-index: 9999;
  100. display: flex;
  101. flex-direction: column;
  102. color: white;
  103. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  104. }
  105. .viewer-header {
  106. display: flex;
  107. justify-content: space-between;
  108. align-items: center;
  109. padding: 12px 20px;
  110. background-color: rgba(0, 0, 0, 0.7);
  111. z-index: 1;
  112. }
  113. .viewer-title {
  114. font-size: 16px;
  115. font-weight: 500;
  116. white-space: nowrap;
  117. overflow: hidden;
  118. text-overflow: ellipsis;
  119. max-width: 70%;
  120. }
  121. .viewer-controls {
  122. display: flex;
  123. gap: 15px;
  124. }
  125. .viewer-button {
  126. background: none;
  127. border: none;
  128. color: white;
  129. cursor: pointer;
  130. font-size: 16px;
  131. padding: 5px;
  132. border-radius: 4px;
  133. display: flex;
  134. align-items: center;
  135. justify-content: center;
  136. transition: background-color 0.2s;
  137. }
  138. .viewer-button:hover {
  139. background-color: rgba(255, 255, 255, 0.1);
  140. }
  141. .viewer-content {
  142. flex: 1;
  143. display: flex;
  144. align-items: center;
  145. justify-content: center;
  146. position: relative;
  147. overflow: hidden;
  148. }
  149. .viewer-image {
  150. max-width: 100%;
  151. max-height: 100%;
  152. object-fit: contain;
  153. transform-origin: center center;
  154. transition: transform 0.05s linear;
  155. cursor: grab;
  156. }
  157. .viewer-image.dragging {
  158. cursor: grabbing;
  159. transition: none;
  160. }
  161. .viewer-toolbar {
  162. display: flex;
  163. justify-content: center;
  164. align-items: center;
  165. padding: 12px;
  166. background-color: rgba(0, 0, 0, 0.7);
  167. gap: 20px;
  168. }
  169. .zoom-level {
  170. font-size: 14px;
  171. min-width: 60px;
  172. text-align: center;
  173. }
  174. .viewer-help {
  175. position: absolute;
  176. bottom: 80px;
  177. left: 50%;
  178. transform: translateX(-50%);
  179. background-color: rgba(0, 0, 0, 0.7);
  180. padding: 15px 20px;
  181. border-radius: 8px;
  182. max-width: 400px;
  183. font-size: 14px;
  184. display: none;
  185. z-index: 2;
  186. }
  187. .viewer-help h3 {
  188. margin-top: 0;
  189. margin-bottom: 10px;
  190. font-size: 16px;
  191. }
  192. .viewer-help ul {
  193. margin: 0;
  194. padding-left: 20px;
  195. }
  196. .viewer-help li {
  197. margin-bottom: 5px;
  198. }
  199. .keyboard-shortcut {
  200. display: inline-block;
  201. background-color: rgba(255, 255, 255, 0.1);
  202. padding: 2px 6px;
  203. border-radius: 3px;
  204. margin: 0 2px;
  205. }
  206. @media (max-width: 768px) {
  207. .viewer-controls {
  208. gap: 10px;
  209. }
  210. .viewer-button {
  211. font-size: 14px;
  212. }
  213. .viewer-toolbar {
  214. padding: 10px;
  215. gap: 15px;
  216. }
  217. }
  218. `;
  219. document.head.appendChild(style);
  220.  
  221. // 创建预览器DOM结构
  222. document.body.innerHTML = `
  223. <div class="modern-image-viewer">
  224. <div class="viewer-header">
  225. <div class="viewer-title">${getImageFileName(imageUrl)}</div>
  226. <div class="viewer-controls">
  227. <button class="viewer-button" id="help-btn" title="帮助">
  228. <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
  229. <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm-1-4h2v2h-2zm2.07-7.75l-.9.92c-.5.51-.86.97-1.04 1.69-.08.32-.13.68-.13 1.14h2c0-.47.08-.91.22-1.31.2-.58.53-.97.98-1.42l.9-.92c.35-.36.58-.82.58-1.35 0-1.1-.9-2-2-2s-2 .9-2 2h2c0-.55.45-1 1-1s1 .45 1 1c0 .28-.12.53-.31.72z"/>
  230. </svg>
  231. </button>
  232. <button class="viewer-button" id="download-btn" title="下载">
  233. <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
  234. <path d="M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"/>
  235. </svg>
  236. </button>
  237. </div>
  238. </div>
  239. <div class="viewer-content">
  240. <img id="viewer-img" class="viewer-image" src="${imageUrl}" alt="预览图片" draggable="false">
  241. </div>
  242. <div class="viewer-toolbar">
  243. <button class="viewer-button" id="rotate-left" title="向左旋转">
  244. <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
  245. <path d="M7.11 8.53L5.7 7.11C4.8 8.27 4.24 9.61 4.07 11h2.02c.14-.87.49-1.72 1.02-2.47zM6.09 13H4.07c.17 1.39.72 2.73 1.62 3.89l1.41-1.42c-.52-.75-.87-1.59-1.01-2.47zm1.01 5.32c1.16.9 2.51 1.44 3.9 1.61V17.9c-.87-.15-1.71-.49-2.46-1.03L7.1 18.32zM13 4.07V1L8.45 5.55 13 10V6.09c2.84.48 5 2.94 5 5.91s-2.16 5.43-5 5.91v2.02c3.95-.49 7-3.85 7-7.93s-3.05-7.44-7-7.93z"/>
  246. </svg>
  247. </button>
  248. <button class="viewer-button" id="zoom-out" title="缩小">
  249. <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
  250. <path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14zM7 9h5v1H7z"/>
  251. </svg>
  252. </button>
  253. <span class="zoom-level" id="zoom-level">100%</span>
  254. <button class="viewer-button" id="zoom-in" title="放大">
  255. <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
  256. <path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/>
  257. <path d="M12 10h-2v2H9v-2H7V9h2V7h1v2h2z"/>
  258. </svg>
  259. </button>
  260. <button class="viewer-button" id="zoom-reset" title="重置">
  261. <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
  262. <path d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"/>
  263. </svg>
  264. </button>
  265. <button class="viewer-button" id="rotate-right" title="向右旋转">
  266. <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
  267. <path d="M15.55 5.55L11 1v3.07C7.06 4.56 4 7.92 4 12s3.05 7.44 7 7.93v-2.02c-2.84-.48-5-2.94-5-5.91s2.16-5.43 5-5.91V10l4.55-4.45zM19.93 11c-.17-1.39-.72-2.73-1.62-3.89l-1.42 1.42c.54.75.88 1.6 1.02 2.47h2.02zM13 17.9v2.02c1.39-.17 2.74-.71 3.9-1.61l-1.44-1.44c-.75.54-1.59.89-2.46 1.03zm3.89-2.42l1.42 1.41c.9-1.16 1.45-2.5 1.62-3.89h-2.02c-.14.87-.48 1.72-1.02 2.48z"/>
  268. </svg>
  269. </button>
  270. </div>
  271. <div class="viewer-help" id="help-panel">
  272. <h3>键盘快捷键</h3>
  273. <ul>
  274. <li><span class="keyboard-shortcut">+</span> 或 <span class="keyboard-shortcut">-</span> 放大/缩小</li>
  275. <li><span class="keyboard-shortcut">0</span> 重置缩放</li>
  276. <li><span class="keyboard-shortcut">←</span> <span class="keyboard-shortcut">→</span> 左右旋转</li>
  277. <li><span class="keyboard-shortcut">R</span> 重置所有变换</li>
  278. <li><span class="keyboard-shortcut">D</span> 下载图片</li>
  279. <li><span class="keyboard-shortcut">Esc</span> 关闭预览器</li>
  280. </ul>
  281. </div>
  282. </div>
  283. `;
  284.  
  285. document.body.style.overflow = "hidden";
  286.  
  287. // 获取元素引用
  288. const viewer = document.querySelector(".modern-image-viewer");
  289. const image = document.getElementById("viewer-img");
  290. const helpBtn = document.getElementById("help-btn");
  291. const helpPanel = document.getElementById("help-panel");
  292. const downloadBtn = document.getElementById("download-btn");
  293. const rotateLeftBtn = document.getElementById("rotate-left");
  294. const rotateRightBtn = document.getElementById("rotate-right");
  295. const zoomInBtn = document.getElementById("zoom-in");
  296. const zoomOutBtn = document.getElementById("zoom-out");
  297. const zoomResetBtn = document.getElementById("zoom-reset");
  298. const zoomLevelDisplay = document.getElementById("zoom-level");
  299.  
  300. // 图片变换状态
  301. const state = {
  302. scale: 1,
  303. rotation: 0,
  304. translateX: 0,
  305. translateY: 0,
  306. dragging: false,
  307. lastX: 0,
  308. lastY: 0,
  309. loaded: false,
  310. };
  311.  
  312. // 图片加载完成事件
  313. image.onload = () => {
  314. state.loaded = true;
  315. image.style.opacity = 1;
  316. applyTransform();
  317. };
  318.  
  319. // 应用变换
  320. function applyTransform() {
  321. image.style.transform = `translate(${state.translateX}px, ${state.translateY}px) rotate(${state.rotation}deg) scale(${state.scale})`;
  322. zoomLevelDisplay.textContent = `${Math.round(state.scale * 100)}%`;
  323. }
  324.  
  325. // 缩放图片
  326. function zoom(delta) {
  327. if (!state.loaded) return;
  328.  
  329. const oldScale = state.scale;
  330. state.scale = Math.max(0.1, Math.min(10, state.scale + delta));
  331.  
  332. // Only adjust position if we have mouse coordinates
  333. if (state.lastClientX !== undefined) {
  334. const imageRect = image.getBoundingClientRect();
  335.  
  336. // Get the position relative to the image's natural center
  337. const naturalWidth = image.naturalWidth;
  338. const naturalHeight = image.naturalHeight;
  339.  
  340. // Calculate the point on the original image that was under the cursor
  341. const viewportX = state.lastClientX - imageRect.left;
  342. const viewportY = state.lastClientY - imageRect.top;
  343.  
  344. // Convert to coordinates relative to the image center in the current scale
  345. const imageX = viewportX - imageRect.width / 2;
  346. const imageY = viewportY - imageRect.height / 2;
  347.  
  348. // Calculate how this point's position changes with the new scale
  349. const scaleDiff = state.scale - oldScale;
  350.  
  351. // Adjust translation to keep the point under cursor
  352. state.translateX -= (imageX * scaleDiff) / oldScale;
  353. state.translateY -= (imageY * scaleDiff) / oldScale;
  354. }
  355.  
  356. applyTransform();
  357. }
  358.  
  359. // 旋转图片
  360. function rotate(degrees) {
  361. if (!state.loaded) return;
  362. state.rotation = (state.rotation + degrees) % 360;
  363. applyTransform();
  364. }
  365.  
  366. // 重置变换
  367. function resetTransform() {
  368. if (!state.loaded) return;
  369. state.scale = 1;
  370. state.rotation = 0;
  371. state.translateX = 0;
  372. state.translateY = 0;
  373. applyTransform();
  374. }
  375.  
  376. // 下载图片
  377. function downloadImage() {
  378. const a = document.createElement("a");
  379. a.href = imageUrl;
  380. a.download = getImageFileName(imageUrl);
  381. document.body.appendChild(a);
  382. a.click();
  383. document.body.removeChild(a);
  384. }
  385.  
  386. // 从URL中提取文件名
  387. function getImageFileName(url) {
  388. try {
  389. const urlObj = new URL(url);
  390. const pathParts = urlObj.pathname.split("/");
  391. const fileName = decodeURIComponent(
  392. pathParts[pathParts.length - 1]
  393. );
  394. return fileName || "图片预览";
  395. } catch (e) {
  396. return "图片预览";
  397. }
  398. }
  399.  
  400. image.addEventListener("mousedown", (e) => {
  401. if (!state.loaded) return;
  402. e.preventDefault();
  403. state.dragging = true;
  404. state.lastX = e.clientX;
  405. state.lastY = e.clientY;
  406. image.classList.add("dragging");
  407. });
  408.  
  409. document.addEventListener("mousemove", (e) => {
  410. state.lastClientX = e.clientX;
  411. state.lastClientY = e.clientY;
  412.  
  413. if (!state.dragging) return;
  414. e.preventDefault();
  415.  
  416. state.translateX += e.clientX - state.lastX;
  417. state.translateY += e.clientY - state.lastY;
  418. state.lastX = e.clientX;
  419. state.lastY = e.clientY;
  420.  
  421. applyTransform();
  422. });
  423.  
  424. document.addEventListener("mouseup", () => {
  425. state.dragging = false;
  426. image.classList.remove("dragging");
  427. });
  428.  
  429. let lastTouchDistance = 0;
  430. let touchRotationStart = 0;
  431.  
  432. image.addEventListener(
  433. "touchstart",
  434. (e) => {
  435. if (!state.loaded) return;
  436. if (e.touches.length === 1) {
  437. e.preventDefault();
  438. state.dragging = true;
  439. state.lastX = e.touches[0].clientX;
  440. state.lastY = e.touches[0].clientY;
  441. image.classList.add("dragging");
  442. } else if (e.touches.length === 2) {
  443. const dx = e.touches[0].clientX - e.touches[1].clientX;
  444. const dy = e.touches[0].clientY - e.touches[1].clientY;
  445. const touchDistance = Math.sqrt(dx * dx + dy * dy);
  446. const scaleFactor = touchDistance / lastTouchDistance;
  447.  
  448. if (
  449. !isNaN(scaleFactor) &&
  450. isFinite(scaleFactor) &&
  451. scaleFactor > 0
  452. ) {
  453. const oldScale = state.scale;
  454. state.scale = Math.max(
  455. 0.1,
  456. Math.min(10, state.scale * scaleFactor)
  457. );
  458.  
  459. const centerX =
  460. (e.touches[0].clientX + e.touches[1].clientX) / 2;
  461. const centerY =
  462. (e.touches[0].clientY + e.touches[1].clientY) / 2;
  463.  
  464. const imageRect = image.getBoundingClientRect();
  465.  
  466. const imageX =
  467. centerX - (imageRect.left + imageRect.width / 2);
  468. const imageY =
  469. centerY - (imageRect.top + imageRect.height / 2);
  470.  
  471. state.translateX += imageX * (1 - scaleFactor);
  472. state.translateY += imageY * (1 - scaleFactor);
  473.  
  474. lastTouchDistance = touchDistance;
  475. }
  476.  
  477. touchRotationStart =
  478. (Math.atan2(
  479. e.touches[1].clientY - e.touches[0].clientY,
  480. e.touches[1].clientX - e.touches[0].clientX
  481. ) *
  482. 180) /
  483. Math.PI;
  484. }
  485. },
  486. { passive: false }
  487. );
  488.  
  489. image.addEventListener(
  490. "touchmove",
  491. (e) => {
  492. if (!state.loaded) return;
  493. e.preventDefault();
  494.  
  495. if (e.touches.length === 1 && state.dragging) {
  496. state.translateX += e.touches[0].clientX - state.lastX;
  497. state.translateY += e.touches[0].clientY - state.lastY;
  498. state.lastX = e.touches[0].clientX;
  499. state.lastY = e.touches[0].clientY;
  500. applyTransform();
  501. } else if (e.touches.length === 2) {
  502. const dx = e.touches[0].clientX - e.touches[1].clientX;
  503. const dy = e.touches[0].clientY - e.touches[1].clientY;
  504. const touchDistance = Math.sqrt(dx * dx + dy * dy);
  505. const scaleFactor = touchDistance / lastTouchDistance;
  506.  
  507. if (
  508. !isNaN(scaleFactor) &&
  509. isFinite(scaleFactor) &&
  510. scaleFactor > 0
  511. ) {
  512. const oldScale = state.scale;
  513. state.scale = Math.max(
  514. 0.1,
  515. Math.min(10, state.scale * scaleFactor)
  516. );
  517.  
  518. const centerX =
  519. (e.touches[0].clientX + e.touches[1].clientX) / 2;
  520. const centerY =
  521. (e.touches[0].clientY + e.touches[1].clientY) / 2;
  522. const imageRect = image.getBoundingClientRect();
  523.  
  524. const x = centerX - imageRect.left;
  525. const y = centerY - imageRect.top;
  526.  
  527. // 这是关键修复 - 调整以保持缩放点位置不变
  528. state.translateX +=
  529. (1 - scaleFactor) * (x - state.translateX);
  530. state.translateY +=
  531. (1 - scaleFactor) * (y - state.translateY);
  532.  
  533. lastTouchDistance = touchDistance;
  534. }
  535.  
  536. const touchRotation =
  537. (Math.atan2(
  538. e.touches[1].clientY - e.touches[0].clientY,
  539. e.touches[1].clientX - e.touches[0].clientX
  540. ) *
  541. 180) /
  542. Math.PI;
  543.  
  544. const rotationDelta = touchRotation - touchRotationStart;
  545. if (!isNaN(rotationDelta) && isFinite(rotationDelta)) {
  546. state.rotation = (state.rotation + rotationDelta) % 360;
  547. touchRotationStart = touchRotation;
  548. }
  549.  
  550. applyTransform();
  551. }
  552. },
  553. { passive: false }
  554. );
  555.  
  556. image.addEventListener("touchend", () => {
  557. state.dragging = false;
  558. image.classList.remove("dragging");
  559. });
  560.  
  561. // 鼠标滚轮缩放
  562. viewer.addEventListener(
  563. "wheel",
  564. (e) => {
  565. if (!state.loaded) return;
  566. e.preventDefault();
  567. const delta = e.deltaY < 0 ? 0.1 : -0.1;
  568.  
  569. // 保存鼠标位置
  570. state.lastClientX = e.clientX;
  571. state.lastClientY = e.clientY;
  572.  
  573. zoom(delta);
  574. },
  575. { passive: false }
  576. );
  577.  
  578. // 按钮点击事件
  579. helpBtn.addEventListener("click", () => {
  580. helpPanel.style.display =
  581. helpPanel.style.display === "block" ? "none" : "block";
  582. });
  583. downloadBtn.addEventListener("click", downloadImage);
  584. rotateLeftBtn.addEventListener("click", () => rotate(-90));
  585. rotateRightBtn.addEventListener("click", () => rotate(90));
  586. zoomInBtn.addEventListener("click", () => zoom(0.1));
  587. zoomOutBtn.addEventListener("click", () => zoom(-0.1));
  588. zoomResetBtn.addEventListener("click", resetTransform);
  589.  
  590. // 键盘快捷键
  591. document.addEventListener("keydown", (e) => {
  592. if (!state.loaded) return;
  593.  
  594. switch (e.key) {
  595. case "Escape":
  596. closeViewer();
  597. break;
  598. case "+":
  599. case "=":
  600. zoom(0.1);
  601. break;
  602. case "-":
  603. zoom(-0.1);
  604. break;
  605. case "0":
  606. state.scale = 1;
  607. applyTransform();
  608. break;
  609. case "ArrowLeft":
  610. rotate(-90);
  611. break;
  612. case "ArrowRight":
  613. rotate(90);
  614. break;
  615. case "r":
  616. case "R":
  617. resetTransform();
  618. break;
  619. case "d":
  620. case "D":
  621. downloadImage();
  622. break;
  623. case "h":
  624. case "H":
  625. case "?":
  626. helpPanel.style.display =
  627. helpPanel.style.display === "block" ? "none" : "block";
  628. break;
  629. }
  630. });
  631.  
  632. // 点击背景关闭帮助面板
  633. viewer.addEventListener("click", (e) => {
  634. if (e.target === viewer && helpPanel.style.display === "block") {
  635. helpPanel.style.display = "none";
  636. }
  637. });
  638.  
  639. // 双击图片重置缩放
  640. image.addEventListener("dblclick", (e) => {
  641. if (!state.loaded) return;
  642. e.preventDefault();
  643.  
  644. if (state.scale === 1) {
  645. state.lastClientX = e.clientX;
  646. state.lastClientY = e.clientY;
  647. zoom(1);
  648. } else {
  649. resetTransform();
  650. }
  651. });
  652. }
  653. createModernImageViewer(viewURL.toString());
  654. return;
  655. }
  656. return;
  657. }
  658. }
  659. })();
  660. (function interceptXHR() {
  661. const originalOpen = XMLHttpRequest.prototype.open;
  662.  
  663. XMLHttpRequest.prototype.open = function (
  664. method,
  665. url,
  666. async,
  667. user,
  668. password
  669. ) {
  670. // hook XMR
  671. if (GM_getValue("notification_showMoreNotification", true)) {
  672. if (
  673. typeof url === "string" &&
  674. url.includes("/ykt-basics/api/inform/news/list")
  675. ) {
  676. url = url.replace(/size=\d+/, "size=1000");
  677. } else if (
  678. typeof url === "string" &&
  679. url.includes("/ykt-site/site/list/student/history")
  680. ) {
  681. url = url.replace(/size=\d+/, "size=15");
  682. }
  683. }
  684.  
  685. return originalOpen.call(this, method, url, async, user, password);
  686. };
  687. })();
  688. (function () {
  689. // 等待页面DOM加载完成
  690. document.addEventListener("DOMContentLoaded", initializeExtension);
  691.  
  692. // 用户设置
  693. const settings = {
  694. home: {
  695. addHomeworkSource: GM_getValue("home_addHomeworkSource", true),
  696. useBiggerButton: GM_getValue("home_useBiggerButton", true),
  697. makeClassClickable: GM_getValue("home_makeClassClickable", true),
  698. },
  699. course: {
  700. addBatchDownload: GM_getValue("course_addBatchDownload", true),
  701. showAllDownloadButoon: GM_getValue("course_showAllDownloadButoon", false),
  702. },
  703. homework: {
  704. showHomeworkSource: GM_getValue("homework_showHomeworkSource", true),
  705. },
  706. notification: {
  707. showMoreNotification: GM_getValue(
  708. "notification_showMoreNotification",
  709. true
  710. ),
  711. sortNotificationsByTime: GM_getValue(
  712. "notification_sortNotificationsByTime",
  713. true
  714. ),
  715. betterNotificationHighlight: GM_getValue(
  716. "notification_betterNotificationHighlight",
  717. true
  718. ),
  719. },
  720. preview: {
  721. autoDownload: GM_getValue("preview_autoDownload", false),
  722. autoSwitchOffice: GM_getValue("preview_autoSwitchOffice", false),
  723. autoSwitchPdf: GM_getValue("preview_autoSwitchPdf", true),
  724. autoSwitchImg: GM_getValue("preview_autoSwitchImg", true),
  725. autoClosePopup: GM_getValue("preview_autoClosePopup", true),
  726. hideTimer: GM_getValue("preview_hideTimer", true),
  727. },
  728. system: {
  729. betterTitle: GM_getValue("system_betterTitle", true),
  730. unlockCopy: GM_getValue("system_unlockCopy", true),
  731. autoUpdate: GM_getValue("system_autoUpdate", false),
  732. showConfigButton: GM_getValue("system_showConfigButton", true),
  733. },
  734. };
  735.  
  736. // 辅助变量
  737. let jsp;
  738. let sumBytes = 0,
  739. loadedBytes = 0,
  740. downloading = false;
  741. let setClicked = false;
  742. let gpage = -1;
  743. let glist = null;
  744. let onlinePreview = null;
  745.  
  746. // 初始化扩展功能
  747. function initializeExtension() {
  748. // 注册菜单命令
  749. registerMenuCommands();
  750.  
  751. const nprogressCSS = GM_getResourceText("NPROGRESS_CSS");
  752. GM_addStyle(nprogressCSS);
  753.  
  754. loadui();
  755.  
  756. addFunctionalCSS();
  757. main();
  758.  
  759. if (settings.system.autoUpdate) {
  760. checkForUpdates();
  761. }
  762.  
  763. main();
  764.  
  765. // 监听URL哈希变化
  766. let hash = location.hash;
  767. setInterval(() => {
  768. if (location.hash != hash) {
  769. hash = location.hash;
  770. main();
  771. }
  772. }, 100);
  773. }
  774.  
  775. // 注册菜单命令
  776. function registerMenuCommands() {
  777. GM_registerMenuCommand(
  778. (settings.system.showConfigButton ? "✅ " : "❌ ") +
  779. "显示插件悬浮窗:" +
  780. (settings.system.showConfigButton ? "已启用" : "已禁用"),
  781. () => {
  782. settings.system.showConfigButton = !settings.system.showConfigButton;
  783. GM_setValue(
  784. "system_showConfigButton",
  785. settings.system.showConfigButton
  786. );
  787. location.reload();
  788. }
  789. );
  790. GM_registerMenuCommand("⚙️ 打开插件设置", () => {
  791. if (
  792. document
  793. .getElementById("yzHelper-settings")
  794. .classList.contains("visible")
  795. ) {
  796. document
  797. .getElementById("yzHelper-settings")
  798. .classList.remove("visible");
  799. setTimeout(() => {
  800. document.getElementById("yzHelper-settings").style.display = "none";
  801. }, 300);
  802. return;
  803. }
  804. document.getElementById("yzHelper-settings").style.display = "flex";
  805. void document.getElementById("yzHelper-settings").offsetWidth;
  806. document.getElementById("yzHelper-settings").classList.add("visible");
  807. });
  808. }
  809. /**
  810. * 通用标签页打开函数
  811. * @param {string} url - 要打开的URL
  812. * @param {Object} options - 选项参数
  813. * @param {boolean} [options.active=true] - 新标签页是否获得焦点
  814. * @param {boolean} [options.insert=true] - 是否在当前标签页旁边插入新标签页
  815. * @param {boolean} [options.setParent=true] - 新标签页是否将当前标签页设为父页面
  816. * @param {string} [options.windowName="_blank"] - window.open的窗口名称
  817. * @param {string} [options.windowFeatures=""] - window.open的窗口特性
  818. * @returns {Object|Window|null} 打开的标签页对象
  819. */
  820. function openTab(url, options = {}) {
  821. const defaultOptions = {
  822. active: true,
  823. insert: true,
  824. setParent: true,
  825. windowName: "_blank",
  826. windowFeatures: "",
  827. };
  828. const finalOptions = { ...defaultOptions, ...options };
  829. if (typeof GM_openInTab === "function") {
  830. try {
  831. return GM_openInTab(url, {
  832. active: finalOptions.active,
  833. insert: finalOptions.insert,
  834. setParent: finalOptions.setParent,
  835. });
  836. } catch (error) {
  837. return window.open(
  838. url,
  839. finalOptions.windowName,
  840. finalOptions.windowFeatures
  841. );
  842. }
  843. }
  844. }
  845. function showUpdateNotification(newVersion) {
  846. const notification = document.createElement("div");
  847. notification.style.cssText = `
  848. position: fixed;
  849. bottom: 80px;
  850. right: 20px;
  851. background: #4a6cf7;
  852. color: white;
  853. padding: 15px 20px;
  854. border-radius: 8px;
  855. box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  856. z-index: 10000;
  857. font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
  858. max-width: 300px;
  859. `;
  860.  
  861. notification.innerHTML = `
  862. <div style="font-weight: bold; margin-bottom: 5px;">发现新版本 v${newVersion}</div>
  863. <div style="font-size: 14px; margin-bottom: 10px;">当前版本 v${GM_info.script.version}</div>
  864. <button id="updateNow" style="background: white; color: #4a6cf7; border: none; padding: 5px 10px; border-radius: 4px; cursor: pointer; margin-right: 10px;">立即更新</button>
  865. <button id="updateLater" style="background: transparent; color: white; border: 1px solid white; padding: 5px 10px; border-radius: 4px; cursor: pointer;">稍后提醒</button>
  866. `;
  867.  
  868. document.body.appendChild(notification);
  869.  
  870. document.getElementById("updateNow").addEventListener("click", function () {
  871. openTab(GM_info.script.downloadURL, { active: true });
  872. document.body.removeChild(notification);
  873. });
  874.  
  875. document
  876. .getElementById("updateLater")
  877. .addEventListener("click", function () {
  878. document.body.removeChild(notification);
  879. });
  880. }
  881.  
  882. function checkForUpdates() {
  883. const lastCheckTime = GM_getValue("lastUpdateCheck", 0);
  884. const now = Date.now();
  885. const ONE_DAY = 24 * 60 * 60 * 1000; // 一天的毫秒数
  886.  
  887. if (now - lastCheckTime > ONE_DAY) {
  888. GM_setValue("lastUpdateCheck", now);
  889. GM_xmlhttpRequest({
  890. method: "GET",
  891. url: GM_info.script.updateURL,
  892. onload: function (response) {
  893. const versionMatch = response.responseText.match(
  894. /@version\s+(\d+\.\d+)/
  895. );
  896. if (versionMatch && versionMatch[1]) {
  897. const latestVersion = versionMatch[1];
  898. const currentVersion = GM_info.script.version;
  899. if (latestVersion > currentVersion) {
  900. showUpdateNotification(latestVersion);
  901. }
  902. }
  903. },
  904. });
  905. }
  906. }
  907.  
  908. function loadui() {
  909. GM_addStyle(`
  910. #yzHelper-settings {
  911. position: fixed;
  912. bottom: 20px;
  913. right: 20px;
  914. background: #ffffff;
  915. box-shadow: 0 5px 25px rgba(0, 0, 0, 0.15);
  916. border-radius: 12px;
  917. z-index: 9999;
  918. width: 500px;
  919. height: 450px;
  920. font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
  921. transition: all 0.3s ease;
  922. opacity: 0;
  923. transform: translateY(10px);
  924. color: #333;
  925. overflow: hidden;
  926. display: flex;
  927. flex-direction: column;
  928. display: none;
  929. }
  930. #yzHelper-settings.visible {
  931. opacity: 1;
  932. transform: translateY(0);
  933. }
  934. #yzHelper-header {
  935. padding: 15px 20px;
  936. border-bottom: 1px solid #eee;
  937. background-color: #ecb000;
  938. color: white;
  939. font-weight: bold;
  940. font-size: 16px;
  941. display: flex;
  942. justify-content: space-between;
  943. align-items: center;
  944. }
  945. #yzHelper-main {
  946. display: flex;
  947. flex: 1;
  948. overflow: hidden;
  949. }
  950. #yzHelper-settings-sidebar {
  951. width: 140px;
  952. background: #f7f7f7;
  953. padding: 15px 0;
  954. border-right: 1px solid #eee;
  955. overflow-y: auto;
  956. }
  957. #yzHelper-settings-sidebar .menu-item {
  958. padding: 12px 15px;
  959. cursor: pointer;
  960. transition: all 0.2s ease;
  961. font-size: 14px;
  962. color: #666;
  963. display: flex;
  964. align-items: center;
  965. gap: 8px;
  966. }
  967. #yzHelper-settings-sidebar .menu-item:hover {
  968. background: #efefef;
  969. color: #333;
  970. }
  971. #yzHelper-settings-sidebar .menu-item.active {
  972. background: #ffbe00;
  973. color: #fff;
  974. font-weight: 500;
  975. }
  976. #yzHelper-settings-sidebar .emoji {
  977. font-size: 16px;
  978. }
  979. #yzHelper-settings-content {
  980. flex: 1;
  981. padding: 20px;
  982. overflow-y: auto;
  983. position: relative;
  984. padding-bottom: 70px; /* Space for buttons */
  985. }
  986. #yzHelper-settings-content .settings-section {
  987. display: none;
  988. }
  989. #yzHelper-settings-content .settings-section.active {
  990. display: block;
  991. }
  992.  
  993. #section-about .about-content {
  994. line-height: 1.6;
  995. font-size: 14px;
  996. }
  997. #section-about h4 {
  998. margin: 16px 0 8px;
  999. font-size: 15px;
  1000. }
  1001. #section-about ul {
  1002. margin: 8px 0;
  1003. padding-left: 20px;
  1004. }
  1005. #section-about li {
  1006. margin-bottom: 4px;
  1007. }
  1008. #section-about .github-link {
  1009. display: inline-flex;
  1010. align-items: center;
  1011. padding: 6px 12px;
  1012. background: #f6f8fa;
  1013. border: 1px solid rgba(27, 31, 36, 0.15);
  1014. border-radius: 6px;
  1015. color: #24292f;
  1016. text-decoration: none;
  1017. font-weight: 500;
  1018. transition: background-color 0.2s;
  1019. }
  1020. #section-about .github-link:hover {
  1021. background-color: #f3f4f6;
  1022. }
  1023. #section-about .github-icon {
  1024. margin-right: 6px;
  1025. fill: currentColor;
  1026. }
  1027. #section-about .feedback-note {
  1028. margin-top: 14px;
  1029. border-top: 1px solid #eaecef;
  1030. padding-top: 14px;
  1031. font-size: 13px;
  1032. color: #57606a;
  1033. }
  1034. #section-about code {
  1035. background: rgba(175, 184, 193, 0.2);
  1036. padding: 0.2em 0.4em;
  1037. border-radius: 6px;
  1038. font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace;
  1039. font-size: 85%;
  1040. }
  1041. #yzHelper-settings h3 {
  1042. margin-top: 0;
  1043. margin-bottom: 15px;
  1044. font-size: 18px;
  1045. font-weight: 600;
  1046. color: #2c3e50;
  1047. padding-bottom: 10px;
  1048. border-bottom: 1px solid #eee;
  1049. }
  1050. #yzHelper-settings .setting-item {
  1051. margin-bottom: 16px;
  1052. }
  1053. #yzHelper-settings .setting-toggle {
  1054. display: flex;
  1055. align-items: center;
  1056. }
  1057. #yzHelper-settings .setting-item:last-of-type {
  1058. margin-bottom: 20px;
  1059. }
  1060. #yzHelper-settings .switch {
  1061. position: relative;
  1062. display: inline-block;
  1063. width: 44px;
  1064. height: 24px;
  1065. margin-right: 10px;
  1066. }
  1067. #yzHelper-settings .switch input {
  1068. opacity: 0;
  1069. width: 0;
  1070. height: 0;
  1071. }
  1072. #yzHelper-settings .slider {
  1073. position: absolute;
  1074. cursor: pointer;
  1075. top: 0;
  1076. left: 0;
  1077. right: 0;
  1078. bottom: 0;
  1079. background-color: #ccc;
  1080. transition: .3s;
  1081. border-radius: 24px;
  1082. }
  1083. #yzHelper-settings .slider:before {
  1084. position: absolute;
  1085. content: "";
  1086. height: 18px;
  1087. width: 18px;
  1088. left: 3px;
  1089. bottom: 3px;
  1090. background-color: white;
  1091. transition: .3s;
  1092. border-radius: 50%;
  1093. }
  1094. #yzHelper-settings input:checked + .slider {
  1095. background-color: #ffbe00;
  1096. }
  1097. #yzHelper-settings input:focus + .slider {
  1098. box-shadow: 0 0 1px #ffbe00;
  1099. }
  1100. #yzHelper-settings input:checked + .slider:before {
  1101. transform: translateX(20px);
  1102. }
  1103. #yzHelper-settings .setting-label {
  1104. font-size: 14px;
  1105. cursor: pointer;
  1106. }
  1107. #yzHelper-settings .setting-description {
  1108. display: block; /* 始终保持在DOM中 */
  1109. margin-left: 54px;
  1110. font-size: 12px;
  1111. color: #666;
  1112. background: #f9f9f9;
  1113. border-left: 3px solid #ffbe00;
  1114. border-radius: 0 4px 4px 0;
  1115. max-height: 0;
  1116. overflow: hidden;
  1117. opacity: 0;
  1118. transition: all 0.3s ease;
  1119. padding: 0 12px;
  1120. }
  1121. #yzHelper-settings .setting-description.visible {
  1122. max-height: 100px;
  1123. opacity: 1;
  1124. margin-top: 8px;
  1125. padding: 8px 12px;
  1126. }
  1127. #yzHelper-settings .buttons {
  1128. display: flex;
  1129. justify-content: flex-end;
  1130. gap: 10px;
  1131. position: fixed;
  1132. bottom: 0px;
  1133. right: 25px;
  1134. background: white;
  1135. padding: 10px 0;
  1136. width: calc(100% - 180px);
  1137. border-top: 1px solid #f5f5f5;
  1138. box-sizing: border-box;
  1139. }
  1140. #yzHelper-settings button {
  1141. background: #ffbe00;
  1142. border: none;
  1143. padding: 8px 16px;
  1144. border-radius: 6px;
  1145. cursor: pointer;
  1146. font-weight: 500;
  1147. color: #fff;
  1148. transition: all 0.2s ease;
  1149. outline: none;
  1150. font-size: 14px;
  1151. }
  1152. #yzHelper-settings button:hover {
  1153. background: #e9ad00;
  1154. }
  1155. #yzHelper-settings button.cancel {
  1156. background: #f1f1f1;
  1157. color: #666;
  1158. }
  1159. #yzHelper-settings button.cancel:hover {
  1160. background: #e5e5e5;
  1161. }
  1162. #yzHelper-settings-toggle {
  1163. position: fixed;
  1164. bottom: 20px;
  1165. right: 20px;
  1166. background: #ffbe00;
  1167. color: #fff;
  1168. width: 50px;
  1169. height: 50px;
  1170. border-radius: 50%;
  1171. display: flex;
  1172. align-items: center;
  1173. justify-content: center;
  1174. font-size: 24px;
  1175. cursor: pointer;
  1176. z-index: 9998;
  1177. box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
  1178. transition: all 0.3s ease;
  1179. }
  1180. #yzHelper-settings-toggle:hover {
  1181. transform: rotate(30deg);
  1182. box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
  1183. }
  1184. #yzHelper-settings .setting-item.disabled .setting-toggle,
  1185. #yzHelper-settings .setting-item .setting-toggle:has(input:disabled) {
  1186. opacity: 0.7;
  1187. }
  1188.  
  1189. #yzHelper-settings input:disabled + .slider {
  1190. background-color: #ffbe00;
  1191. opacity: 0.5;
  1192. cursor: not-allowed;
  1193. }
  1194.  
  1195. #yzHelper-settings input:disabled + .slider:before {
  1196. background-color: #f0f0f0;
  1197. }
  1198.  
  1199. #yzHelper-settings .setting-item:has(input:disabled) .setting-label:after {
  1200. content: " 🔒";
  1201. font-size: 12px;
  1202. }
  1203.  
  1204. #yzHelper-settings .setting-item:has(input:disabled) .setting-description {
  1205. border-left-color: #ccc;
  1206. font-style: italic;
  1207. }
  1208. #yzHelper-version {
  1209. position: absolute;
  1210. bottom: 15px;
  1211. left: 20px;
  1212. font-size: 12px;
  1213. color: #999;
  1214. }
  1215. `);
  1216.  
  1217. // 设置面板
  1218. const settingsToggle = document.createElement("div");
  1219. settingsToggle.id = "yzHelper-settings-toggle";
  1220. settingsToggle.innerHTML = "⚙️";
  1221. settingsToggle.title = "云邮助手设置";
  1222. if (settings.system.showConfigButton) {
  1223. document.body.appendChild(settingsToggle);
  1224. }
  1225.  
  1226. const settingsPanel = document.createElement("div");
  1227. settingsPanel.id = "yzHelper-settings";
  1228.  
  1229. const header = `
  1230. <div id="yzHelper-header">
  1231. <span>云邮教学空间助手</span>
  1232. <span id="yzHelper-version">v${GM_info.script.version}</span>
  1233. </div>
  1234. `;
  1235.  
  1236. const mainContent = `
  1237. <div id="yzHelper-main">
  1238. <div id="yzHelper-settings-sidebar">
  1239. <div class="menu-item active" data-section="home">
  1240. <span class="emoji">👤</span>
  1241. <span>个人主页</span>
  1242. </div>
  1243. <div class="menu-item" data-section="preview">
  1244. <span class="emoji">🖼️</span>
  1245. <span>课件预览</span>
  1246. </div>
  1247. <div class="menu-item" data-section="course">
  1248. <span class="emoji">📚</span>
  1249. <span>课程详情</span>
  1250. </div>
  1251. <div class="menu-item" data-section="homework">
  1252. <span class="emoji">📝</span>
  1253. <span>作业详情</span>
  1254. </div>
  1255. <div class="menu-item" data-section="notification">
  1256. <span class="emoji">📢</span>
  1257. <span>消息通知</span>
  1258. </div>
  1259. <div class="menu-item" data-section="system">
  1260. <span class="emoji">⚙️</span>
  1261. <span>系统设置</span>
  1262. </div>
  1263. <div class="menu-item" data-section="about">
  1264. <span class="emoji">ℹ️</span>
  1265. <span>关于助手</span>
  1266. </div>
  1267. </div>
  1268.  
  1269. <div id="yzHelper-settings-content">
  1270. <!-- 个人主页设置 -->
  1271. <div class="settings-section active" id="section-home">
  1272. <h3>👤 个人主页设置</h3>
  1273. <div class="setting-item">
  1274. <div class="setting-toggle">
  1275. <label class="switch">
  1276. <input type="checkbox" id="home_useBiggerButton" ${
  1277. settings.home.useBiggerButton ? "checked" : ""
  1278. }>
  1279. <span class="slider"></span>
  1280. </label>
  1281. <span class="setting-label" data-for="description-home_useBiggerButton">加大翻页按钮尺寸</span>
  1282. </div>
  1283. <div class="setting-description" id="description-home_useBiggerButton">
  1284. 增大页面翻页按钮的尺寸和点击区域,提升操作便捷性。
  1285. </div>
  1286. </div>
  1287. <div class="setting-item">
  1288. <div class="setting-toggle">
  1289. <label class="switch">
  1290. <input type="checkbox" id="home_addHomeworkSource" ${
  1291. settings.home.addHomeworkSource ? "checked" : ""
  1292. }>
  1293. <span class="slider"></span>
  1294. </label>
  1295. <span class="setting-label" data-for="description-home_addHomeworkSource">显示作业来源</span>
  1296. </div>
  1297. <div class="setting-description" id="description-home_addHomeworkSource">
  1298. 为作业添加来源,直观显示发布作业的课程。
  1299. </div>
  1300. </div>
  1301. <div class="setting-item">
  1302. <div class="setting-toggle">
  1303. <label class="switch">
  1304. <input type="checkbox" id="home_makeClassClickable" ${
  1305. settings.home.makeClassClickable ? "checked" : ""
  1306. }>
  1307. <span class="slider"></span>
  1308. </label>
  1309. <span class="setting-label" data-for="description-home_makeClassClickable">便捷跳转</span>
  1310. </div>
  1311. <div class="setting-description" id="description-home_makeClassClickable">
  1312. 点击"我的课程"跳转课程页。
  1313. </div>
  1314. </div>
  1315. </div>
  1316.  
  1317. <!-- 课件预览设置 -->
  1318. <div class="settings-section" id="section-preview">
  1319. <h3>🖼️ 课件预览设置</h3>
  1320. <div class="setting-item">
  1321. <div class="setting-toggle">
  1322. <label class="switch">
  1323. <input type="checkbox" id="preview_autoDownload" ${
  1324. settings.preview.autoDownload ? "checked" : ""
  1325. }>
  1326. <span class="slider"></span>
  1327. </label>
  1328. <span class="setting-label" data-for="description-preview_autoDownload">预览课件时自动下载</span>
  1329. </div>
  1330. <div class="setting-description" id="description-preview_autoDownload">
  1331. 当打开课件预览时,自动触发下载操作,方便存储课件到本地。
  1332. </div>
  1333. </div>
  1334. <div class="setting-item">
  1335. <div class="setting-toggle">
  1336. <label class="switch">
  1337. <input type="checkbox" id="preview_autoSwitchOffice" ${
  1338. settings.preview.autoSwitchOffice ? "checked" : ""
  1339. }>
  1340. <span class="slider"></span>
  1341. </label>
  1342. <span class="setting-label" data-for="description-preview_autoSwitchOffice">使用 Office365 预览 Office 文件</span>
  1343. </div>
  1344. <div class="setting-description" id="description-preview_autoSwitchOffice">
  1345. 使用微软 Office365 在线服务预览 Office 文档,提供更好的浏览体验。
  1346. </div>
  1347. </div>
  1348. <div class="setting-item">
  1349. <div class="setting-toggle">
  1350. <label class="switch">
  1351. <input type="checkbox" id="preview_autoSwitchPdf" ${
  1352. settings.preview.autoSwitchPdf ? "checked" : ""
  1353. }>
  1354. <span class="slider"></span>
  1355. </label>
  1356. <span class="setting-label" data-for="description-preview_autoSwitchPdf">使用 浏览器原生阅读器 预览 PDF 文件</span>
  1357. </div>
  1358. <div class="setting-description" id="description-preview_autoSwitchPdf">
  1359. 使用系统(浏览器)原生的阅读器预览PDF文档,提供更好的浏览体验。移动端及部分平板可能不支持。
  1360. </div>
  1361. </div>
  1362. <div class="setting-item">
  1363. <div class="setting-toggle">
  1364. <label class="switch">
  1365. <input type="checkbox" id="preview_autoSwitchImg" ${
  1366. settings.preview.autoSwitchImg ? "checked" : ""
  1367. }>
  1368. <span class="slider"></span>
  1369. </label>
  1370. <span class="setting-label" data-for="description-preview_autoSwitchImg">使用 脚本内置的阅读器 预览 图片 文件</span>
  1371. </div>
  1372. <div class="setting-description" id="description-preview_autoSwitchImg">
  1373. 使用脚本内置的阅读器预览图片文件,提供更好的浏览体验。
  1374. </div>
  1375. </div>
  1376. <div class="setting-item">
  1377. <div class="setting-toggle">
  1378. <label class="switch">
  1379. <input type="checkbox" id="preview_autoClosePopup" ${
  1380. settings.preview.autoClosePopup ? "checked" : ""
  1381. }>
  1382. <span class="slider"></span>
  1383. </label>
  1384. <span class="setting-label" data-for="description-preview_autoClosePopup">自动关闭弹窗</span>
  1385. </div>
  1386. <div class="setting-description" id="description-preview_autoClosePopup">
  1387. 自动关闭预览时出现的"您已经在学习"及同类弹窗。
  1388. </div>
  1389. </div>
  1390. <div class="setting-item">
  1391. <div class="setting-toggle">
  1392. <label class="switch">
  1393. <input type="checkbox" id="preview_hideTimer" ${
  1394. settings.preview.hideTimer ? "checked" : ""
  1395. }>
  1396. <span class="slider"></span>
  1397. </label>
  1398. <span class="setting-label" data-for="description-preview_hideTimer">隐藏预览界面倒计时</span>
  1399. </div>
  1400. <div class="setting-description" id="description-preview_hideTimer">
  1401. 隐藏预览界面中的倒计时提示,获得无干扰的阅读体验。
  1402. </div>
  1403. </div>
  1404. </div>
  1405. <!-- 课程详情设置 -->
  1406. <div class="settings-section" id="section-course">
  1407. <h3>📚 课程详情设置</h3>
  1408. <div class="setting-item">
  1409. <div class="setting-toggle">
  1410. <label class="switch">
  1411. <input type="checkbox" id="course_addBatchDownload" ${
  1412. settings.course.addBatchDownload ? "checked" : ""
  1413. }>
  1414. <span class="slider"></span>
  1415. </label>
  1416. <span class="setting-label" data-for="description-course_addBatchDownload">增加批量下载按钮</span>
  1417. </div>
  1418. <div class="setting-description" id="description-course_addBatchDownload">
  1419. 增加批量下载按钮,方便一键下载课程中的所有课件。
  1420. </div>
  1421. </div>
  1422. <div class="setting-item">
  1423. <div class="setting-toggle">
  1424. <label class="switch">
  1425. <input type="checkbox" id="course_showAllDownloadButoon" ${
  1426. settings.course.showAllDownloadButoon
  1427. ? "checked"
  1428. : ""
  1429. }>
  1430. <span class="slider"></span>
  1431. </label>
  1432. <span class="setting-label" data-for="description-course_showAllDownloadButoon">显示所有下载按钮</span>
  1433. </div>
  1434. <div class="setting-description" id="description-course_showAllDownloadButoon">
  1435. 使每个课件文件都有下载按钮,不允许下载的课件在启用后也可以下载。
  1436. </div>
  1437. </div>
  1438. </div>
  1439.  
  1440. <!-- 作业详情设置 -->
  1441. <div class="settings-section" id="section-homework">
  1442. <h3>📝 作业详情设置</h3>
  1443. <div class="setting-item">
  1444. <div class="setting-toggle">
  1445. <label class="switch">
  1446. <input type="checkbox" id="homework_showHomeworkSource" ${
  1447. settings.homework.showHomeworkSource
  1448. ? "checked"
  1449. : ""
  1450. }>
  1451. <span class="slider"></span>
  1452. </label>
  1453. <span class="setting-label" data-for="description-homework_showHomeworkSource">显示作业所属课程</span>
  1454. </div>
  1455. <div class="setting-description" id="description-homework_showHomeworkSource">
  1456. 在作业详情页显示作业所属的课程名称,便于区分不同课程的作业。
  1457. </div>
  1458. </div>
  1459. </div>
  1460.  
  1461. <!-- 消息通知设置 -->
  1462. <div class="settings-section" id="section-notification">
  1463. <h3>📢 消息通知设置</h3>
  1464. <div class="setting-item">
  1465. <div class="setting-toggle">
  1466. <label class="switch">
  1467. <input type="checkbox" id="notification_showMoreNotification" ${
  1468. settings.notification.showMoreNotification
  1469. ? "checked"
  1470. : ""
  1471. }>
  1472. <span class="slider"></span>
  1473. </label>
  1474. <span class="setting-label" data-for="description-notification_showMoreNotification">显示更多的通知</span>
  1475. </div>
  1476. <div class="setting-description" id="description-notification_showMoreNotification">
  1477. 在通知列表中显示更多的历史通知,不再受限于默认显示数量。
  1478. </div>
  1479. </div>
  1480. <div class="setting-item">
  1481. <div class="setting-toggle">
  1482. <label class="switch">
  1483. <input type="checkbox" id="notification_sortNotificationsByTime" ${
  1484. settings.notification.sortNotificationsByTime
  1485. ? "checked"
  1486. : ""
  1487. }>
  1488. <span class="slider"></span>
  1489. </label>
  1490. <span class="setting-label" data-for="description-notification_sortNotificationsByTime">通知按照时间排序</span>
  1491. </div>
  1492. <div class="setting-description" id="description-notification_sortNotificationsByTime">
  1493. 将通知按照时间先后顺序排列,更容易找到最新或最早的通知。
  1494. </div>
  1495. </div>
  1496. <div class="setting-item">
  1497. <div class="setting-toggle">
  1498. <label class="switch">
  1499. <input type="checkbox" id="notification_betterNotificationHighlight" ${
  1500. settings.notification.betterNotificationHighlight
  1501. ? "checked"
  1502. : ""
  1503. }>
  1504. <span class="slider"></span>
  1505. </label>
  1506. <span class="setting-label" data-for="description-notification_betterNotificationHighlight">优化未读通知高亮</span>
  1507. </div>
  1508. <div class="setting-description" id="description-notification_betterNotificationHighlight">
  1509. 增强未读通知的视觉提示,使未读消息更加醒目,不易遗漏重要信息。
  1510. </div>
  1511. </div>
  1512. </div>
  1513. <!-- 系统设置 -->
  1514. <div class="settings-section" id="section-system">
  1515. <h3>⚙️ 系统设置</h3>
  1516. <div class="setting-item">
  1517. <div class="setting-toggle">
  1518. <label class="switch">
  1519. <input type="checkbox" id="system_fixTicketBug" checked disabled>
  1520. <span class="slider"></span>
  1521. </label>
  1522. <span class="setting-label" data-for="description-system_fixTicketBug">修复ticket跳转问题</span>
  1523. </div>
  1524. <div class="setting-description" id="description-system_fixTicketBug">
  1525. 修复登录过期后,重新登录出现无法获取ticket提示的问题。
  1526. </div>
  1527. </div>
  1528. <div class="setting-item">
  1529. <div class="setting-toggle">
  1530. <label class="switch">
  1531. <input type="checkbox" id="system_betterTitle" ${
  1532. settings.system.betterTitle ? "checked" : ""
  1533. }>
  1534. <span class="slider"></span>
  1535. </label>
  1536. <span class="setting-label" data-for="description-system_betterTitle">优化页面标题</span>
  1537. </div>
  1538. <div class="setting-description" id="description-system_betterTitle">
  1539. 优化浏览器标签页的标题显示,更直观地反映当前页面内容。
  1540. </div>
  1541. </div>
  1542. <div class="setting-item">
  1543. <div class="setting-toggle">
  1544. <label class="switch">
  1545. <input type="checkbox" id="system_unlockCopy" ${
  1546. settings.system.unlockCopy ? "checked" : ""
  1547. }>
  1548. <span class="slider"></span>
  1549. </label>
  1550. <span class="setting-label" data-for="description-system_unlockCopy">解除复制限制</span>
  1551. </div>
  1552. <div class="setting-description" id="description-system_unlockCopy">
  1553. 解除全局的复制限制,方便摘录内容进行学习笔记。
  1554. </div>
  1555. </div>
  1556. <div class="setting-item">
  1557. <div class="setting-toggle">
  1558. <label class="switch">
  1559. <input type="checkbox" id="system_autoUpdate" ${
  1560. settings.system.autoUpdate ? "checked" : ""
  1561. }>
  1562. <span class="slider"></span>
  1563. </label>
  1564. <span class="setting-label" data-for="description-system_autoUpdate">内置更新检查</span>
  1565. </div>
  1566. <div class="setting-description" id="description-system_autoUpdate">
  1567. 定期检查脚本更新,确保您始终使用最新版本的功能和修复。
  1568. </div>
  1569. </div>
  1570. <div class="setting-item">
  1571. <div class="setting-toggle">
  1572. <label class="switch">
  1573. <input type="checkbox" id="system_showConfigButton" ${
  1574. settings.system.showConfigButton ? "checked" : ""
  1575. }>
  1576. <span class="slider"></span>
  1577. </label>
  1578. <span class="setting-label" data-for="description-system_showConfigButton">显示插件悬浮窗</span>
  1579. </div>
  1580. <div class="setting-description" id="description-system_showConfigButton">
  1581. 在网页界面显示助手配置按钮,方便随时调整设置。
  1582. </div>
  1583. </div>
  1584. </div>
  1585. <!-- 关于助手 -->
  1586. <div class="settings-section" id="section-about">
  1587. <h3>ℹ️ 关于云邮教学空间助手</h3>
  1588. <div class="about-content">
  1589. <p>云邮教学空间助手是一款专为云邮教学空间平台设计的浏览器增强脚本。</p>
  1590. <h4>🚀 主要功能</h4>
  1591. <ul>
  1592. <li>📍 个人主页优化 - 智能布局,提升交互体验</li>
  1593. <li>📄 课件预览增强 - 流畅浏览,轻松获取学习资源</li>
  1594. <li>📥 课程管理优化 - 批量下载,多样化下载选项</li>
  1595. <li>📋 作业管理助手 - 精准显示课程归属,提高管理效率</li>
  1596. <li>🔔 通知管理优化 - 智能整理,突出重点通知</li>
  1597. <li>🛠️ 系统功能增强 - 页面标题优化,解除复制限制等实用功能</li>
  1598. </ul>
  1599. <h4>🔗 相关链接</h4>
  1600. <p>
  1601. <a href="https://github.com/uarix/ucloud-Evolved/" target="_blank" class="github-link">
  1602. <svg class="github-icon" height="16" width="16" viewBox="0 0 16 16" aria-hidden="true">
  1603. <path d="M8 0c4.42 0 8 3.58 8 8a8.013 8.013 0 0 1-5.45 7.59c-.4.08-.55-.17-.55-.38 0-.27.01-1.13.01-2.2 0-.75-.25-1.23-.54-1.48 1.78-.2 3.65-.88 3.65-3.95 0-.88-.31-1.59-.82-2.15.08-.2.36-1.02-.08-2.12 0 0-.67-.22-2.2.82-.64-.18-1.32-.27-2-.27-.68 0-1.36.09-2 .27-1.53-1.03-2.2-.82-2.2-.82-.44 1.1-.16 1.92-.08 2.12-.51.56-.82 1.28-.82 2.15 0 3.06 1.86 3.75 3.64 3.95-.23.2-.44.55-.51 1.07-.46.21-1.61.55-2.33-.66-.15-.24-.6-.83-1.23-.82-.67.01-.27.38.01.53.34.19.73.9.82 1.13.16.45.68 1.31 2.69.94 0 .67.01 1.3.01 1.49 0 .21-.15.45-.55.38A7.995 7.995 0 0 1 0 8c0-4.42 3.58-8 8-8Z"></path>
  1604. </svg>
  1605. <span>GitHub 项目主页</span>
  1606. </a>
  1607. </p>
  1608. <p class="feedback-note">
  1609. 如有问题或建议,请通过
  1610. <a href="https://github.com/uarix/ucloud-Evolved/issues" target="_blank">GitHub Issues</a>
  1611. 提交反馈。
  1612. </p>
  1613. </div>
  1614. </div>
  1615. <div class="buttons">
  1616. <button id="cancelSettings" class="cancel">取消</button>
  1617. <button id="saveSettings">保存设置</button>
  1618. </div>
  1619. </div>
  1620. </div>
  1621. `;
  1622.  
  1623. settingsPanel.innerHTML = header + mainContent;
  1624. document.body.appendChild(settingsPanel);
  1625.  
  1626. // 菜单切换功能
  1627. document
  1628. .querySelectorAll("#yzHelper-settings-sidebar .menu-item")
  1629. .forEach((item) => {
  1630. item.addEventListener("click", function () {
  1631. // 移除所有菜单项的活动状态
  1632. document
  1633. .querySelectorAll("#yzHelper-settings-sidebar .menu-item")
  1634. .forEach((i) => {
  1635. i.classList.remove("active");
  1636. });
  1637. document
  1638. .querySelectorAll("#yzHelper-settings-content .settings-section")
  1639. .forEach((section) => {
  1640. section.classList.remove("active");
  1641. });
  1642.  
  1643. this.classList.add("active");
  1644. const sectionId = "section-" + this.getAttribute("data-section");
  1645. document.getElementById(sectionId).classList.add("active");
  1646.  
  1647. // 隐藏所有设置描述
  1648. document.querySelectorAll(".setting-description").forEach((desc) => {
  1649. desc.classList.remove("visible");
  1650. });
  1651. });
  1652. });
  1653.  
  1654. // 设置描述显示/隐藏功能
  1655. document.querySelectorAll(".setting-label").forEach((label) => {
  1656. label.addEventListener("click", function () {
  1657. const descriptionId = this.getAttribute("data-for");
  1658. const description = document.getElementById(descriptionId);
  1659.  
  1660. // 隐藏所有其他描述
  1661. document.querySelectorAll(".setting-description").forEach((desc) => {
  1662. if (desc.id !== descriptionId) {
  1663. desc.classList.remove("visible");
  1664. }
  1665. });
  1666.  
  1667. // 切换当前描述的可见性
  1668. description.classList.toggle("visible");
  1669. });
  1670. });
  1671.  
  1672. function settingsTrigger() {
  1673. const isVisible = settingsPanel.classList.contains("visible");
  1674. if (isVisible) {
  1675. settingsPanel.classList.remove("visible");
  1676. setTimeout(() => {
  1677. settingsPanel.style.display = "none";
  1678. }, 300);
  1679. } else {
  1680. settingsPanel.style.display = "flex";
  1681. void settingsPanel.offsetWidth;
  1682. settingsPanel.classList.add("visible");
  1683. }
  1684. }
  1685.  
  1686. settingsToggle.addEventListener("click", settingsTrigger);
  1687.  
  1688. document.getElementById("cancelSettings").addEventListener("click", () => {
  1689. settingsPanel.classList.remove("visible");
  1690. setTimeout(() => {
  1691. settingsPanel.style.display = "none";
  1692. }, 300);
  1693. });
  1694.  
  1695. document.getElementById("saveSettings").addEventListener("click", () => {
  1696. Array.from(
  1697. document
  1698. .querySelector("#yzHelper-settings-content")
  1699. .querySelectorAll('input[type="checkbox"]:not(:disabled)')
  1700. ).forEach((checkbox) => {
  1701. const checkboxId = checkbox.id;
  1702. if (checkboxId.includes("_")) {
  1703. const [category, settingName] = checkboxId.split("_");
  1704. if (settings[category] && settingName) {
  1705. settings[category][settingName] = checkbox.checked;
  1706. GM_setValue(`${category}_${settingName}`, checkbox.checked);
  1707. }
  1708. } else {
  1709. settings[checkboxId] = checkbox.checked;
  1710. GM_setValue(checkboxId, checkbox.checked);
  1711. }
  1712. });
  1713. settingsPanel.classList.remove("visible");
  1714. setTimeout(() => {
  1715. settingsPanel.style.display = "none";
  1716. showNotification("设置已保存", "刷新页面后生效");
  1717. }, 300);
  1718. });
  1719.  
  1720. const ul_observer = new MutationObserver((mutations) => {
  1721. const dropdownMenus = document.querySelectorAll(
  1722. 'ul.el-dropdown-menu.el-popper.dropdown-info[id*="dropdown-menu"]'
  1723. );
  1724.  
  1725. if (dropdownMenus.length > 0) {
  1726. dropdownMenus.forEach((menu) => {
  1727. if (!menu.querySelector(".plugin-settings-item")) {
  1728. const settingsItem = document.createElement("li");
  1729. settingsItem.setAttribute("tabindex", "-1");
  1730. settingsItem.classList.add(
  1731. "el-dropdown-menu__item",
  1732. "plugin-settings-item"
  1733. );
  1734. settingsItem.innerHTML = "<!---->插件设置";
  1735. settingsItem.addEventListener("click", settingsTrigger);
  1736. menu.appendChild(settingsItem);
  1737. }
  1738. });
  1739. ul_observer.disconnect();
  1740. }
  1741. });
  1742. ul_observer.observe(document.body, {
  1743. childList: true,
  1744. subtree: true,
  1745. });
  1746. }
  1747.  
  1748. // 通知函数
  1749. function showNotification(title, message) {
  1750. const notification = document.createElement("div");
  1751. notification.style.cssText = `
  1752. position: fixed;
  1753. bottom: 80px;
  1754. right: 20px;
  1755. background: #4CAF50;
  1756. color: white;
  1757. padding: 15px 20px;
  1758. border-radius: 8px;
  1759. box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  1760. z-index: 10000;
  1761. font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
  1762. max-width: 300px;
  1763. opacity: 0;
  1764. transform: translateY(-10px);
  1765. transition: all 0.3s ease;
  1766. `;
  1767.  
  1768. notification.innerHTML = `
  1769. <div style="font-weight: bold; margin-bottom: 5px;">${title}</div>
  1770. <div style="font-size: 14px;">${message}</div>
  1771. `;
  1772.  
  1773. document.body.appendChild(notification);
  1774.  
  1775. void notification.offsetWidth;
  1776.  
  1777. notification.style.opacity = "1";
  1778. notification.style.transform = "translateY(0)";
  1779.  
  1780. setTimeout(() => {
  1781. notification.style.opacity = "0";
  1782. notification.style.transform = "translateY(-10px)";
  1783. setTimeout(() => {
  1784. document.body.removeChild(notification);
  1785. }, 300);
  1786. }, 3000);
  1787. }
  1788.  
  1789. // 获取Token
  1790. function getToken() {
  1791. const cookieMap = new Map();
  1792. document.cookie.split("; ").forEach((cookie) => {
  1793. const [key, value] = cookie.split("=");
  1794. cookieMap.set(key, value);
  1795. });
  1796. const token = cookieMap.get("iClass-token");
  1797. const userid = cookieMap.get("iClass-uuid");
  1798. return [userid, token];
  1799. }
  1800.  
  1801. // 文件下载相关函数
  1802. async function downloadFile(url, filename) {
  1803. console.log("Call download");
  1804. downloading = true;
  1805. await jsp;
  1806. NProgress.configure({ trickle: false, speed: 0 });
  1807. try {
  1808. const response = await fetch(url);
  1809.  
  1810. if (!response.ok) {
  1811. throw new Error(`HTTP error! status: ${response.status}`);
  1812. }
  1813.  
  1814. const contentLength = response.headers.get("content-length");
  1815. if (!contentLength) {
  1816. throw new Error("Content-Length response header unavailable");
  1817. }
  1818.  
  1819. const total = parseInt(contentLength, 10);
  1820. sumBytes += total;
  1821. const reader = response.body.getReader();
  1822. const chunks = [];
  1823. while (true) {
  1824. const { done, value } = await reader.read();
  1825. if (done) break;
  1826. if (!downloading) {
  1827. NProgress.done();
  1828. return;
  1829. }
  1830. chunks.push(value);
  1831. loadedBytes += value.length;
  1832. NProgress.set(loadedBytes / sumBytes);
  1833. }
  1834. NProgress.done();
  1835. sumBytes -= total;
  1836. loadedBytes -= total;
  1837. const blob = new Blob(chunks);
  1838. const downloadUrl = window.URL.createObjectURL(blob);
  1839. const a = document.createElement("a");
  1840. a.href = downloadUrl;
  1841. a.download = filename;
  1842. document.body.appendChild(a);
  1843. a.click();
  1844. window.URL.revokeObjectURL(downloadUrl);
  1845. } catch (error) {
  1846. console.error("Download failed:", error);
  1847. }
  1848. }
  1849.  
  1850. // 任务搜索函数
  1851. async function searchTask(siteId, keyword, token) {
  1852. const res = await fetch(
  1853. "https://apiucloud.bupt.edu.cn/ykt-site/work/student/list",
  1854. {
  1855. headers: {
  1856. authorization: "Basic cG9ydGFsOnBvcnRhbF9zZWNyZXQ=",
  1857. "blade-auth": token,
  1858. "content-type": "application/json;charset=UTF-8",
  1859. },
  1860. body: JSON.stringify({
  1861. siteId,
  1862. keyword,
  1863. current: 1,
  1864. size: 5,
  1865. }),
  1866. method: "POST",
  1867. }
  1868. );
  1869. const json = await res.json();
  1870. return json;
  1871. }
  1872.  
  1873. // 课程搜索函数
  1874. async function searchCourse(userId, id, keyword, token) {
  1875. const res = await fetch(
  1876. "https://apiucloud.bupt.edu.cn/ykt-site/site/list/student/current?size=999999&current=1&userId=" +
  1877. userId +
  1878. "&siteRoleCode=2",
  1879. {
  1880. headers: {
  1881. authorization: "Basic cG9ydGFsOnBvcnRhbF9zZWNyZXQ=",
  1882. "blade-auth": token,
  1883. },
  1884. body: null,
  1885. method: "GET",
  1886. }
  1887. );
  1888. const json = await res.json();
  1889. const list = json.data.records.map((x) => ({
  1890. id: x.id,
  1891. name: x.siteName,
  1892. teachers: x.teachers.map((y) => y.name).join(", "),
  1893. }));
  1894.  
  1895. async function searchWithLimit(list, id, keyword, token, limit = 5) {
  1896. for (let i = 0; i < list.length; i += limit) {
  1897. const batch = list.slice(i, i + limit);
  1898. const jobs = batch.map((x) => searchTask(x.id, keyword, token));
  1899. const ress = await Promise.all(jobs);
  1900. for (let j = 0; j < ress.length; j++) {
  1901. const res = ress[j];
  1902. if (res.data && res.data.records && res.data.records.length > 0) {
  1903. for (const item of res.data.records) {
  1904. if (item.id == id) {
  1905. return batch[j];
  1906. }
  1907. }
  1908. }
  1909. }
  1910. }
  1911. return null;
  1912. }
  1913. return await searchWithLimit(list, id, keyword, token);
  1914. }
  1915.  
  1916. // 获取任务列表
  1917. async function getTasks(siteId, token) {
  1918. const res = await fetch(
  1919. "https://apiucloud.bupt.edu.cn/ykt-site/work/student/list",
  1920. {
  1921. headers: {
  1922. authorization: "Basic cG9ydGFsOnBvcnRhbF9zZWNyZXQ=",
  1923. "blade-auth": token,
  1924. "content-type": "application/json;charset=UTF-8",
  1925. },
  1926. body: JSON.stringify({
  1927. siteId,
  1928. current: 1,
  1929. size: 9999,
  1930. }),
  1931. method: "POST",
  1932. }
  1933. );
  1934. const json = await res.json();
  1935. return json;
  1936. }
  1937.  
  1938. // 搜索课程
  1939. async function searchCourses(nids) {
  1940. const result = {};
  1941. let ids = [];
  1942. for (let id of nids) {
  1943. const r = get(id);
  1944. if (r) result[id] = r;
  1945. else ids.push(id);
  1946. }
  1947.  
  1948. if (ids.length == 0) return result;
  1949. const [userid, token] = getToken();
  1950. const res = await fetch(
  1951. "https://apiucloud.bupt.edu.cn/ykt-site/site/list/student/current?size=999999&current=1&userId=" +
  1952. userid +
  1953. "&siteRoleCode=2",
  1954. {
  1955. headers: {
  1956. authorization: "Basic cG9ydGFsOnBvcnRhbF9zZWNyZXQ=",
  1957. "blade-auth": token,
  1958. },
  1959. body: null,
  1960. method: "GET",
  1961. }
  1962. );
  1963. const json = await res.json();
  1964. const list = json.data.records.map((x) => ({
  1965. id: x.id,
  1966. name: x.siteName,
  1967. teachers: x.teachers.map((y) => y.name).join(", "),
  1968. }));
  1969. const hashMap = new Map();
  1970. let count = ids.length;
  1971. for (let i = 0; i < ids.length; i++) {
  1972. hashMap.set(ids[i], i);
  1973. }
  1974.  
  1975. async function searchWithLimit(list, limit = 5) {
  1976. for (let i = 0; i < list.length; i += limit) {
  1977. const batch = list.slice(i, i + limit);
  1978. const jobs = batch.map((x) => getTasks(x.id, token));
  1979. const ress = await Promise.all(jobs);
  1980. for (let j = 0; j < ress.length; j++) {
  1981. const res = ress[j];
  1982. if (res.data && res.data.records && res.data.records.length > 0) {
  1983. for (const item of res.data.records) {
  1984. if (hashMap.has(item.id)) {
  1985. result[item.id] = batch[j];
  1986. set(item.id, batch[j]);
  1987. if (--count == 0) {
  1988. return result;
  1989. }
  1990. }
  1991. }
  1992. }
  1993. }
  1994. }
  1995. return result;
  1996. }
  1997. return await searchWithLimit(list);
  1998. }
  1999.  
  2000. // 获取未完成列表
  2001. async function getUndoneList() {
  2002. const [userid, token] = getToken();
  2003. const res = await fetch(
  2004. "https://apiucloud.bupt.edu.cn/ykt-site/site/student/undone?userId=" +
  2005. userid,
  2006. {
  2007. headers: {
  2008. authorization: "Basic cG9ydGFsOnBvcnRhbF9zZWNyZXQ=",
  2009. "blade-auth": token,
  2010. },
  2011. method: "GET",
  2012. }
  2013. );
  2014. const json = await res.json();
  2015. return json;
  2016. }
  2017.  
  2018. // 获取详情
  2019. async function getDetail(id) {
  2020. const [userid, token] = getToken();
  2021. const res = await fetch(
  2022. "https://apiucloud.bupt.edu.cn/ykt-site/work/detail?assignmentId=" + id,
  2023. {
  2024. headers: {
  2025. authorization: "Basic cG9ydGFsOnBvcnRhbF9zZWNyZXQ=",
  2026. "blade-auth": token,
  2027. },
  2028. body: null,
  2029. method: "GET",
  2030. }
  2031. );
  2032. const json = await res.json();
  2033. return json;
  2034. }
  2035.  
  2036. // 获取站点资源
  2037. async function getSiteResource(id) {
  2038. const [userid, token] = getToken();
  2039. const res = await fetch(
  2040. "https://apiucloud.bupt.edu.cn/ykt-site/site-resource/tree/student?siteId=" +
  2041. id +
  2042. "&userId=" +
  2043. userid,
  2044. {
  2045. headers: {
  2046. authorization: "Basic cG9ydGFsOnBvcnRhbF9zZWNyZXQ=",
  2047. "blade-auth": token,
  2048. },
  2049. body: null,
  2050. method: "POST",
  2051. }
  2052. );
  2053. const json = await res.json();
  2054. const result = [];
  2055. function foreach(data) {
  2056. if (!data || !Array.isArray(data)) return;
  2057. data.forEach((x) => {
  2058. if (x.attachmentVOs && Array.isArray(x.attachmentVOs)) {
  2059. x.attachmentVOs.forEach((y) => {
  2060. if (y.type !== 2 && y.resource) result.push(y.resource);
  2061. });
  2062. }
  2063. if (x.children) foreach(x.children);
  2064. });
  2065. }
  2066. foreach(json.data);
  2067. return result;
  2068. }
  2069.  
  2070. // 更新作业显示
  2071. async function updateAssignmentDisplay(list, page) {
  2072. if (!list || list.length === 0) return;
  2073.  
  2074. // 获取当前页的作业
  2075. const tlist = list.slice((page - 1) * 6, page * 6);
  2076. if (tlist.length === 0) return;
  2077.  
  2078. // 获取课程信息
  2079. const ids = tlist.map((x) => x.activityId);
  2080. const infos = await searchCourses(ids);
  2081.  
  2082. // 确保所有信息都已获取到
  2083. if (Object.keys(infos).length === 0) return;
  2084.  
  2085. // 准备显示文本
  2086. const texts = tlist.map((x) => {
  2087. const info = infos[x.activityId];
  2088. return info ? `${info.name}(${info.teachers})` : "加载中...";
  2089. });
  2090.  
  2091. // 等待作业元素显示
  2092. const timeout = 5000; // 5秒超时
  2093. const startTime = Date.now();
  2094.  
  2095. let nodes;
  2096. while (Date.now() - startTime < timeout) {
  2097. nodes = $x(
  2098. '//*[@id="layout-container"]/div[2]/div[2]/div/div[2]/div[1]/div[3]/div[2]/div/div'
  2099. );
  2100. if (
  2101. nodes.length > 0 &&
  2102. nodes.some((node) => node.children[0] && node.children[0].innerText)
  2103. ) {
  2104. break;
  2105. }
  2106. await sleep(100);
  2107. }
  2108.  
  2109. // 更新课程信息显示
  2110. for (let i = 0; i < Math.min(nodes.length, texts.length); i++) {
  2111. if (nodes[i] && nodes[i].children[1]) {
  2112. if (nodes[i].children[1].children.length === 0) {
  2113. const p = document.createElement("div");
  2114. const t = document.createTextNode(texts[i]);
  2115. p.appendChild(t);
  2116. p.style.color = "#0066cc";
  2117. nodes[i].children[1].insertAdjacentElement("afterbegin", p);
  2118. } else {
  2119. nodes[i].children[1].children[0].innerHTML = texts[i];
  2120. nodes[i].children[1].children[0].style.color = "#0066cc";
  2121. }
  2122. }
  2123. }
  2124. }
  2125.  
  2126. // XPath选择器
  2127. function $x(xpath, context = document) {
  2128. const iterator = document.evaluate(
  2129. xpath,
  2130. context,
  2131. null,
  2132. XPathResult.ANY_TYPE,
  2133. null
  2134. );
  2135. const results = [];
  2136. let item;
  2137. while ((item = iterator.iterateNext())) {
  2138. results.push(item);
  2139. }
  2140. return results;
  2141. }
  2142.  
  2143. // 本地存储
  2144. function set(k, v) {
  2145. const h = JSON.parse(localStorage.getItem("zzxw") || "{}");
  2146. h[k] = v;
  2147. localStorage.setItem("zzxw", JSON.stringify(h));
  2148. }
  2149.  
  2150. function get(k) {
  2151. const h = JSON.parse(localStorage.getItem("zzxw") || "{}");
  2152. return h[k];
  2153. }
  2154.  
  2155. // 插入课程信息
  2156. function insert(x) {
  2157. if (!x) return;
  2158. if (
  2159. $x(
  2160. "/html/body/div[1]/div/div[2]/div[2]/div/div/div[2]/div/div[2]/div[1]/div/div/div[1]/div/p"
  2161. ).length > 2
  2162. )
  2163. return;
  2164. const d = $x(
  2165. "/html/body/div[1]/div/div[2]/div[2]/div/div/div[2]/div/div[2]/div[1]/div/div/div[1]/div/p[1]"
  2166. );
  2167. if (!d.length) {
  2168. setTimeout(() => insert(x), 50);
  2169. return;
  2170. }
  2171. // 检查是否已经插入过
  2172. const existingText = Array.from(d[0].parentNode.childNodes).some(
  2173. (node) => node.textContent && node.textContent.includes(x.name)
  2174. );
  2175.  
  2176. if (!existingText) {
  2177. const p = document.createElement("p");
  2178. const t = document.createTextNode(x.name + "(" + x.teachers + ")");
  2179. p.appendChild(t);
  2180. d[0].after(p);
  2181. }
  2182. }
  2183.  
  2184. // 辅助函数
  2185. function sleep(n) {
  2186. return new Promise((res) => setTimeout(res, n));
  2187. }
  2188.  
  2189. async function wait(func) {
  2190. let r = func();
  2191. if (r instanceof Promise) r = await r;
  2192. if (r) return r;
  2193. await sleep(50);
  2194. return await wait(func);
  2195. }
  2196.  
  2197. async function waitChange(func, value) {
  2198. const r = value;
  2199. while (1) {
  2200. let t = func();
  2201. if (t instanceof Promise) t = await t;
  2202. if (t != r) return t;
  2203. await sleep(50);
  2204. }
  2205. }
  2206.  
  2207. // 预览URL相关
  2208. async function getPreviewURL(storageId) {
  2209. const res = await fetch(
  2210. "https://apiucloud.bupt.edu.cn/blade-source/resource/preview-url?resourceId=" +
  2211. storageId
  2212. );
  2213. const json = await res.json();
  2214. onlinePreview = json.data.onlinePreview;
  2215. return json.data.previewUrl;
  2216. }
  2217.  
  2218. // 启用文本选择 修改按钮尺寸
  2219. function addFunctionalCSS() {
  2220. GM_addStyle(`
  2221. .teacher-home-page .home-left-container .in-progress-section .in-progress-body .in-progress-item .activity-box .activity-title {
  2222. height: auto !important;
  2223. }
  2224. #layout-container > div.main-content > div.router-container > div > div.my-course-page {
  2225. max-height: none !important;
  2226. }
  2227. `);
  2228. if (settings.notification.betterNotificationHighlight) {
  2229. GM_addStyle(`
  2230. .notification-with-dot {
  2231. background-color: #fff8f8 !important;
  2232. border-left: 5px solid #f56c6c !important;
  2233. box-shadow: 0 2px 6px rgba(245, 108, 108, 0.2) !important;
  2234. padding: 0 22px !important;
  2235. margin-bottom: 8px !important;
  2236. border-radius: 4px !important;
  2237. transition: all 0.3s ease !important;
  2238. }
  2239. .notification-with-dot:hover {
  2240. background-color: #fff0f0 !important;
  2241. box-shadow: 0 4px 12px rgba(245, 108, 108, 0.3) !important;
  2242. transform: translateY(-2px) !important;
  2243. }
  2244. `);
  2245. }
  2246. if (settings.system.unlockCopy) {
  2247. GM_addStyle(`
  2248. .el-checkbox, .el-checkbox-button__inner, .el-empty__image img, .el-radio,
  2249. div, span, p, a, h1, h2, h3, h4, h5, h6, li, td, th {
  2250. -webkit-user-select: auto !important;
  2251. -moz-user-select: auto !important;
  2252. -ms-user-select: auto !important;
  2253. user-select: auto !important;
  2254. }
  2255. `);
  2256. document.addEventListener(
  2257. "copy",
  2258. function (e) {
  2259. e.stopImmediatePropagation();
  2260. },
  2261. true
  2262. );
  2263.  
  2264. document.addEventListener(
  2265. "selectstart",
  2266. function (e) {
  2267. e.stopImmediatePropagation();
  2268. },
  2269. true
  2270. );
  2271. }
  2272. if (settings.home.useBiggerButton) {
  2273. GM_addStyle(`
  2274. .teacher-home-page .home-left-container .my-lesson-section .my-lesson-header .header-control .banner-control-btn, .teacher-home-page .home-left-container .in-progress-section .in-progress-header .header-control .banner-control-btn {
  2275. width: 60px !important;
  2276. height: 30px !important;
  2277. background: #f2f2f2 !important;
  2278. line-height: auto !important;
  2279. }
  2280. .teacher-home-page .home-left-container .my-lesson-section .my-lesson-header .header-control .banner-control-btn span,.teacher-home-page .home-left-container .in-progress-section .in-progress-header .header-control .banner-control-btn span {
  2281. font-size: 22px !important;
  2282. }
  2283. .el-icon-arrow-left, .el-icon-arrow-right {
  2284. height: 100%;
  2285. display: flex;
  2286. align-items: center;
  2287. justify-content: center;
  2288. }
  2289. `);
  2290. }
  2291. }
  2292.  
  2293. // 主函数
  2294. async function main() {
  2295. "use strict";
  2296. // ticket跳转
  2297. if (new URLSearchParams(location.search).get("ticket")?.length) {
  2298. setTimeout(() => {
  2299. location.href = "https://ucloud.bupt.edu.cn/uclass/#/student/homePage";
  2300. }, 500);
  2301. return;
  2302. }
  2303.  
  2304. // 课件预览页面
  2305. if (
  2306. location.href.startsWith(
  2307. "https://ucloud.bupt.edu.cn/uclass/course.html#/resourceLearn"
  2308. )
  2309. ) {
  2310. if (settings.system.betterTitle) {
  2311. function extractFilename(url) {
  2312. try {
  2313. const match = url.match(/previewUrl=([^&]+)/);
  2314. if (!match) return null;
  2315.  
  2316. const previewUrl = decodeURIComponent(match[1]);
  2317.  
  2318. // 从content-disposition中提取文件名
  2319. const filenameMatch = previewUrl.match(/filename%3D([^&]+)/);
  2320. if (!filenameMatch) return null;
  2321.  
  2322. return decodeURIComponent(decodeURIComponent(filenameMatch[1]));
  2323. } catch (e) {
  2324. return null;
  2325. }
  2326. }
  2327. const url = location.href;
  2328. const filename = extractFilename(url);
  2329. const pageTitle = "[预览] " + (filename || "课件") + " - 教学云空间";
  2330. document.title = pageTitle;
  2331. }
  2332. if (settings.preview.autoClosePopup) {
  2333. const dialogBox = document.querySelector("div.el-message-box__wrapper");
  2334.  
  2335. if (
  2336. dialogBox &&
  2337. window.getComputedStyle(dialogBox).display !== "none"
  2338. ) {
  2339. const messageElement = dialogBox.querySelector(
  2340. ".el-message-box__message p"
  2341. );
  2342. if (
  2343. messageElement &&
  2344. (messageElement.textContent.includes("您正在学习其他课件") ||
  2345. messageElement.textContent.includes("您已经在学习此课件了"))
  2346. ) {
  2347. const confirmButton = dialogBox.querySelector(
  2348. ".el-button--primary"
  2349. );
  2350. if (confirmButton) {
  2351. confirmButton.click();
  2352. } else {
  2353. console.log("未找到确认按钮");
  2354. }
  2355. }
  2356. }
  2357. }
  2358. if (settings.preview.hideTimer) {
  2359. GM_addStyle(`
  2360. .preview-container .time {
  2361. display: none !important;
  2362. }
  2363. `);
  2364. }
  2365. }
  2366.  
  2367. // 作业详情页面
  2368. if (
  2369. location.href.startsWith(
  2370. "https://ucloud.bupt.edu.cn/uclass/course.html#/student/assignmentDetails_fullpage"
  2371. )
  2372. ) {
  2373. const q = new URLSearchParams(location.href);
  2374. const id = q.get("assignmentId");
  2375. const r = get(id);
  2376. const [userid, token] = getToken();
  2377. const title = q.get("assignmentTitle");
  2378. if (settings.system.betterTitle) {
  2379. const pageTitle = "[作业] " + title + " - 教学云空间";
  2380. document.title = pageTitle;
  2381. }
  2382. if (settings.homework.showHomeworkSource) {
  2383. // 显示相关课程信息
  2384. if (r) {
  2385. insert(r);
  2386. } else {
  2387. if (!id || !title) return;
  2388. try {
  2389. const courseInfo = await searchCourse(userid, id, title, token);
  2390. if (courseInfo) {
  2391. insert(courseInfo);
  2392. set(id, courseInfo);
  2393. }
  2394. } catch (e) {
  2395. console.error("获取课程信息失败", e);
  2396. }
  2397. }
  2398. }
  2399.  
  2400. // 处理资源预览和下载
  2401. try {
  2402. const detail = (await getDetail(id)).data;
  2403. if (!detail || !detail.assignmentResource) return;
  2404.  
  2405. const filenames = detail.assignmentResource.map((x) => x.resourceName);
  2406. const urls = await Promise.all(
  2407. detail.assignmentResource.map((x) => {
  2408. return getPreviewURL(x.resourceId);
  2409. })
  2410. );
  2411.  
  2412. await wait(
  2413. () =>
  2414. $x('//*[@id="assignment-info"]/div[2]/div[2]/div[2]/div').length > 0
  2415. );
  2416.  
  2417. $x('//*[@id="assignment-info"]/div[2]/div[2]/div[2]/div').forEach(
  2418. (x, index) => {
  2419. if (
  2420. x.querySelector(".by-icon-eye-grey") ||
  2421. x.querySelector(".by-icon-yundown-grey")
  2422. ) {
  2423. x.querySelector(".by-icon-eye-grey").remove();
  2424. x.querySelector(".by-icon-yundown-grey").remove();
  2425. }
  2426.  
  2427. // 添加预览按钮
  2428. const i = document.createElement("i");
  2429. i.title = "预览";
  2430. i.classList.add("by-icon-eye-grey");
  2431. i.addEventListener("click", () => {
  2432. const url = urls[index];
  2433. const filename = filenames[index];
  2434. if (settings.preview.autoDownload) {
  2435. downloadFile(url, filename);
  2436. console.log("Autodownload");
  2437. }
  2438. if (
  2439. filename.endsWith(".xls") ||
  2440. filename.endsWith(".xlsx") ||
  2441. url.endsWith(".doc") ||
  2442. url.endsWith(".docx") ||
  2443. url.endsWith(".ppt") ||
  2444. url.endsWith(".pptx")
  2445. )
  2446. openTab(
  2447. "https://view.officeapps.live.com/op/view.aspx?src=" +
  2448. encodeURIComponent(url),
  2449. { active: true, insert: true }
  2450. );
  2451. else if (onlinePreview !== null)
  2452. openTab(onlinePreview + encodeURIComponent(url), {
  2453. active: true,
  2454. insert: true,
  2455. });
  2456. });
  2457.  
  2458. // 添加下载按钮
  2459. const i2 = document.createElement("i");
  2460. i2.title = "下载";
  2461. i2.classList.add("by-icon-yundown-grey");
  2462. i2.addEventListener("click", () => {
  2463. downloadFile(urls[index], filenames[index]);
  2464. });
  2465.  
  2466. // 插入按钮
  2467. if (x.children.length >= 3) {
  2468. x.children[3]?.remove();
  2469. x.children[2]?.insertAdjacentElement("afterend", i);
  2470. x.children[2]?.remove();
  2471. x.children[1]?.insertAdjacentElement("afterend", i2);
  2472. } else {
  2473. x.appendChild(i2);
  2474. x.appendChild(i);
  2475. }
  2476. }
  2477. );
  2478. } catch (e) {
  2479. console.error("处理资源失败", e);
  2480. }
  2481. }
  2482.  
  2483. // 主页面
  2484. else if (
  2485. location.href.startsWith(
  2486. "https://ucloud.bupt.edu.cn/uclass/#/student/homePage"
  2487. ) ||
  2488. location.href.startsWith(
  2489. "https://ucloud.bupt.edu.cn/uclass/index.html#/student/homePage"
  2490. )
  2491. ) {
  2492. if (settings.system.betterTitle) {
  2493. const pageTitle = "个人主页 - 教学云空间";
  2494. document.title = pageTitle;
  2495. }
  2496. if (settings.home.addHomeworkSource) {
  2497. // 未完成任务列表
  2498. const list = glist || (await getUndoneList()).data.undoneList;
  2499. if (!list || !Array.isArray(list)) return;
  2500. glist = list;
  2501.  
  2502. const observer = new MutationObserver(async (mutations) => {
  2503. // 当前页码
  2504. const pageElement = document.querySelector(
  2505. "#layout-container > div.main-content > div.router-container > div > div.teacher-home-page > div.home-left-container.home-inline-block > div.in-progress-section.home-card > div.in-progress-header > div > div:nth-child(2) > div > div.banner-indicator.home-inline-block"
  2506. );
  2507.  
  2508. if (!pageElement) return;
  2509.  
  2510. // 解析页码
  2511. const currentPage = parseInt(
  2512. pageElement.innerHTML.trim().split("/")[0]
  2513. );
  2514. if (isNaN(currentPage)) return;
  2515.  
  2516. // 页码变化则更新显示
  2517. if (currentPage !== gpage) {
  2518. gpage = currentPage;
  2519. await updateAssignmentDisplay(list, currentPage);
  2520. }
  2521. });
  2522.  
  2523. observer.observe(document.body, {
  2524. childList: true,
  2525. subtree: true,
  2526. attributes: false,
  2527. characterData: true,
  2528. });
  2529.  
  2530. // 初始化页码
  2531. let page = 1;
  2532. const pageElement = document.querySelector(
  2533. "#layout-container > div.main-content > div.router-container > div > div.teacher-home-page > div.home-left-container.home-inline-block > div.in-progress-section.home-card > div.in-progress-header > div > div:nth-child(2) > div > div.banner-indicator.home-inline-block"
  2534. );
  2535.  
  2536. if (pageElement) {
  2537. page = parseInt(pageElement.innerHTML.trim().split("/")[0]);
  2538. gpage = page;
  2539. }
  2540.  
  2541. // 更新作业显示
  2542. await updateAssignmentDisplay(list, page);
  2543. }
  2544. if (settings.home.makeClassClickable) {
  2545. // 本学期课程点击事件
  2546. document.querySelectorAll('div[class="header-label"]').forEach((el) => {
  2547. if (el.textContent.includes("本学期课程")) {
  2548. el.style.cursor = "pointer";
  2549. el.addEventListener("click", (e) => {
  2550. e.preventDefault();
  2551. window.location.href =
  2552. "https://ucloud.bupt.edu.cn/uclass/index.html#/student/myCourse";
  2553. });
  2554. }
  2555. });
  2556. }
  2557. }
  2558.  
  2559. // 课程主页
  2560. else if (
  2561. location.href.startsWith(
  2562. "https://ucloud.bupt.edu.cn/uclass/course.html#/student/courseHomePage"
  2563. )
  2564. ) {
  2565. try {
  2566. const site = JSON.parse(localStorage.getItem("site"));
  2567. if (!site || !site.id) return;
  2568. if (settings.system.betterTitle) {
  2569. const pageTitle = "[课程] " + site.siteName + " - 教学云空间";
  2570. document.title = pageTitle;
  2571. }
  2572.  
  2573. const id = site.id;
  2574. const resources = await getSiteResource(id);
  2575.  
  2576. // 添加下载按钮到每个资源
  2577. const resourceItems = $x(
  2578. '//div[@class="resource-item"]/div[@class="right"]'
  2579. );
  2580. const previewItems = $x(
  2581. '//div[@class="resource-item"]/div[@class="left"]'
  2582. );
  2583.  
  2584. if (resourceItems.length > 0) {
  2585. resourceItems.forEach((x, index) => {
  2586. if (index >= resources.length) return;
  2587.  
  2588. if (settings.preview.autoDownload) {
  2589. previewItems[index].addEventListener(
  2590. "click",
  2591. async (e) => {
  2592. const url = await getPreviewURL(resources[index].id);
  2593. downloadFile(url, resources[index].name);
  2594. console.log("Autodownload");
  2595. },
  2596. false
  2597. );
  2598. }
  2599. if (settings.course.showAllDownloadButoon) {
  2600. const i = document.createElement("i");
  2601. i.title = "下载";
  2602. i.classList.add("by-icon-download");
  2603. i.classList.add("btn-icon");
  2604. i.classList.add("visible");
  2605. i.style.cssText = `
  2606. display: inline-block !important;
  2607. visibility: visible !important;
  2608. cursor: pointer !important;
  2609. `;
  2610.  
  2611. // 获取data-v属性
  2612. const dataAttr = Array.from(x.attributes).find((attr) =>
  2613. attr.localName.startsWith("data-v")
  2614. );
  2615. if (dataAttr) {
  2616. i.setAttribute(dataAttr.localName, "");
  2617. }
  2618.  
  2619. i.addEventListener(
  2620. "click",
  2621. async (e) => {
  2622. e.stopPropagation();
  2623. const url = await getPreviewURL(resources[index].id);
  2624. downloadFile(url, resources[index].name);
  2625. },
  2626. false
  2627. );
  2628. if (x.children.length) x.children[0].remove();
  2629. x.insertAdjacentElement("afterbegin", i);
  2630. }
  2631. });
  2632.  
  2633. // "下载全部"按钮
  2634. if (
  2635. !document.getElementById("downloadAllButton") &&
  2636. resources.length > 0 &&
  2637. settings.course.addBatchDownload
  2638. ) {
  2639. const downloadAllButton = `<div style="display: flex;flex-direction: row;justify-content: end;margin-right: 24px;margin-top: 20px;">
  2640. <button type="button" class="el-button submit-btn el-button--primary" id="downloadAllButton">
  2641. 下载全部
  2642. </button>
  2643. </div>`;
  2644.  
  2645. const resourceList = $x(
  2646. "/html/body/div/div/div[2]/div[2]/div/div/div"
  2647. );
  2648. if (resourceList.length > 0) {
  2649. const containerElement = document.createElement("div");
  2650. containerElement.innerHTML = downloadAllButton;
  2651. resourceList[0].before(containerElement);
  2652.  
  2653. document.getElementById("downloadAllButton").onclick =
  2654. async () => {
  2655. downloading = !downloading;
  2656. if (downloading) {
  2657. document.getElementById("downloadAllButton").innerHTML =
  2658. "取消下载";
  2659. for (let file of resources) {
  2660. if (!downloading) return;
  2661. await downloadFile(
  2662. await getPreviewURL(file.id),
  2663. file.name
  2664. );
  2665. }
  2666. // 下载完成后重置按钮
  2667. if (downloading) {
  2668. downloading = false;
  2669. document.getElementById("downloadAllButton").innerHTML =
  2670. "下载全部";
  2671. }
  2672. } else {
  2673. document.getElementById("downloadAllButton").innerHTML =
  2674. "下载全部";
  2675. }
  2676. };
  2677. }
  2678. }
  2679. }
  2680. } catch (e) {
  2681. console.error("课程主页处理失败", e);
  2682. }
  2683. } else if (location.href == "https://ucloud.bupt.edu.cn/#/") {
  2684. if (settings.system.betterTitle) {
  2685. const pageTitle = "首页 - 教学云空间";
  2686. document.title = pageTitle;
  2687. }
  2688. }
  2689. // 通知页
  2690. else if (
  2691. location.href ==
  2692. "https://ucloud.bupt.edu.cn/uclass/index.html#/set/notice_fullpage"
  2693. ) {
  2694. if (settings.system.betterTitle) {
  2695. const pageTitle = "通知 - 教学云空间";
  2696. document.title = pageTitle;
  2697. }
  2698.  
  2699. function processNotifications() {
  2700. const noticeContainer = document.querySelector(
  2701. "#layout-container > div.main-content > div.router-container > div > div > div.setNotice-body > ul"
  2702. );
  2703. if (!noticeContainer) {
  2704. console.log("通知容器未找到");
  2705. return;
  2706. }
  2707. const noticeItems = Array.from(noticeContainer.querySelectorAll("li"));
  2708. if (noticeItems.length === 0) {
  2709. console.log("未找到通知项");
  2710. return;
  2711. }
  2712. if (settings.notification.sortNotificationsByTime) {
  2713. noticeItems.sort((a, b) => {
  2714. const timeA = a.querySelector("span._left-time");
  2715. const timeB = b.querySelector("span._left-time");
  2716. if (!timeA || !timeB) {
  2717. return 0;
  2718. }
  2719. const timeTextA = timeA.textContent.trim();
  2720. const timeTextB = timeB.textContent.trim();
  2721. const dateA = new Date(timeTextA);
  2722. const dateB = new Date(timeTextB);
  2723. return dateB - dateA;
  2724. });
  2725. }
  2726. noticeItems.forEach((item) => {
  2727. if (settings.notification.betterNotificationHighlight) {
  2728. const hasRedDot = item.querySelector(
  2729. "div.el-badge sup.el-badge__content.is-dot"
  2730. );
  2731. if (hasRedDot) {
  2732. item.classList.remove("notification-with-dot");
  2733. item.classList.add("notification-with-dot");
  2734. } else {
  2735. item.classList.remove("notification-with-dot");
  2736. }
  2737. }
  2738. noticeContainer.appendChild(item);
  2739. });
  2740. }
  2741. if (
  2742. settings.notification.sortNotificationsByTime ||
  2743. settings.notification.betterNotificationHighlight
  2744. ) {
  2745. // 等待通知元素加载好了再处理
  2746. const loadingMaskSelector =
  2747. "#layout-container > div.main-content > div.router-container > div > div > div.setNotice-body > div.el-loading-mask";
  2748. const observer = new MutationObserver((mutations) => {
  2749. const loadingMask = document.querySelector(loadingMaskSelector);
  2750. if (loadingMask && loadingMask.style.display === "none") {
  2751. processNotifications();
  2752. observer.disconnect();
  2753. }
  2754. });
  2755.  
  2756. const loadingMask = document.querySelector(loadingMaskSelector);
  2757. if (loadingMask && loadingMask.style.display === "none") {
  2758. processNotifications();
  2759. } else {
  2760. observer.observe(document.body, {
  2761. attributes: true,
  2762. attributeFilter: ["style"],
  2763. subtree: true,
  2764. });
  2765. setTimeout(() => observer.disconnect(), 10000);
  2766. }
  2767. }
  2768. }
  2769. }
  2770. })();