Advertisement
Dotsarecool

Video Interval Calculator v1.1

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