Last active
December 27, 2015 12:29
-
-
Save sugavaneshb/c8d8116d05868416d9ae to your computer and use it in GitHub Desktop.
Script to grab youtube links present in a quora answer. **Still working on it**
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript: function init() { | |
// the minimum version of jQuery we want | |
var v = "1.3.2"; | |
// check prior inclusion and version | |
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) { | |
var done = false; | |
var script = document.createElement("script"); | |
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js"; | |
script.onload = script.onreadystatechange = function() { | |
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) { | |
done = true; | |
} | |
}; | |
document.getElementsByTagName("head")[0].appendChild(script); | |
} | |
} | |
var links; | |
function addButton(buttonName, func) { | |
var element = document.createElement("input"); | |
element.setAttribute("type", "button"); | |
element.setAttribute("value", buttonName); | |
element.setAttribute("name", buttonName); | |
element.setAttribute("onclick", func); | |
return element; | |
//document.flotta.appendChild(element); | |
} | |
function saveTextAsFile() | |
{ | |
var textToWrite = $('#youtube-download-span').text().replace(/www\./g, "\nwww."); | |
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'}); | |
var fileNameToSaveAs = "youtube links"; | |
var downloadLink = document.createElement("a"); | |
downloadLink.download = fileNameToSaveAs; | |
downloadLink.innerHTML = "Download File"; | |
if (window.webkitURL != null) | |
{ | |
// Chrome allows the link to be clicked | |
// without actually adding it to the DOM. | |
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob); | |
} | |
else | |
{ | |
// Firefox requires the link to be added to the DOM | |
// before it can be clicked. | |
downloadLink.href = window.URL.createObjectURL(textFileAsBlob); | |
downloadLink.onclick = destroyClickedElement; | |
downloadLink.style.display = "none"; | |
document.body.appendChild(downloadLink); | |
} | |
downloadLink.click(); | |
} | |
function createPlaylistURL() | |
{ | |
_frontURL = 'http://www.youtube.com/v/'; | |
_middleUrl = '?version=3&loop=1&playlist='; | |
return _frontURL + links[0] + _middleUrl + links.splice(1, links.length - 1); | |
} | |
function createPlaylist() | |
{ | |
var _playlist = document.createElement('a'); | |
_playlist.href = createPlaylistURL(); //http://www.google.com"; | |
_playlist.target = '_blank'; | |
document.body.appendChild(_playlist); | |
_playlist.click(); | |
_playlist.parentNode.removeChild(_playlist); | |
} | |
function grabLinks() { | |
divs = jQuery(".qtext_embed"); | |
_results = []; | |
for (_i = 0; _i < divs.length; _i++) { | |
var yt_id = jQuery(divs[_i]).attr('data-yt-id'); | |
if (yt_id.length > 0) { | |
//alert(yt_id); | |
_results.push(yt_id); | |
} | |
} | |
return _results; | |
} | |
function createLinkText(linkIds) | |
{ | |
var link_div = document.createElement('div'); | |
var yt_front = 'www.youtube.com/watch?v=' | |
for(_i = 0; _i < linkIds.length; _i++) | |
{ | |
var link_text = document.createElement('h3'); | |
link_text.style.color = '#cc0000'; | |
link_text.innerHTML = yt_front + linkIds[_i]; | |
link_div.appendChild(link_text); | |
} | |
return link_div; | |
} | |
function start() { | |
init(); | |
links = grabLinks(); | |
var download_div = document.createElement('span'); | |
download_div.id = 'youtube-download-span'; | |
var style = download_div.style; | |
style.padding = '5px'; | |
style.borderRadius = '1em'; | |
style.lineHeight = '1.6'; | |
style.display = 'inline-block'; | |
style.margin = '5px auto'; | |
style.boxShadow = '4px 4px 3px #999'; | |
style.border = '1px #999 solid'; | |
style.backgroundColor = '#ffe'; | |
download_div.appendChild(createLinkText(links)); | |
var container_div = document.createElement('div'); | |
container_div.style.textAlign = 'center'; | |
download_div.appendChild(addButton("Create playlist", "createPlaylist()")); | |
download_div.appendChild(addButton("Save as Text", "saveTextAsFile()")); | |
container_div.appendChild(download_div); | |
//container_div.style.position = 'absolute'; | |
//container_div.style.top = 0; | |
//jQuery(container_div).appendTo(document.body); | |
document.body.insertBefore(container_div,document.body.firstChild); | |
} | |
start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment