智慧树网课滚动到当前集

在页面右上角添加一个固定按钮,点击后跳转到激活的文件项,加载时自动跳转一次

目前為 2024-12-17 提交的版本,檢視 最新版本

// ==UserScript==
// @name         智慧树网课滚动到当前集
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  在页面右上角添加一个固定按钮,点击后跳转到激活的文件项,加载时自动跳转一次
// @author       You
// @match        https://hike.zhihuishu.com/aidedteaching/sourceLearning/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 创建按钮并设置样式
    const button = document.createElement('button');
    button.innerText = '滚动到当前';
    button.style.position = 'fixed';
    button.style.top = '20px';
    button.style.right = '5px';
    button.style.padding = '10px 20px';
    button.style.backgroundColor = '#4CAF50';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';
    button.style.zIndex = '1000';

    // 将按钮添加到页面
    document.body.appendChild(button);

    // 获取激活的文件项并跳转
    function scrollToActiveElement() {
        const activeElement = document.querySelector('.file-item.active');
        if (activeElement) {
            activeElement.scrollIntoView({ behavior: 'instant', block: 'center' });
        } else {
            alert('未找到激活的文件项!');
        }
    }

    // 点击按钮时跳转
    button.addEventListener('click', scrollToActiveElement);

    // 页面加载时自动跳转
    window.addEventListener('load', function() {
        setTimeout(scrollToActiveElement, 500); // 加载后500ms自动跳转
    });
})();