// ==UserScript== // @name 動畫瘋 - 截圖工具 // @namespace hbl917070 // @version 0.2 // @description 在 動畫瘋 加入 截圖 的功能 // @author hbl917070(深海異音) // @match https://ani.gamer.com.tw/animeVideo.php?sn=* // @grant none // @downloadURL none // ==/UserScript== /** * 最後修改日期: * 2019/09/08 * * 說明: * 在 動畫瘋 加入 截圖 的功能 * * 作者小屋: * https://home.gamer.com.tw/homeindex.php?owner=hbl917070 * */ /** * * 版本說明 * * 2019/09/08:修復某些圖片附檔名錯誤的BUG * */ (function () { //要插入的html var s_html = `
截圖
(截圖快速鍵 「F8」
`; var dom_div = document.createElement('div'); dom_div.innerHTML = s_html; //目前截圖的編號(用於刪除圖片) window.ct_nub = 1; //把截圖的按鈕,插入到標題上方 var dom_player = document.getElementsByClassName('anime-title')[0]; dom_player.parentNode.insertBefore(dom_div, dom_player); //繪製圖片用的 canvas var cc = document.createElement('canvas'); document.body.append(cc); //截圖快速鍵 document.body.addEventListener("keydown", function (e) { if (e.keyCode == 119) { //F8 add_ct(); } }); /** * 影片截圖 */ function add_ct() { //取得影片目前的畫面 var video = document.getElementById('ani_video_html5_api'); cc.width = video.videoWidth; cc.height = video.videoHeight; cc.setAttribute('style', 'width:0px; height:0px;'); cc.getContext('2d').drawImage(video, 0, 0, video.videoWidth, video.videoHeight); let base64 = cc.toDataURL("image/jpeg", 0.95); let imgsrc = URL.createObjectURL(dataURItoBlob(base64));// Blob url let imgtime = document.querySelector('.vjs-current-time .vjs-current-time-display').innerHTML;//目前時間 let s_tit = document.querySelector('.anime_name h1').innerHTML; s_tit = s_tit.replace(/[/]/g, ' ').replace(/[\\]/g, ' ').replace(/[:]/g, ':').replace(/[*]/g, ' ') .replace(/["]/g, ' ').replace(/[?]/g, ' ').replace(/[<]/g, ' ').replace(/[>]/g, ' ').replace(/[|]/g, ' ') .replace(/[\t]/g, ' ').replace(/[\n]/g, ' ').replace(/[\r]/g, ' ');//避免windows不支援的檔名 let s_time = imgtime.replace(/[:]/g, ':'); let imgname = s_tit + ' ' + s_time + '.jpg';//圖片檔名 let dom_ct_item = document.createElement('div'); dom_ct_item.setAttribute('class', 'ct_item ' + 'ct_nub_' + window.ct_nub); let shtml = `
{{ct_time}}
`; shtml = shtml.replace(/{{ct_img}}/g, imgsrc) .replace(/{{ct_time}}/g, imgtime) .replace(/{{ct_name}}/g, imgname) .replace(/{{nub}}/g, window.ct_nub); dom_ct_item.innerHTML = shtml; document.getElementById('ct_output').append(dom_ct_item); window.ct_nub += 1; } /** * 刪除圖片 * @param nub */ function ct_delete(nub) { let child = document.querySelector('.ct_nub_' + nub); child.parentNode.removeChild(child); } /** * Blob object 轉 Blob url * @param dataURI */ function dataURItoBlob(dataURI) { // convert base64/URLEncoded data component to raw binary data held in a string var byteString; if (dataURI.split(',')[0].indexOf('base64') >= 0) byteString = atob(dataURI.split(',')[1]); else byteString = unescape(dataURI.split(',')[1]); // separate out the mime component var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; // write the bytes of the string to a typed array var ia = new Uint8Array(byteString.length); for (var i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i); } return new Blob([ia], { type: mimeString }); } window.add_ct = add_ct; window.ct_delete = ct_delete; window.dataURItoBlob = dataURItoBlob; })();