Tumblr Videos to HD Redirector and Dashboard Download Button

Automatically promotes Tumblr video links to raw HD versions, and displays a download button below videos on the dashboard

Versión del día 01/09/2017. Echa un vistazo a la versión más reciente.

// ==UserScript==
// @name         Tumblr Videos to HD Redirector and Dashboard Download Button
// @namespace    TumblrVideoReszr
// @description  Automatically promotes Tumblr video links to raw HD versions, and displays a download button below videos on the dashboard
// @version      0.3
// @author       Kai Krause <[email protected]>
// @match        http://*.tumblr.com/*
// @match        https://*.tumblr.com/*
// @run-at       document-start
// ==/UserScript==

// Video URL Patterns
// https://vt.media.tumblr.com/tumblr_ID_NUM.mp4
// https://vtt.tumblr.com/tumblr_ID_NUM.mp4
// https://vt.tumblr.com/tumblr_ID_NUM.mp4

function redirectToHD () {
	var loc = location.toString();
	
	// Check that the URL is a ~.mp4
	if (!loc.endsWith('.mp4')) return;
	
	var lowQuality = /[$_]\d*.mp4$/;
	// Do not redirect if already HD
	if (!loc.match(lowQuality)) return;
	// Change to HD
	loc = loc.replace(lowQuality, '.mp4');

	// If the URL is HTTP, change it to HTTPS
	if (!loc.startsWith('https://')) {
		loc = loc.replace(/^http/, 'https');
	}

	// Redirect to the HD video
	window.location = loc;
}
redirectToHD();

function createDownloadButtons () {
	// Get all tumblr posts on dashboard page
	var posts = document.getElementsByClassName('post_media')
	
	// Get all tumblr posts on a blog - Good luck, they're all iframes.
	/*
	if (posts[0] == null) {
		posts = document.getElementsByClassName('video-wrapper');
	}
	*/
	
	for (var i = 0; i < posts.length; ++i) {
		var videos = posts[i].getElementsByTagName('video');
		if (videos[0] != null) {
			for (var a = 0; a < videos.length; ++a) {
				// if the button already exists, ignore this post
				if (posts[i].querySelector('#videoDownloadButton_kk')) break;
				// Create the button style
				var downloadButton = document.createElement('a');
				downloadButton.innerText = 'Download This Video (HD)';
				downloadButton.style.display = 'block';
				downloadButton.style.width = '100%';
				downloadButton.style.height = '100%';
				downloadButton.style.padding = '4px';
				downloadButton.style.border = '2px solid #2F3D51';
				downloadButton.style.backgroundColor = '#2F3D51';
				downloadButton.style.color = '#979EA8';
				downloadButton.style.fontWeight = '600';
				downloadButton.style.textAlign = 'center';
				// Convert the video preview image URL to a video URL
				var videoURL = videos[a].poster;
				console.log('url is: ' + videoURL);
				videoURL = videoURL.replace(/\d+(?=\.media)/, 'vt');
				videoURL = videoURL.replace(/[^_]*$/, '');
				videoURL = videoURL.replace(/_$/, '.mp4');
				// Set and create the download button
				downloadButton.setAttribute('id', 'videoDownloadButton_kk');
				downloadButton.setAttribute('href', videoURL);
				posts[i].appendChild(downloadButton);
			}
		}
	}
}
setTimeout(createDownloadButtons,1200);
document.addEventListener('scroll', createDownloadButtons);