Skip to content

Instantly share code, notes, and snippets.

@archagon
Last active January 1, 2016 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save archagon/8145078 to your computer and use it in GitHub Desktop.
Save archagon/8145078 to your computer and use it in GitHub Desktop.
An attempt at making a bookmarklet to download any YouTube and/or MP4 videos on a page.
(function() {
var appName = "";
var jQueryVersion = "2.0.3";
var jQueryUIVersion = "1.10.3";
var jQueryUITheme = "redmond";
function loadPrerequisites(callback)
{
loadJQuery(function() {
loadJQueryUI(function() {
callback();
});
});
}
function loadJQuery(callback)
{
if (window.jQuery === undefined || window.jQuery.fn.jquery < jQueryVersion)
{
var done = false;
var script = document.createElement("script");
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + jQueryVersion + "/jquery.min.js";
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete"))
{
done = true;
callback();
}
};
document.getElementsByTagName("head")[0].appendChild(script);
}
else
{
callback();
}
}
function loadJQueryUI(callback)
{
var cssURL = "https://ajax.googleapis.com/ajax/libs/jqueryui/" + jQueryUIVersion + "/themes/" + jQueryUITheme + "/jquery-ui.min.css";
$('head').append("<link rel='stylesheet' href='" + cssURL + "' type='text/css' />");
if ($.ui === undefined || $.ui.version < jQueryUIVersion)
{
var script = "http://ajax.googleapis.com/ajax/libs/jqueryui/" + jQueryUIVersion + "/jquery-ui.min.js";
$.getScript(script, function(data, textStatus, jqxhr) {
callback();
});
}
else
{
callback();
}
}
function initMyBookmarklet()
{
(window.myBookmarklet = function() {
video_links = getMP4VideoLinks();
if (video_links.length == 0)
{
showDialog("video-dialog", "No Videos Found", "Sorry! No videos were found.", ["OK"], [function() { $(this).dialog("close"); }]);
}
else
{
video_links.push("Cancel");
actions = [];
for (var i = 0; i < video_links.length; i++)
{
actions.push(function() { $(this).dialog("close"); });
}
showDialog("video-dialog", "Select a Video", "Please select a video to download.", video_links, actions);
}
})();
}
function getMP4VideoLinks()
{
var link_strings = [], links = document.links;
for (var i = 0; i < links.length; i++)
{
if (links[i].href.match(/\.mp4$/i))
{
link_strings.push(links[i].href);
}
}
return link_strings;
}
function showDialog(id, title, text, buttons, callbacks)
{
hid = "#" + id;
if ($(hid).length == 0)
{
var html = "\
<div id='" + id + "' title='" + title + "'>\
<p>" + text + "</p>\
</div>";
$("body").append(html);
//$("div").css("visibility", "hidden");
}
var buttonsObject = new Object();
for (var i = 0; i < buttons.length; i++)
{
buttonsObject[buttons[i]] = callbacks[i];
}
$(hid).dialog({
resizable:false,
width:200,
height:140,
modal:true,
buttons:buttonsObject
});
}
loadPrerequisites(initMyBookmarklet);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment