// ==UserScript== // @name 【伟哥自用】CB站 一键截图 // @description Chaturbate 按 S 键一键截图,并保存成 PNG 格式 // @version 0.1.7.2 // @author 移植自KazurinのB站一键截图 // @license MIT // @namespace CB@https://bbs.tampermonkey.net.cn // @match https://chaturbate.com/lihengdao/ // @match https://chaturbate.com/* // @icon https://www.google.com/s2/favicons?sz=64&domain=chaturbate.com // @grant none // ==/UserScript== // Chaturbate 伟哥のCB站一键截图 (按S键前,应当手动切换至1080p最高清晰度) // 移植自: KazurinのB站一键截图 https://greasyfork.org/zh-CN/scripts/414896-bilibili-一键截图/code 'use strict'; var video, canvas, ctx, link; function lazyInitialize() { if(!canvas) { /* video = document.querySelector('#bilibili-player video') || document.querySelector('.bilibili-player-video video');*/ video = document.querySelector('#vjs_video_3_html5_api') || document.querySelector('video');// chaturbate canvas = document.createElement('canvas'); ctx = canvas.getContext('2d'); link = document.createElement('a'); link.style.display = 'none'; document.body.appendChild(link); } } function getVideoCurrentTime(video) { return Date.now() } function getPerformerName(){ const href = document.URL const segments = new URL(href).pathname.split('/'); const last = segments.pop() || segments.pop(); // Handle potential trailing slash return last || '' // lihengdao } function startDownload(uri) { link.href = uri; link.download = getPerformerName() + '@' + getVideoCurrentTime(video) + '.png'; link.click(); } function onError(error) { console.error('截图失败'); console.error(error); window.alert('截图失败,请重试。如果此问题反复出现,请在 F12 控制台查看错误消息,并向开发者反馈。'); } function downloadScreenshot() { lazyInitialize(); canvas.width = video.videoWidth; canvas.height = video.videoHeight; ctx.drawImage(video, 0, 0, canvas.width, canvas.height); if(typeof(canvas.toBlob) !== 'undefined') { // 如果浏览器支持 canvas.toBlob(),则用 blob URL 下载 canvas.toBlob(function(blob) { try { var url = URL.createObjectURL(blob, 'image/png'); startDownload(url); URL.revokeObjectURL(url); } catch(e) { onError(e); } }); } else { // 否则用 data URL 下载 startDownload(canvas.toDataURL('image/png')); } } function hasActiveInput() { // 检查当前焦点是否在输入框上 const activeTag = document.activeElement.tagName; return activeTag == 'INPUT' || activeTag == 'TEXTAREA'; } document.addEventListener('keydown', function(e) { if(e.keyCode == 83 && !hasActiveInput()) {// press 'S' e.preventDefault(); // 检查是否是video页面 if(document.querySelector('video') === null){ console.log('this is not a video page, screenshot is not supported.') }else{ try { downloadScreenshot(); } catch(e) { onError(e); } } } });