User:Quarenon/minify.js

/* */

/** * minify.js * * Add a button to minify a CSS or JS page that ends in *.src * * Packer version 3.0 (final) - copyright 2004-2007, Dean Edwards * http://www.opensource.org/licenses/mit-license * http://dean.edwards.name/packer/ */

var srcPageExt = wgPageName.match(/(.*)\.src\.(js|css)$/i); if (srcPageExt != null && wgAction == 'view') { appendCSS('#minifier { padding: 1em; border: 1px solid black; margin-top: 1em; background-color: #EFEFFF; color: black }');

// Prevent errors about missing base API $.getScript('http://deanedwards.googlecode.com/svn/trunk/js/my.js', function {		$.getScript('http://dean.edwards.name/packer/Packer.js');		$.getScript('http://dean.edwards.name/packer/Words.js');

$(document).ready(function {			var $imgLoading = $(' ').attr({src: stylepath + '/common/progress-wheel.gif', width: 16, height: 16, alt: '...'});			var $imgError = $(' ').attr({src: 'http://img12.imageshack.us/img12/508/1248624996error.png', width: 16, height: 16, alt: '!!'});

var oldSize = 0; var newSize = 0;

function cssmin(input) { // remove comments (will strip comment-based hacks!) input = input.replace(/\/\*[\w\W]*?\*\//g, '');

// ws around beginning/end of declaration blocks input = input.replace(/\s*{\s*/g, '{'); input = input.replace(/;?\s*}\s*/g, '}'); // ws surrounding semicolons input = input.replace(/\s*;\s*/g, ';');

// ws between rules and colons input = input.replace(/\s*([{;])\s*([\*_]?[\w\-]+)\s*:\s*(\b|[#'"])/g, '$1$2:$3');				// ws at beginning/end				return $.trim(input);			}

/* Display an error */ function showError(str) { $('#minifyStatus').empty.append($imgError).append(' ' + str); $('#minifier input').attr('disabled', null); }

/* Fetch page content and edit token */ function init { $('#minifier input').attr('disabled', 'disabled'); $('#minifyStatus').empty.append($imgLoading).append(' Loading...');

var ajaxOpts = { data: { 'action': 'query', 'prop': 'info|revisions', 'intoken': 'edit', 'rvprop': 'content', 'titles': wgPageName, 'format': 'json' },					cache: false, dataType: 'json', success: minify, error: function(xhr, error) { showError('Ajax error: ' + error); }, url: wgScriptPath + '/api.php', timeout: 10000 // msec };

$.ajax(ajaxOpts);

// Stop page submittal return false; }

/* Minify and edit page */ function minify(response) { var pages = response.query.pages; var page = null;

for (var i in pages) { page = pages[i]; }				var output = ''; var input = page.revisions[0]['*']; oldSize = input.length;

try { output = ((srcPageExt[2] == 'css') ? cssmin(input) : (new Packer).pack(input, $('#minifyEncode').val, $('#minifyShrink').val)); } catch (e) { showError(e.message); return; }

output = '/*' + wgPageName + ' minified on ' + (new Date).toUTCString + '*/\n' + output; newSize = output.length;

var ajaxOpts = { data: { 'summary': 'Minified ' + wgPageName + '', 'action': 'edit', 'title': $('#minifyPage').val, 'basetimestamp': (page.missing != null) ? page.starttimestamp : page.revisions[0].timestamp, 'starttimestamp': page.starttimestamp, 'token': page.edittoken, 'text': output, 'format': 'json' },					type: 'POST', dataFmt: 'json', success: finished, error: function(xhr, error) { showError('Ajax error: ' + error); }, url: wgScriptPath + '/api.php', timeout: 10000 // msec };

$.ajax(ajaxOpts); }

/* Verify successful edit */ function finished(response) { $('#minifyStatus').empty.text('Minification successful! ' + oldSize + ' bytes \u2192 ' + newSize + ' bytes (' + Math.floor(100 * newSize / oldSize) + '%)'); }

var $page = $('#' + ((skin == 'monaco') ? 'article' : 'content')); var $div = $(' ').attr('id', 'minifier'); var $form = $(' ').attr('action', '#').text('Minify this page as: ').submit(init); $form.append($(' ').attr({name: 'minifyPage', id: 'minifyPage', type: 'text', size: 40, value: srcPageExt[1] + '.' + srcPageExt[2]})); $form.append(' ').append($(' ').attr({type: 'submit', value: 'Minify'}));

// Options for the JS minifier if (srcPageExt[2] == 'js') { $form.append($(' ').attr({name: 'minifyShrink', id: 'minifyShrink', type: 'checkbox'})).append(' ').append($(' ').attr('for', 'minifyShrink').text('Shrink variables')).append(' '); $form.append($(' ').attr({name: 'minifyEncode', id: 'minifyEncode', type: 'checkbox'})).append(' ').append($(' ').attr('for', 'minifyEncode').text('Base62 encode')).append(' '); var $small = $(' '); $small.append('(Powered by ').append($('').attr({href: 'http://dean.edwards.name/packer/', title: '/packer/'}).text('/packer/')).append(' by Dean Edwards)'); $form.append($small); }

$form.append($(' ')).append($(' ').attr('id', 'minifyStatus')); $div.append($form); $page.prepend($div); }); // $(document).ready	}); // $.getScript } // if /* */