MediaWiki:Gadget-FlashEmbed.js

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes.

  • Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
  • Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
  • Opera, Konqueror and Safari users can just click the Reload button.
  • Chrome: press Ctrl+F5 or Shift+F5
// * @name				FlashEmbed
// * @dependencies		jquery, mediawiki
// * @source
// * @revision    		2.5
// * @revision date		Jan 08 2019
// * @author:			[[User:Bizzeebeever]]
// * @license			unknown
// * @comment			Embed Flash objects. Allows user to disable/enable individual 
// * @comment			Flash objects, enable/disable all on page, or enable site-wide
// * @comment			Only flash objects from defined hosts can be embedded
// * @comment			Whitelisted objects will be enabled by default
// * @comment			Coming soon: a feature allowing you to permanently enable individual
// * @comment			  objects site-wide
// * @comment			Original by [[User:Olipro]].
// * @comment			For use by [[Template:Flash]]

//TODO: allow fine-grained user whitelising?
;( function( $ ) {
	//utility functions
	function bool( str ) {
		return String( str ).match( /true/i ) ? true : false;
	}

	function trunc( a, d ) {
		var digits = d || 20, str = String( a );
		return ( str.length > digits ) ? str.substr( 0, digits ) + '...' : str;
	}

	function stripProtocol( url ) {
		return ( String( url ).match( /^([^:]+:)(\/\/.+$)/ ) || [ '', '', '' ] )[ 2 ]
	}

	function parseProtocol( url ) {
		return ( String( url ).match( /^([^:]+:)\/\// ) || [ '', '' ] )[ 1 ]
	}

	function parseHost( url ) {
		//returns host-name from url string
		return ( String( url ).match( /^\w+:\/\/([^\/]+)\// ) || [ '', '' ] )[ 1 ];
	}



	function buildFlashObject( data ) {
		//following HTML may be out-of-date
		return $( '<embed>', {
			type: 'application/x-shockwave-flash',
			flashvars: data.flashvars,
			src: data.url,
			css: {
				width: data.width,
				height: data.height
			}
		});
	}

	function attachMenuEvents() {
		$( '.fe-disable-object' ).click( function() {
			disable( $( this ).closest( '.fe-object' ) );
		} );
		$( '.fe-enable-all' ).click( enableAll );
		$( '.fe-always-enable' ).click( alwaysEnable );
		$( '.fe-always-disable' ).click( alwaysDisable );
		$( '.fe-disable-all' ).click( disableAll );
	}

	function setMenus() {
		//this may need to be changed to speed things up a bit.
		//perhaps simply toggle the body class?
		var total = $( '.fe-object' ).length;
		if ( $( '.fe-object.fe-disabled' ).length && total > 1 )
			$( '.fe-enable-all' ).show()
		else
			$( '.fe-enable-all' ).hide();
		if ( $( '.fe-object.fe-enabled' ).length && total > 1 )
			$( '.fe-disable-all' ).show()
		else
			$( '.fe-disable-all' ).hide();
		if ( enableFlash ) {
			$( '.fe-always-enable' ).hide();
			$( '.fe-always-disable' ).show();
		}
		else {
			$( '.fe-always-enable' ).show();
			$( '.fe-always-disable' ).hide();
		}
	}

	function enableItem( $o ) {
		if ( $o.hasClass( 'fe-invalid' ) ) return;
		$o.addClass( 'fe-enabled' )
			.removeClass( 'fe-disabled' )
			.append( buildFlashObject( $o.data( 'flashparams' ) ) )
			.find( '.fe-placeholder' ).hide();
	}

	function disableItem( $o ) {
		$o.find( '.fe-placeholder' ).show();
		$o.addClass( 'fe-disabled' )
			.removeClass( 'fe-enabled' )
			.find( 'embed' ).remove();
	}

	function enable( $obj ) {
		$obj.each( function() { enableItem( $( this ) ); } );
		setMenus();
	}

	function disable( $obj ) {
		$obj.each( function() { disableItem( $( this ) ); } );
		setMenus();
	}

	function enableAll() {
		enable( $( '.fe-object.fe-disabled' ) );
	}

	function disableAll() {
		disable( $( '.fe-object.fe-enabled' ) );
	}

	function alwaysEnable() {
		$.cookie( cookieName, true, cookieMeta );
		enableFlash = true;
		enableAll();
		mw.notify( 'Flash has been enabled by default for ' + location.host,
			{ title: 'Flash enabled site-wide' } );
	}

	function alwaysDisable() {
		$.cookie( cookieName, null, cookieMeta );
		enableFlash = false;
		disableAll();
		mw.notify( 'Flash has been disabled by default for ' + location.host,
			{ title: 'Flash disabled site-wide' } );
	}

	/* Url constructor */
	function Url( url ) {
		if ( this == window ) return new Url( url );
		this.host = parseHost( url );
		this.protocol = parseProtocol( url );
		this.relative = stripProtocol( url );
		this.allow = !!( urls[ url ] || urls[ this.relative ] || hosts[ this.host ] )
			|| !!( mw.config.get( 'wgCanonicalNamespace' ).match( userspaces ) &&
				allowExternalFlashInUserspace )
			|| !!allowExternalFlash;
		this.enable = !!( urls[ url ] || urls[ this.relative ] );
		this.protocolSafe = !protocolSafe ? true : this.protocol == 'https:' &&
			this.protocol != location.protocol ? false : true;
		this.toString = function() {
			return url
		};
	}

	function processDiv() {
		//TODO: fix for protocol safety. Pages served via https should not
		//allow embeds served via non-https
		var $t = $( this ),
			url = new Url( $t.find( '.fe-link' ).hide().find( 'a' ).attr( 'href' ) ),
			data = {
				url: url,
				text: ( url.allow ? 'Click to show' : 'Cannot render' ) + ' Flash object from ',
				linktext: url.allow ? url : 'external site',
				width: $t.attr( 'data-fe-width' ) || 400,
				height: $t.attr( 'data-fe-height' ) || 300,
				flashvars: $t.attr( 'data-fe-flashvars' ) || null
			},
			$title = $( '<span class="fe-title">' )
				.text( data.text )
				.append( $( '<a>', {
					href: data.url,
					title: data.url,
					text: trunc( data.linktext || data.url ),
					click: function( e ) { e.stopPropagation() }
				})
			);
		if ( !url.allow ) $t.addClass( 'fe-invalid' );
		$t.data( 'flashparams', data );
		$t.find( '.fe-overlay' ).append( $title )
			.on( 'click', function() { enable( $( this ).parents( '.fe-object' ) ) } );
		( enableFlash || url.enable ? enableItem : disableItem )( $t );
	}

	function init() {
		$( '.fe-object' ).each( processDiv );
		attachMenuEvents();
		setMenus();
	}


	var userspaces = /^(User|User_talk)$/i;
	var strings = {
		en: {
			DEFAULT_ENABLE_MSG: 'Flash has been enabled by default for ',
			DEFAULT_ENABLE_TTL: 'Flash enabled site-wide',
			DEFAULT_DISABLE_MSG: 'Flash has been disabled by default for ',
			DEFAULT_DISABLE_TTL: 'Flash disabled site-wide',
			PLACEHOLDER_1_ALLOW: 'Click to show',
			PLACEHOLDER_1_DENY: 'Cannot render',
			PLACEHOLDER_2: ' Flash object from ',
			PLACEHOLDER_3: 'external site'
		}
	};
	var defaultLang = 'en';
	var cookieName = 'allow_flash_objects';
	var cookieMeta = { path: '/', expires: 180 };
	var enableFlash = bool( $.cookie( cookieName ) );

	//If set to true, external flash will be allowed
	var allowExternalFlash = false;

	//If set to true, external flash will be allowed...in USERSPACE
	var allowExternalFlashInUserspace = false;

	//https: embeds will be allowed on an http: page
	//but not vice-versa
	//TODO: actually implement this
	var protocolSafe = true;

	//embeds from hosts defined as true are allowed
	//embeds from hosts defined as false are NEVER allowed
	//embeds from undefined hosts will not be allowed...unless defined in urls
	//TODO: move hosts and url whitelists to separate file so this gadget
	//	can be reused by other sites
	var hosts = {
		'images.uncyclomedia.co': true,
		'images.uncyc.org': true,
		'www.classicgamesarcade.com': true
	};
	hosts[ location.host ] = true;

	//define the url for a permanently-whitelisted FILE here
	//urls defined here as true will ALWAYS be ENABLED by default
	//urls defined here as false will be DISABLED by default,
	//  ...unless the host is defined in hosts
	//protocol may be explicit OR undefined
	//TODO: whitelisted urls can be from external sites. Is this good?
	var urls = {
		'//images.uncyclomedia.co/uncyclopedia/en/a/a1/UnTunesPlayer.swf': true,
		'//fools.uncyclomedia.co/naturel_clipped.swf': true
	};
	$( document ).ready( init );
})( jQuery );