// ==UserScript==
// @name YouTube Screenshot Helper
// @name:zh-TW YouTube 截圖助手
// @name:zh-CN YouTube 截图助手
// @namespace https://www.tampermonkey.net/
// @version 1.5
// @description YouTube Screenshot Tool – supports hotkey capture, burst mode, customizable hotkeys, burst interval settings, and menu language switch between Chinese and English.
// @description:zh-TW YouTube截圖工具,支援快捷鍵截圖、連拍模式、自定義快捷鍵、連拍間隔、中英菜單切換
// @description:zh-CN YouTube截图工具,支援快捷键截图、连拍模式、自定义快捷键、连拍间隔、中英菜单切换
// @author ChatGPT
// @match https://www.youtube.com/*
// @grant GM_registerMenuCommand
// @grant GM_getValue
// @grant GM_setValue
// @license MIT
// ==/UserScript==
(function () {
'use strict';
// 預設參數
const CONFIG = {
defaultHotkey: 's',
defaultInterval: 1000,
minInterval: 100,
defaultLang: 'EN',
};
// 取得設定值
let screenshotKey = GM_getValue('screenshotKey', CONFIG.defaultHotkey);
let interval = Math.max(parseInt(GM_getValue('captureInterval', CONFIG.defaultInterval)), CONFIG.minInterval);
let lang = GM_getValue('lang', CONFIG.defaultLang);
// 多語系
const I18N = {
EN: {
langToggle: 'LANG EN',
setHotkey: `Set Screenshot Key (Now: ${screenshotKey.toUpperCase()})`,
setInterval: `Set Burst Interval (Now: ${interval}ms)`,
promptKey: 'Enter new hotkey (a-z):',
promptInterval: `Enter new interval (min ${CONFIG.minInterval}ms):`,
},
ZH: {
langToggle: '語言 中文',
setHotkey: `設定截圖快捷鍵(目前:${screenshotKey.toUpperCase()})`,
setInterval: `設定連拍間隔(目前:${interval}ms)`,
promptKey: '請輸入新的快捷鍵(單一字母):',
promptInterval: `請輸入新的連拍間隔(單位ms,最低 ${CONFIG.minInterval}ms):`,
},
};
const t = I18N[lang];
// 狀態變數
let keyDown = false;
let intervalId = null;
// 取得影片元素
const getVideoElement = () => document.querySelector('video');
// 格式化時間
const formatTime = (seconds) => {
const h = String(Math.floor(seconds / 3600)).padStart(2, '0');
const m = String(Math.floor((seconds % 3600) / 60)).padStart(2, '0');
const s = String(Math.floor(seconds % 60)).padStart(2, '0');
const ms = String(Math.floor((seconds % 1) * 1000)).padStart(3, '0');
return `${h}_${m}_${s}_${ms}`;
};
// 取得影片ID
const getVideoID = () => {
const match = window.location.href.match(/[?&]v=([^&]+)/);
return match ? match[1] : 'unknown';
};
// 取得影片標題(多重嘗試,兼容新版YouTube)
const getVideoTitle = () => {
let title =
document.querySelector('h1.ytd-watch-metadata')?.textContent ||
document.querySelector('h1.title')?.innerText ||
document.querySelector('h1')?.innerText ||
document.querySelector('meta[name="title"]')?.getAttribute('content') ||
document.title ||
'unknown';
// 移除檔名不允許的字元
return title.replace(/[\\/:*?"<>|]/g, '').trim();
};
// 截圖主程式
function takeScreenshot() {
const video = getVideoElement();
if (!video) return;
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
const link = document.createElement('a');
const timestamp = formatTime(video.currentTime);
const title = getVideoTitle();
const id = getVideoID();
const resolution = `${canvas.width}x${canvas.height}`;
// 命名規則:時間_影片標題_影片ID_解析度.png
link.download = `${timestamp}_${title}_${id}_${resolution}.png`;
link.href = canvas.toDataURL('image/png');
link.click();
}
// 快捷鍵事件
document.addEventListener('keydown', (e) => {
if (
e.key.toLowerCase() === screenshotKey &&
!keyDown &&
!['INPUT', 'TEXTAREA'].includes(e.target.tagName)
) {
keyDown = true;
takeScreenshot();
intervalId = setInterval(takeScreenshot, interval);
}
});
document.addEventListener('keyup', (e) => {
if (e.key.toLowerCase() === screenshotKey) {
keyDown = false;
clearInterval(intervalId);
}
});
// 油猴選單
GM_registerMenuCommand(t.setHotkey, () => {
const input = prompt(t.promptKey, screenshotKey);
if (input && /^[a-zA-Z]$/.test(input)) {
GM_setValue('screenshotKey', input.toLowerCase());
location.reload();
}
});
GM_registerMenuCommand(t.setInterval, () => {
const input = parseInt(prompt(t.promptInterval, interval));
if (!isNaN(input) && input >= CONFIG.minInterval) {
GM_setValue('captureInterval', input);
location.reload();
}
});
GM_registerMenuCommand(t.langToggle, () => {
GM_setValue('lang', lang === 'EN' ? 'ZH' : 'EN');
location.reload();
});
})();