BIT-乐学-折叠每节的公告等重复内容

在单独页面显示小节的课程,每个页面开头都会有基本无用的重复内容。这个脚本可以折叠它并提供展开按钮。

安装此脚本?
作者推荐脚本

您可能也喜欢BIT-乐学-修改侧边栏课程

安装此脚本
  1. // ==UserScript==
  2. // @name BIT-乐学-折叠每节的公告等重复内容
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2.0
  5. // @description 在单独页面显示小节的课程,每个页面开头都会有基本无用的重复内容。这个脚本可以折叠它并提供展开按钮。
  6. // @license GPL-3.0-or-later
  7. // @supportURL https://github.com/YDX-2147483647/BIT-enhanced/issues
  8. // @author Y.D.X.
  9. // @match https://lexue.bit.edu.cn/course/view.php?id=*&section=*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict'
  15.  
  16. function add_style_sheet () {
  17. const sheet = document.createElement('style')
  18. sheet.innerHTML = `
  19. [role=main] > .course-content > .collapse-content {
  20. height: 15em;
  21. overflow: auto;
  22. }
  23.  
  24. .hider {
  25. margin-top: -4em;
  26. background: linear-gradient(#0000, lightgray);
  27. height: 4em;
  28.  
  29. display: grid;
  30. place-content: center;
  31. }
  32. #show-all {
  33. z-index: 2;
  34. }
  35. `
  36. document.head.appendChild(sheet)
  37. }
  38.  
  39. const course_content = document.querySelector('[role=main] > .course-content')
  40. const front_content = course_content.querySelector('.course-content > ul:first-child')
  41. const single_section = course_content.querySelector('.single-section')
  42. const collapse = 'collapse-content' // moodle 已经占用了 .collapse:not(.show)
  43.  
  44. function trim_headings () {
  45. front_content.querySelectorAll('h1, h2, h3, h4, h5, h6').forEach(h => {
  46. h.textContent = h.textContent.trim()
  47. })
  48. }
  49.  
  50. function add_show_all_button () {
  51. const div = document.createElement('div')
  52. div.classList.add('hider')
  53. div.innerHTML = '<button id="show-all">▼展开</button>'
  54. course_content.insertBefore(div, single_section)
  55.  
  56. div.querySelector('#show-all').addEventListener('click', () => {
  57. front_content.classList.remove(collapse)
  58. div.hidden = true
  59. })
  60. }
  61.  
  62. function initiate_collapse () {
  63. front_content.classList.add(collapse)
  64. }
  65.  
  66. if (front_content.clientHeight > 1 / 3 * window.innerHeight) {
  67. add_style_sheet()
  68.  
  69. trim_headings()
  70. initiate_collapse()
  71. add_show_all_button()
  72. }
  73. })()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址