Friday, February 4, 2011

Youtube Shortifier


// ==UserScript==
// @name           Youtube Shortifier
// @namespace      Lunatrius
// @description    Make it short. Period.
// @include        http://www.youtube.com/watch*
// @version        1.1
// @history        1.1 Improved the code that gets the video ID
// @history        1.0 Initial release
// @licence        MIT; http://www.opensource.org/licenses/mit-license.php
// @copyright      2011, Lunatrius
// ==/UserScript==

// check for updates
try {
 ScriptUpdater.check(95865, "1.1");
} catch(e) { };

// get elements by their id attribute
function $(id) {
 return document.getElementById(id);
}

// fade away the element 1/2
function fadeOut(element, time) {
 setTimeout(function(){
  fade(element, (time / 4) / 20, 1);
 }, 3 * time / 4);
}

// fade away the element 2/2
function fade(element, delay, alpha) {
 if(alpha <= 0) {
  element.style.display = "none";
  return;
 }
 element.style.color = ["rgba(255, 0, 0, ", alpha, ")"].join("");
 setTimeout(function(){
  fade(element, delay, alpha - 0.05);
 }, delay);
}

// shortify the youtube link
function setShortURL(event) {
 if(event) {
  if(/video-time-[a-z]+/i.test(event.target.id)) {
   if((parseInt($("video-time-hour").value) * 3600 + parseInt($("video-time-minute").value) * 60 + parseInt($("video-time-second").value)) > parseInt($("video-length").value)) {
    var e = $("video-error");
    e.style.display = "";
    e.style.color = "rgba(255, 0, 0, 2)";
    e.textContent = "The time is set past the video ending!";
    fadeOut(e, 3000);
   }
  }
 }
 var id = $("video-id").value;
 var hd = $("video-hd").checked ? "?hd=1" : "";
 var timeArray = ["#t="];
 if($("video-time-hour").value > 0) timeArray.push([$("video-time-hour").value, "h"].join(""));
 if($("video-time-minute").value > 0) timeArray.push([$("video-time-minute").value, "m"].join(""));
 if($("video-time-second").value > 0) timeArray.push([$("video-time-second").value, "s"].join(""));
 var time = timeArray.length > 1 ? timeArray.join("") : "";
 $("video-url").value = [id, hd, time].join("")
}

// fill out video length selectors
function fillTime(id, limit) {
 for(var i = 0; i <= limit; i++) {
  var option = document.createElement("option");
  option.value = i;
  option.textContent = i;
  $(id).appendChild(option);
 }
}

// main entry point
function main() {
 var div = document.createElement("div");
 div.className = "watch-module";
 div.innerHTML = [
  "<div class=\"watch-module-body\">",
   "<h4>Youtube Shortifier</h4>",
   "<table style=\"width: 100%;\">",
    "<tr>",
     "<td>Time</td>",
     "<td>",
      "<select id=\"video-time-hour\" style=\"display: none;\" />",
      "<select id=\"video-time-minute\" style=\"display: none;\" />",
      "<select id=\"video-time-second\" style=\"display: none;\" />",
     "</td>",
    "</tr>",
    "<tr>",
     "<td>HD?</td>",
     "<td>",
      "<input type=\"checkbox\" id=\"video-hd\" />",
     "</td>",
    "</tr>",
    "<tr>",
     "<td colspan=\"2\">",
      "<input type=\"hidden\" id=\"video-id\" />",
      "<input type=\"hidden\" id=\"video-length\" />",
      "<input type=\"text\" id=\"video-url\" style=\"width: 90%;\" />",
      "<div style=\"display: none; color: rgba(255, 0, 0, 1); padding-top: 4px;\" id=\"video-error\"></div>",
     "</td>",
    "</tr>",
   "</table>",
  "</div>"
 ].join("");
 $("watch-sidebar").insertBefore(div, $("watch-sidebar").firstChild);

 $("video-id").value = document.evaluate("//link[@rel='shortlink']/@href", document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue.nodeValue;

 var limit = 0;
 var time = parseInt($("movie_player").getAttribute("flashvars").match(/&length_seconds=(\d+)/i)[1]);
 $("video-length").value = time;

 limit = Math.floor(time / 3600);
 fillTime("video-time-hour", limit);
 if(time >= 3600)
  $("video-time-hour").style.display = "";

 limit = Math.max(Math.floor((time % 3600) / 60), time >= 3600 ? 59 : 0);
 fillTime("video-time-minute", limit);
 if(time >= 60)
  $("video-time-minute").style.display = "";

 limit = Math.max(time % 60, time >= 60 ? 59 : 0);
 fillTime("video-time-second", limit);
 if(time >= 0)
  $("video-time-second").style.display = "";

 $("video-time-hour").addEventListener("change", setShortURL, false);
 $("video-time-minute").addEventListener("change", setShortURL, false);
 $("video-time-second").addEventListener("change", setShortURL, false);
 $("video-hd").addEventListener("click", setShortURL, false);
 $("video-url").addEventListener("click", function() {$("video-url").select();}, false);

 setShortURL();
}

// execute the script
main();

0 comments:

Post a Comment