MediaWiki:Gadget-VideoResizer.js: Difference between revisions

From the Super Mario Wiki, the Mario encyclopedia
Jump to navigationJump to search
No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Automatically resize videos to fit the page width */
/* Automatically resize videos to fit the page width */


function videoResizer() {
function autoResizer() {
$('video').each(function() {
$('#mw-content-text .autoResize').each(function() {
var body = $('#mw-content-text .mw-parser-output');
var body = $('#mw-content-text .mw-parser-output');
var self = $(this);
var iframe = $(this).find('iframe');
if (typeof self.attr('data-orig-width') == 'undefined') {
self.attr('data-orig-width', self.width());
self.attr('data-orig-height', self.height());
}
if (body.width() < self.attr('data-orig-width')) {
var aspect = self.attr('data-orig-width') / self.attr('data-orig-height');
var newWidth = Math.round(body.width());
var newHeight = Math.round(body.width() / aspect);
self.css('width', newWidth);
self.css('height', newHeight);
}
});
}
function videoJsResizer() {
$('.video-js').each(function() {
var body = $('#mw-content-text .mw-parser-output');
var self = $(this);
var video = self.find('video');
video.removeAttr('style');
if (body.width() < video.attr('data-orig-width')) {
var aspect = video.attr('data-orig-width') / video.attr('data-orig-height');
var newWidth = Math.round(body.width());
var newHeight = Math.round(body.width() / aspect);
self.css('width', newWidth);
self.css('height', newHeight);
} else if (self.width() != video.attr('data-orig-width')) {
self.css('width', video.attr('data-orig-width'));
self.css('height', video.attr('data-orig-height'));
}
});
}
function youtubeResizer() {
$('.autoResize').each(function(){
var body = $('#mw-content-text .mw-parser-output');
var self = $(this);
var iframe = self.find('iframe');
if (typeof iframe.attr('data-orig-width') == 'undefined') {
if (typeof iframe.attr('data-orig-width') == 'undefined') {
iframe.attr('data-orig-width', iframe.width());
iframe.attr('data-orig-width', iframe.width());
iframe.attr('data-orig-height', iframe.height());
iframe.attr('data-orig-height', iframe.height());
}
}
if (body.width() < iframe.attr('data-orig-width')) {
var newWidth = $(this).parent().parent().is('section') ? body.width() : body.width() - 10;
if (newWidth < iframe.attr('data-orig-width')) {
var aspect = iframe.attr('data-orig-width') / iframe.attr('data-orig-height');
var aspect = iframe.attr('data-orig-width') / iframe.attr('data-orig-height');
var newWidth = Math.round(body.width());
var newWidth = Math.ceil(newWidth);
var newHeight = Math.round(body.width() / aspect);
var newHeight = Math.floor(newWidth / aspect);
self.css('width', newWidth);
$(this).css('width', newWidth);
iframe.attr('width', newWidth);
iframe.attr('width', newWidth);
iframe.attr('height', newHeight);
iframe.attr('height', newHeight);
} else if (iframe.width() != iframe.attr('data-orig-width')) {
} else if (iframe.width() != iframe.attr('data-orig-width')) {
self.css('width', iframe.attr('data-orig-width'));
$(this).css('width', iframe.attr('data-orig-width'));
iframe.attr('width', iframe.attr('data-orig-width'));
iframe.attr('width', iframe.attr('data-orig-width'));
iframe.attr('height', iframe.attr('data-orig-height'));
iframe.attr('height', iframe.attr('data-orig-height'));
Line 59: Line 24:
});
});
}
}
$(videoResizer);
$(autoResizer);
$(youtubeResizer);
$(window).resize(function() {
$(window).resize(function() {
$(videoJsResizer);
clearTimeout(window.resizeFinished);
$(youtubeResizer);
window.resizeFinished = setTimeout(function() {
});
$(autoResizer);
 
}, 100);
/* Loop uploaded videos under 15 seconds long */
$(function() {
$('video').each(function() {
if ($(this).attr('data-durationhint') < 15)
$(this).attr('loop', '');
});
});
});

Latest revision as of 05:48, February 21, 2023

/* Automatically resize videos to fit the page width */

function autoResizer() {
	$('#mw-content-text .autoResize').each(function() {
		var body = $('#mw-content-text .mw-parser-output');
		var iframe = $(this).find('iframe');
		if (typeof iframe.attr('data-orig-width') == 'undefined') {
			iframe.attr('data-orig-width', iframe.width());
			iframe.attr('data-orig-height', iframe.height());
		}
		var newWidth = $(this).parent().parent().is('section') ? body.width() : body.width() - 10;
		if (newWidth < iframe.attr('data-orig-width')) {
			var aspect = iframe.attr('data-orig-width') / iframe.attr('data-orig-height');
			var newWidth = Math.ceil(newWidth);
			var newHeight = Math.floor(newWidth / aspect);
			$(this).css('width', newWidth);
			iframe.attr('width', newWidth);
			iframe.attr('height', newHeight);
		} else if (iframe.width() != iframe.attr('data-orig-width')) {
			$(this).css('width', iframe.attr('data-orig-width'));
			iframe.attr('width', iframe.attr('data-orig-width'));
			iframe.attr('height', iframe.attr('data-orig-height'));
		}
	});
}
$(autoResizer);
$(window).resize(function() {
	clearTimeout(window.resizeFinished);
	window.resizeFinished = setTimeout(function() {
		$(autoResizer);
	}, 100);
});