Advertisement
Dotsarecool

Video Interval Calculator v1.2

Apr 11th, 2021 (edited)
1,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Video Interval Calculator
  3. // @namespace    com.dotsarecool.util
  4. // @version      1.2
  5. // @description  Provides interval calculator options on YouTube and Twitch videos and video embeds
  6. // @author       IsoFrieze
  7. // @match        http*://*.youtube.com/watch*
  8. // @match        http*://*.youtube.com/embed*
  9. // @match        http*://*.twitch.tv/videos*
  10. // @match        http*://*player.twitch.tv/?video*
  11. // @require      http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
  12. // @grant        none
  13. // ==/UserScript==
  14.  
  15. 'use strict';
  16.  
  17. var start, end;
  18. var done = false;
  19.  
  20. $(document).ready(function() {
  21.   domain = document.location.toString();
  22.   if (domain.includes("youtube")) {
  23.     youtube.initControls(document.getElementById("movie_player"));
  24.     done = true;
  25.   } else if (domain.includes("player.twitch") || domain.includes("twitch.tv/videos")) {
  26.       $(".video-player").on("DOMNodeInserted", function() {
  27.           if (done) return;
  28.           time = $("p[data-a-target=player-seekbar-current-time]");
  29.           if (time.length > 0) {
  30.               done = true;
  31.               twitch.initControls($(".video-player video")[0]);
  32.           }
  33.       });
  34.   }
  35. });
  36.  
  37. var twitch = {
  38.   player: {},
  39.   initControls: function(p) {
  40.       console.log(p);
  41.       player = p;
  42.       interval = document.createElement("div");
  43.       $(interval).attr("class", "player-seek__time");
  44.       $(interval).attr("id", "int-int");
  45.       $("p[data-a-target=player-seekbar-current-time]").after(interval);
  46.       $("#int-int").append("<span id='int-time-exp'>&nbsp;&nbsp;&gt;</span> ");
  47.       $("#int-time-exp").attr("style", "cursor:pointer;");
  48.       $("#int-time-exp").click(function() {
  49.           twitch.expandControls();
  50.       });
  51.       twitch.keyboardFrameAdvance();
  52.   },
  53.   expandControls: function() {
  54.     $("#int-time-exp").html("&nbsp;&nbsp;&lt;&nbsp;&nbsp;");
  55.     $("#int-time-exp").click(function() {
  56.       twitch.contractControls();
  57.     });
  58.     button_s = document.createElement("span");
  59.     $(button_s).attr("id", "int-but-start");
  60.     $(button_s).attr("style", "cursor:pointer;");
  61.     $(button_s).text("00:00.000");
  62.     $(button_s).click(function() {
  63.       start = player.currentTime;
  64.       $("#int-but-start").html(prettyTime(start));
  65.       if (end >= start) {
  66.         $("#int-time-int").html(prettyTime(end - start));
  67.       } else {
  68.         $("#int-time-int").html("??:??.???");
  69.       }
  70.     });
  71.     $("#int-int").append(button_s);
  72.     button_e = document.createElement("span");
  73.     $(button_e).attr("id", "int-but-end");
  74.     $(button_e).attr("style", "cursor:pointer;");
  75.     $(button_e).text("00:00.000");
  76.     $(button_e).click(function() {
  77.       end = player.currentTime;
  78.       $("#int-but-end").text(prettyTime(end));
  79.       if (end >= start) {
  80.         $("#int-time-int").html(prettyTime(end - start));
  81.       } else {
  82.         $("#int-time-int").html("??:??.???");
  83.       }
  84.     });
  85.     $("#int-int").append(" <span id='int-time-sep'>/</span> ");
  86.     $("#int-int").append(button_e);
  87.     $("#int-int").append(" <span id='int-time-equal'>=</span> <span id='int-time-int'>00:00.000</span>");
  88.   },
  89.   contractControls: function() {
  90.     $("#int-time-exp").html("&nbsp;&nbsp;&gt;");
  91.     $("#int-time-exp").click(function() {
  92.       twitch.expandControls();
  93.     });
  94.     $("#int-time-exp ~ span").remove();
  95.   },
  96.   keyboardFrameAdvance: function() {
  97.       $(".video-player").keydown(function(e) {
  98.           if (e.which == 188 || e.which == 190) {
  99.               if (player.paused) {
  100.                   framerate = 60; //idk how to get this
  101.                   player.currentTime = player.currentTime + (1/framerate) * (e.which==188 ? -1 : 1);
  102.               } else {
  103.                   player.pause();
  104.               }
  105.           }
  106.       });
  107.   }
  108. };
  109.  
  110. var youtube = {
  111.   player: {},
  112.   initControls: function(p) {
  113.     console.log(p);
  114.     player = p;
  115.     interval = document.createElement("div");
  116.     $(interval).attr("class", "ytp-time-display notranslate");
  117.     $(interval).attr("id", "int-int");
  118.     $(".ytp-left-controls").append(interval);
  119.     $("#int-int").append("<span id='int-time-exp'>&gt;</span> ");
  120.     $("#int-time-exp").attr("style", "cursor:pointer;");
  121.     $("#int-time-exp").click(function() {
  122.       youtube.expandControls();
  123.     });
  124.   },
  125.   expandControls: function() {
  126.     $("#int-time-exp").html("&lt;&nbsp;&nbsp;");
  127.     $("#int-time-exp").click(function() {
  128.       youtube.contractControls();
  129.     });
  130.     button_s = document.createElement("span");
  131.     $(button_s).attr("id", "int-but-start");
  132.     $(button_s).attr("style", "cursor:pointer;");
  133.     $(button_s).text("00:00.000");
  134.     $(button_s).click(function() {
  135.       start = player.getCurrentTime();
  136.       $("#int-but-start").html(prettyTime(start));
  137.       if (end >= start) {
  138.         $("#int-time-int").html(prettyTime(end - start));
  139.       } else {
  140.         $("#int-time-int").html("??:??.???");
  141.       }
  142.     });
  143.     $("#int-int").append(button_s);
  144.     button_e = document.createElement("span");
  145.     $(button_e).attr("id", "int-but-end");
  146.     $(button_e).attr("style", "cursor:pointer;");
  147.     $(button_e).text("00:00.000");
  148.     $(button_e).click(function() {
  149.       end = player.getCurrentTime();
  150.       $("#int-but-end").text(prettyTime(end));
  151.       if (end >= start) {
  152.         $("#int-time-int").html(prettyTime(end - start));
  153.       } else {
  154.         $("#int-time-int").html("??:??.???");
  155.       }
  156.     });
  157.     $("#int-int").append(" <span id='int-time-sep'>/</span> ");
  158.     $("#int-int").append(button_e);
  159.     $("#int-int").append(" <span id='int-time-equal'>=</span> <span id='int-time-int'>00:00.000</span>");
  160.   },
  161.   contractControls: function() {
  162.     $("#int-time-exp").html("&gt;");
  163.     $("#int-time-exp").click(function() {
  164.       youtube.expandControls();
  165.     });
  166.     $("#int-time-exp ~ span").remove();
  167.   }
  168. };
  169.  
  170. // Number t to string in format "00:00.000"
  171. function prettyTime(t) {
  172.   seconds = Math.floor(t);
  173.   ms = Math.round(1000*(t - seconds));
  174.   sec = seconds % 60;
  175.   min = Math.floor(seconds / 60) % 60;
  176.   hr = Math.floor(seconds / (60*60));
  177.   if (hr > 0) {
  178.     return (hr>9?hr:"0"+hr) + ":" + (min<1?"00":(min>9?min:"0"+min)) + ":" + (sec<1?"00":(sec>9?sec:"0"+sec)) + "." + (ms<1?"000":(ms<10?"00"+ms:(ms<100?"0"+ms:ms)));
  179.   } else {
  180.     return (min<1?"00":(min>9?min:"0"+min)) + ":" + (sec<1?"00":(sec>9?sec:"0"+sec)) + "." + (ms<1?"000":(ms<10?"00"+ms:(ms<100?"0"+ms:ms)));
  181.   }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement