// ==UserScript== // @name B站视频截图 // @namespace http://tampermonkey.net/ // @version 0.2.7 // @description 视频一键截图,提取封面 // @license MIT // @author Bleu_Bleine // @match https://www.bilibili.com/* // @match https://live.bilibili.com/* // @grant GM_setValue // @grant GM_getValue // @require https://cdn.jsdelivr.net/npm/jquery@3.6.4/dist/jquery.min.js // @run-at document-start // @downloadURL https://update.greasyfork.icu/scripts/410670/B%E7%AB%99%E8%A7%86%E9%A2%91%E6%88%AA%E5%9B%BE.user.js // @updateURL https://update.greasyfork.icu/scripts/410670/B%E7%AB%99%E8%A7%86%E9%A2%91%E6%88%AA%E5%9B%BE.meta.js // ==/UserScript== (function() { 'use strict'; var script = document.createElement('script'); script.type = 'text/javascript'; script.append(` Element.prototype._attachShadow = Element.prototype.attachShadow; Element.prototype.attachShadow = function() { return this._attachShadow({mode: 'open'}); } `); (document.head || document.documentElement).appendChild(script); var timeFormat = function(currentTime) { currentTime = parseInt(currentTime); var timeStr = '00_00'; if(currentTime > 0) { var sec = currentTime % 60 <= 9 ? '0'+currentTime % 60 : currentTime % 60; var min = Math.floor(currentTime / 60) <= 9 ? '0'+Math.floor(currentTime / 60) : Math.floor(currentTime / 60); timeStr = min+'_'+sec; } return timeStr; } var onVideoCut = async function() { if(typeof player !=='undefined') { let videoTime = timeFormat(player.getCurrentTime()); let imgUrl = await player.readFrameAsDataURL(); downloadImg(imgUrl,videoTime); return; } if(typeof livePlayer !=='undefined') { let video = livePlayer.getVideoEl() let videoTime = timeFormat(video.currentTime); let imgUrl = livePlayer.capturePic(); downloadImg(imgUrl,videoTime); return; } var video; var videos = $('video').length ? $('video') : $('bwp-video'); $.each(videos,(i,v) => { if(v.currentSrc) video = v; }); if(!video){ var iframe = document.getElementsByTagName('iframe')[0]; video = iframe.contentWindow.document.getElementsByTagName('video')[0]; } if(!video) return; var canvas = document.createElement('canvas'); if($('#videoCut').length > 0){ canvas = document.getElementById('videoCut'); } else{ canvas.setAttribute('id','videoCut'); $('body').append(canvas); } canvas.setAttribute('width',video.videoWidth); canvas.setAttribute('height',video.videoHeight); canvas.style.display = 'none'; var base64; if(video.shadowRoot) { //兼容 Microsoft Edge base64 = $(video.shadowRoot.lastChild).find('canvas')[0].toDataURL('image/jpeg'); }else { var ctx = canvas.getContext('2d'); ctx.drawImage(video,0,0,video.videoWidth,video.videoHeight); base64 = canvas.toDataURL('image/jpeg'); } var videoTime = timeFormat(video.currentTime); var imgUrl = base64; downloadImg(base64,videoTime); } function downloadImg(imgUrl,videoTime) { var eleLink = document.createElement('a'); var videoTitle = $('#viewbox_report > h1.video-title,.live-skin-main-text,.media-wrapper > h1').attr('title'); if(!videoTitle) videoTitle = document.title.replace(/-(?:番剧|电影|电视剧|国创)(-全集)?-高清(?:正版|独家)在线观看-bilibili-哔哩哔哩/,''); if($('#player-title').length > 0) videoTitle = videoTitle+' '+$('#player-title').text() eleLink.download = videoTitle+'_'+videoTime; eleLink.href = imgUrl; eleLink.click(); window.URL.revokeObjectURL(eleLink.href); } async function getCover(bvCode) { let data = await $.getJSON('https://api.bilibili.com/x/web-interface/view?bvid='+bvCode) if(data && data.code == 0) { let url = data.data.pic.replace('http://','https://'); let response = await fetch(url); let blob = await response.blob(); let imgUrl = window.URL.createObjectURL(blob); downloadImg(imgUrl,'封面'); } } let keyDownListener = function(e) {if(e.code == 'KeyS' && $(':focus').length == 0) onVideoCut();} function shortKeyOn(on) { if(on) { window.addEventListener('keydown', keyDownListener); }else { window.removeEventListener('keydown', keyDownListener); } } $(function(){ var styleEl = document.createElement('style'); document.head.appendChild(styleEl); var styleSheet = styleEl.sheet; styleSheet.insertRule('.progress-shadow-show .bilibili-player-video-wrap .top-wrap-cut {visibility: hidden;opacity: 0;}',0); styleSheet.insertRule('.video-control-show .bilibili-player-video-wrap .top-wrap-cut {visibility: visible;opacity: 1;}',0); var url = window.location.href; var bvCode = url.match(/BV[a-zA-Z0-9]+/); var useShortKey = true; const myCookie = GM_getValue('BZSPJTSK'); if(myCookie && myCookie == 'off') { useShortKey = false; } shortKeyOn(useShortKey); if(url.indexOf('live.bilibili.com') !== -1) { setTimeout(function() { $('.icon-left-part').append(``); },2000); }else if(url.indexOf('bangumi') !== -1) { setTimeout(function() { $('.bpx-player-top-title').after(`
截图
`); $('.bpx-player-top-title').after(`
快捷键:${useShortKey ? '开':'关'}
`); },1000); }else { $('#bilibili-player').prepend(``); if(bvCode) { $('#bilibili-player').prepend(``); } $('#bilibili-player').prepend(``); } $('body').on('click','#videoCutBtn',function() {onVideoCut();}); $('body').on('click','#getCoverBtn',function() {getCover(bvCode[0]);}); $('body').on('click','#shortKey',function() { useShortKey = !useShortKey; shortKeyOn(useShortKey); $('#bilibili-player > #shortKey').css('background-color',useShortKey ? '#ff85ad' : '#999'); $('#shortKey.top-wrap-cut').text(`快捷键:${useShortKey ? '开':'关'}`); GM_setValue('BZSPJTSK',useShortKey ? 'on' : 'off'); }); }); })();