Greasy Fork

Greasy Fork is available in English.

南工在线转码助手

Display cookies, video ID, and manage transcoding queue from online.njtech.edu.cn

目前为 2024-11-30 提交的版本,查看 最新版本

// ==UserScript==
// @name         南工在线转码助手
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Display cookies, video ID, and manage transcoding queue from online.njtech.edu.cn
// @author       千纸鹤也要飞啊
// @match        *://online.njtech.edu.cn/video/*
// @grant        GM_addStyle
// ==/UserScript==
 
(function() {
    'use strict';
 
    // 添加悬浮窗样式
    GM_addStyle(`
        #cookie-display {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
            z-index: 9999;
            font-size: 12px;
            max-width: 300px;
            word-break: break-all;
        }
        #cookie-display-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
        }
        #cookie-display-content {
            margin-top: 5px;
        }
        .close-button {
            cursor: pointer;
            padding: 0 5px;
        }
        .queue-button, .show-queue-button, .start-transcoding-button {
            padding: 5px 8px;
            background-color: #4CAF50; /* Green */
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            flex: 1; /* 让按钮平分宽度 */
            margin: 0 5px; /* 按钮之间的间距 */
        }
        .queue-button:hover,
        .show-queue-button:hover,
        .start-transcoding-button:hover {
            background-color: #45a049;
        }
        .start-transcoding-button {
            background-color: #FFD700; /* Yellow */
        }
        .start-transcoding-button:hover {
            background-color: #FFC107; /* Lighter Yellow */
        }
    `);
 
    // 创建悬浮窗
    function createFloatingWindow() {
        const title = document.createElement('span');
        title.textContent = '转码助手';
 
        const closeButton = document.createElement('span');
        closeButton.textContent = '×';
        closeButton.className = 'close-button';
        closeButton.onclick = () => div.style.display = 'none';
 
        const div = document.createElement('div');
        div.id = 'cookie-display';
 
        const header = document.createElement('div');
        header.id = 'cookie-display-header';
 
        const content = document.createElement('div');
        content.id = 'cookie-display-content';
 
        const queueButton = document.createElement('button');
        queueButton.textContent = '添加到转码队列';
        queueButton.className = 'queue-button';
        queueButton.onclick = addToTranscodeQueue;
 
        const showQueueButton = document.createElement('button');
        showQueueButton.textContent = '展示待转码队列';
        showQueueButton.className = 'show-queue-button';
        showQueueButton.onclick = showTranscodingQueue;
 
        const startTranscodingButton = document.createElement('button');
        startTranscodingButton.textContent = '开始转码队列';
        startTranscodingButton.className = 'start-transcoding-button';
        startTranscodingButton.onclick = startTranscodingQueue; // 触发转码队列的函数
 
        // 使用容器实现按钮排列
        const buttonContainer = document.createElement('div');
        buttonContainer.style.display = 'flex';
        buttonContainer.appendChild(queueButton);
        buttonContainer.appendChild(showQueueButton);
        buttonContainer.appendChild(startTranscodingButton); // 添加新按钮
 
        header.appendChild(title);
        header.appendChild(closeButton);
        div.appendChild(header);
        div.appendChild(content);
        div.appendChild(buttonContainer); // 添加按钮容器
 
        return div;
    }
 
    // 获取指定cookie值
    function getCookie(name) {
        const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
        return match ? match[2] : null;
    }
 
    // 获取视频ID
    function getVideoID() {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get('id') || '未找到';
    }
 
    // 更新cookie和ID显示
    function updateDisplay() {
        const content = document.getElementById('cookie-display-content');
        const onlineToken = getCookie('online_token') || '未找到';
        const csrfToken = getCookie('csrfToken') || '未找到';
        const videoID = getVideoID();
 
        content.innerHTML = `
            <strong>online_token:</strong> ${onlineToken}<br>
            <strong>csrfToken:</strong> ${csrfToken}<br>
            <strong>视频ID:</strong> ${videoID}
        `;
    }
 
    // 添加到转码队列的功能
    async function addToTranscodeQueue() {
        const onlineToken = getCookie('online_token');
        const videoID = getVideoID();
 
        if (!onlineToken || videoID === '未找到') {
            alert('未找到在线令牌或视频ID,请检查!');
            return;
        }
 
        const url = 'https://online.njtech.edu.cn/api/v2/automation/media_transcoder/work_queue/items';
        const options = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                Accept: 'application/problem+json',
                Authorization: `Bearer ${onlineToken}`
            },
            body: JSON.stringify({
                episodes: [],
                seasons: [],
                videos: [videoID]
            })
        };
 
        try {
            const response = await fetch(url, options);
            if (response.status === 204) {
                alert('成功添加到转码队列!');
            } else if (response.status === 500) {
                alert('已在队列中!');
            } else {
                const errorData = await response.json();
                console.error(errorData);
                alert('添加到转码队列失败,查看控制台获取详细错误信息。');
            }
        } catch (error) {
            console.error(error);
            alert('发生错误,查看控制台获取详细错误信息。');
        }
    }
 
    // 展示待转码队列的功能
    async function showTranscodingQueue() {
        const onlineToken = getCookie('online_token');
 
        if (!onlineToken) {
            alert('未找到在线令牌,请检查!');
            return;
        }
 
        const url = 'https://online.njtech.edu.cn/api/v2/automation/media_transcoder/work_queue';
        const options = {
            method: 'GET',
            headers: {
                Accept: 'application/json, application/problem+json',
                Authorization: `Bearer ${onlineToken}`
            }
        };
 
        try {
            const response = await fetch(url, options);
            const data = await response.json();
 
            // 提取字符串并展示
            const queueItems = data.queue;
 
            // 创建对话框
            const dialogContent = document.createElement('div');
            if (queueItems.length === 0) {
                dialogContent.innerHTML = '待转码队列: 暂无待转码项。';
            } else {1
                dialogContent.innerHTML = '待转码队列:';
                queueItems.forEach((item, index) => {
                    dialogContent.innerHTML += `${index + 1}. ${item},`; // 添加序号并换行
                });
            }
 
            // 使用 alert 替代或者使用自定义的对话框
            alert(dialogContent.innerHTML); // 此处使用 alert,若要使用自定义对话框,请自行创建并修改样式。
        } catch (error) {
            console.error(error);
            alert('获取待转码队列失败,查看控制台获取详细错误信息。');
        }
    }
 
    // 开始转码队列的功能
    async function startTranscodingQueue() {
        // 显示对话框提示用户转码已开始
        alert("转码已开始");
 
        const url = 'https://online.njtech.edu.cn/api/v2/automation/media_transcoder/transcoding';
        const options = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                Accept: 'application/problem+json',
                Authorization: 'Bearer onlinetoken'
            },
            body: undefined
        };
 
        try {
            const response = await fetch(url, options);
            const data = await response.json();
            console.log(data);
        } catch (error) {
            console.error(error);
        }
    }
 
 
    // 主函数
function init() {
        const floatingWindow = createFloatingWindow();
        document.body.appendChild(floatingWindow);
 
        // 初始更新cookie和ID显示
        updateDisplay();
 
        // 每5秒更新一次cookie和ID显示
        setInterval(updateDisplay, 5000);
    }
 
    // 当页面加载完成后初始化
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();