Advertisement
AnCak

Untitled

Mar 1st, 2023 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Embed Youtube Take Screenshot
  3. // @namespace    http://tampermonkey.net/
  4. // @version      Beta
  5. // @description  ...
  6. // @author       None
  7. // @match        https://www.youtube.com/embed/*
  8. // @icon         https://www.svgrepo.com/show/111512/photo-camera.svg
  9. // @grant        none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13.   'use strict';
  14.  
  15.   const player = document.querySelector(".html5-video-player");
  16.  
  17.   function getPlayerTime(){
  18.     const currentTime = player.getCurrentTime();
  19.     const hour = Math.floor(currentTime/3600),
  20.           min = Math.floor(currentTime%3600/60),
  21.           sec = Math.floor(currentTime%60);
  22.     return (0 < hour ? hour + "h" : "") + (0 < min ? (0 < hour && 10 > min ? "0" : "") + min + "m" : 0 < hour && 0 === min ? "00m" : "0m") + (10 > sec ? "0" : "") + sec + "s"
  23.   }
  24.  
  25.   function cleanFilename(string){
  26.     return string.replace(/[\\/:*?"<>|]+/g,"");
  27.  }
  28.  
  29.  function takeScreenshot(){
  30.    const videoData = player.getVideoData(),
  31.          video = document.querySelector(".video-stream.html5-main-video"),
  32.          canvas = document.createElement("canvas"),
  33.          ctx = canvas.getContext("2d");
  34.  
  35.    canvas.width = video.videoWidth;
  36.    canvas.height = video.videoHeight;
  37.    ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
  38.  
  39.    let dataURL = canvas.toDataURL("image/png");
  40.    const author = videoData.author;
  41.    const title = videoData.title;
  42.    const time = getPlayerTime();
  43.    const filename = cleanFilename(`${author} - ${title} [${time}]`)
  44.    console.log('filename', filename)
  45.  
  46.    canvas.toBlob((blob) => {
  47.      const a = document.createElement("a");
  48.      a.href = URL.createObjectURL(blob);
  49.      a.download = `${filename}.png`
  50.      a.click();
  51.      URL.revokeObjectURL(a.href);
  52.    })
  53.  }
  54.  
  55.  function createSvgButton (className, viewBox, paths, func) {
  56.    const button = document.createElement('button'),
  57.          svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  58.  
  59.    button.className = "ytp-button";
  60.    button.classList.add(`ytp-my-${className}`);
  61.  
  62.    svg.setAttributeNS(null, "viewBox", viewBox);
  63.    svg.setAttributeNS(null, "height", "100%");
  64.    svg.setAttributeNS(null, "width", "100%");
  65.  
  66.    for (var d of paths){
  67.      const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
  68.       path.setAttributeNS(null, "d", d);
  69.       path.setAttributeNS(null, "fill", "#fff");
  70.       svg.appendChild(path);
  71.     }
  72.  
  73.     button.append(svg);
  74.     button.addEventListener('click', takeScreenshot);
  75.     return button;
  76.   };
  77.  
  78.   const captureButton = createSvgButton('capture', '-97.5 -97.5 585.00 585.00' ,
  79.   ['M365,95h-70.855l-15.1-40.267C276.85,48.878,271.253,45,265,45H125c-6.253,0-11.85,3.878-14.045,9.733 L95.855,95H25c-13.807,0-25,11.193-25,25v200c0,13.807,11.193,25,25,25h340c13.807,0,25-11.193,25-25V120 C390,106.193,378.807,95,365,95z M195,125c52.383,0,95,42.617,95,95s-42.617,95-95,95s-95-42.617-95-95S142.617,125,195,125z',
  80.   'M130,220c0,35.841,29.159,65,65,65s65-29.159,65-65s-29.159-65-65-65S130,184.159,130,220z']); // SVG: https://www.svgrepo.com/svg/111512/photo-camera
  81.  
  82.   const rc = document.querySelector('.ytp-right-controls');
  83.   rc.prepend(captureButton);
  84.  
  85. })();
  86.  
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement