// ==UserScript==
// @name 南工在线转码助手
// @namespace http://tampermonkey.net/
// @version 1.3
// @description Display cookies, video ID, and manage transcoding queue from online.njtech.edu.cn
// @author 千纸鹤也要飞啊
// @match *://online.njtech.edu.cn/video/*
// @grant GM_addStyle
// @downloadURL none
// ==/UserScript==
(function () {
"use strict";
GM_addStyle(`
#cookie-display {
position: fixed;
top: 20px;
right: 20px;
padding: 10px;
border: 3px solid #ccc;
border-radius: 10px;
background-color: var(--global-background);
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
z-index: 9999;
font-size: 12px;
max-width: 300px;
word-break: break-all;
cursor: grab; /* 添加抓取光标 */
}
#cookie-display.dragging {
cursor: grabbing; /* 添加拖动光标 */
}
#cookie-display-header {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
}
#cookie-display-content {
margin-top: 5px;
text-size:16px;
}
.close-button {
cursor: pointer;
padding: 0 5px;
}
.queue-button, .show-queue-button, .start-transcoding-button {
padding: 5px 8px;
background-color: #4CAF50;
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;
}
.start-transcoding-button:hover {
background-color: #FFC107;
}
.danger-button {
padding: 10px;
background-color: red;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
margin-top: 10px;
width: calc(100%);
}
.danger-button:hover {
background-color: darkred;
}
`);
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);
const dangerButton = document.createElement("button");
dangerButton.textContent = "一键转码所有未转码的视频(危险)";
dangerButton.className = "danger-button";
dangerButton.onclick = startTranscodingAll;
header.appendChild(title);
header.appendChild(closeButton);
div.appendChild(header);
div.appendChild(content);
div.appendChild(buttonContainer);
div.appendChild(dangerButton);
// 添加拖动逻辑
let isDragging = false;
let offsetX = 0;
let offsetY = 0;
header.onmousedown = function (e) {
isDragging = true;
div.classList.add("dragging");
offsetX = e.clientX - div.offsetLeft;
offsetY = e.clientY - div.offsetTop;
};
document.onmousemove = function (e) {
if (isDragging) {
div.style.left = `${e.clientX - offsetX}px`;
div.style.top = `${e.clientY - offsetY}px`;
}
};
document.onmouseup = function () {
if (isDragging) {
isDragging = false;
div.classList.remove("dragging");
}
};
return div;
}
function getCookie(name) {
const match = document.cookie.match(
new RegExp("(^| )" + name + "=([^;]+)")
);
return match ? match[2] : null;
}
function getVideoID() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get("id") || "未找到";
}
function updateDisplay() {
const content = document.getElementById("cookie-display-content");
const onlineToken = getCookie("online_token") || "未找到";
const csrfToken = getCookie("csrfToken") || "未找到";
const videoID = getVideoID();
content.innerHTML = `
online_token: ${onlineToken}
csrfToken: ${csrfToken}
视频ID: ${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 {
dialogContent.innerHTML = "待转码队列:";
queueItems.forEach((item, index) => {
dialogContent.innerHTML += `${index + 1}. ${item}
`;
});
}
alert(dialogContent.innerHTML);
} 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);
}
}
async function startTranscodingAll() {
alert("开始转码所有未转码的视频,操作危险!");
const url = 'https://online.njtech.edu.cn/api/v2/automation/media_transcoder/COMPOSCAN_TRANSCODING';
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/problem+json',
Authorization: 'Bearer ' + getCookie("online_token")
},
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);
updateDisplay();
setInterval(updateDisplay, 5000);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
})();