Vidstack Player for LMS Thapar

Enhance <video> elements with Vidstack player on LMS Thapar website

目前為 2024-05-21 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Vidstack Player for LMS Thapar
// @namespace    http://tampermonkey.net/
// @version      0.1
// @license MIT
// @description  Enhance <video> elements with Vidstack player on LMS Thapar website
// @author       You
// @match        https://lms.thapar.edu/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Import styles
    var styleElement = document.createElement('link');
    styleElement.rel = 'stylesheet';
    styleElement.href = 'https://cdn.vidstack.io/player/theme.css';
    document.head.appendChild(styleElement);

    var videoStyleElement = document.createElement('link');
    videoStyleElement.rel = 'stylesheet';
    videoStyleElement.href = 'https://cdn.vidstack.io/player/video.css';
    document.head.appendChild(videoStyleElement);

    // Import elements
    var scriptElement = document.createElement('script');
    scriptElement.src = 'https://cdn.vidstack.io/player';
    scriptElement.type = 'module';
    document.body.appendChild(scriptElement);

    // Replace <video> elements with Vidstack player markup
    function replaceVideosWithVidstackPlayer() {
        var videos = document.querySelectorAll('video');
        videos.forEach(function(video) {
            var src = getVideoSource(video);
            if (src) {
                var poster = video.poster;
                var thumbnails = video.getAttribute('data-thumbnails');

                // Create <media-player> element
                var mediaPlayer = document.createElement('media-player');
                mediaPlayer.setAttribute('src', src);
                mediaPlayer.setAttribute('title', 'Video');
                mediaPlayer.setAttribute('poster', poster);

                // Create <media-provider> element
                var mediaProvider = document.createElement('media-provider');

                // Create <media-video-layout> element
                var mediaVideoLayout = document.createElement('media-video-layout');
                mediaVideoLayout.setAttribute('thumbnails', thumbnails);

                // Append elements to <media-player>
                mediaPlayer.appendChild(mediaProvider);
                mediaPlayer.appendChild(mediaVideoLayout);

                // Replace <video> with <media-player>
                video.parentNode.replaceChild(mediaPlayer, video);
            }
        });
    }

    // Function to retrieve video source
    function getVideoSource(video) {
        if (video.src) {
            return video.src;
        } else if (video.querySelector('source')) {
            return video.querySelector('source').src;
        }
        return null;
    }

    // Call replaceVideosWithVidstackPlayer() when the page is fully loaded
    window.addEventListener('load', function() {
        replaceVideosWithVidstackPlayer();
    });
})();