Advertisement
Dotsarecool

Video Interval Calculator

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