Skip to content

Instantly share code, notes, and snippets.

@mwinckler
Created April 26, 2012 20:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwinckler/2503070 to your computer and use it in GitHub Desktop.
Save mwinckler/2503070 to your computer and use it in GitHub Desktop.
SC2Planner.com format-for-print bookmarklet
(function() {
var boName = prompt('What do you want to name this build order?');
var supply = 6;
var orders = $('<ol></ol>').css({'list-style-type':'none'});
// For the duration this window is displayed, suspend the
// document events trapping selection/mousedown/etc so that
// the user can select/copy/paste the build order. See the
// click handler on the close buttons for event restoration.
var docEvents = jQuery.extend(true, {}, $(document).data('events'));
$(document).unbind();
function restoreEvents() {
for (var key in docEvents) {
for (var i = 0; i < docEvents[key].length; i++) {
$(document).bind(key, docEvents[key][i].handler);
}
}
docEvents = {};
}
function padZero(str, totalLength) {
str = String(str);
totalLength = parseInt(totalLength);
if (str.length >= totalLength) {
return str;
}
return (new Array(totalLength - str.length + 1).join('0')) + str;
}
$('#build .order').each(function() {
var orderNumber = $(this).attr('id').match(/\d+$/)[0];
var s = parseInt($(this).find('#order_caption_' + orderNumber).text() || supply);
var itemName = $('#actionText_'+orderNumber).text();
var startSeconds = parseInt($('#actionHighlight_'+orderNumber).attr('starttime')) / 100;
var formattedTime = padZero(Math.floor(startSeconds / 60), 2) + ':' + padZero(Math.floor(startSeconds % 60), 2);
var li = $('<li></li>').text(s + ': ' + itemName).prepend(
$('<span></span>').addClass('printhack_time').text('[' + formattedTime + ']')
);
if (itemName.match(/^scv|drone|probe$/i)) {
li.addClass('printhack_worker');
}
orders.append(li);
supply = s + 1;
});
var outputWindow = $('<div></div>').css({'position':'absolute','left':'50%', 'top':'40px','z-index':'10000','max-width':'900px'}).addClass('printhack_window').append(
$('<div></div>').css({
'position':'relative',
'left':'-50%',
'right':'0',
'margin-left':'auto',
'margin-right':'auto',
'top':'20px',
'background':'#fff',
'overflow':'auto',
'text-align':'left',
'border-radius':'10px',
'box-shadow':'0 4px 4px #000',
'padding':'10px 20px'
}).append(
$('<input type="button"></input>').addClass('printhide').val('Close').css({'position':'absolute','top':'10px','right':'10px'}).click(function() { restoreEvents(); $('.printhack_window').remove(); })
).append(
$('<label></label>').addClass('printhide').text('Show workers').prepend(
$('<input type="checkbox"></input>').addClass('printhide').click(function() { $('li.printhack_worker').toggle(); })
).css({'margin':'5px 10px'})
).append(
$('<label></label>').addClass('printhide').text('Show times').prepend(
$('<input type="checkbox"></input>').addClass('printhide').attr('checked','checked').click(function() { $('span.printhack_time').toggle(); })
).css({'margin':'5px 10px'})
).append(
$('<h2></h2>').text(boName)
).append(
$('<a></a>').attr('href', document.location.href).text(document.location.href)
).append(
orders
).append(
$('<input type="button"></input>').addClass('printhide').val('Close').click(function() { restoreEvents(); $('.printhack_window').remove(); })
)
);
$('body').append(outputWindow);
$('li.printhack_worker').hide();
if ($('#printhack_style').length ==0) {
$('<style id="printhack_style" media="print">' + "\n"
+ 'body * { display: none !important; } ' + "\n"
+ 'body .printhack_window, body .printhack_window ol, body .printhack_window li, body .printhack_window h2, body .printhack_window div { display:block !important; } ' +"\n"
+ 'body .printhack_window span, body .printhack_window a { display:inline !important; }' + "\n"
+ 'body .printhide { display: none !important; }</style>').appendTo('head');
}
})();
@mwinckler
Copy link
Author

To make this work: in your browser, create a new bookmark and set its location/URL to:

javascript:(function(){var script=document.createElement('SCRIPT');script.src='https://raw.github.com/gist/2503070/sc2planner_printhack.js';document.body.appendChild(script);})()

Then go to your build order at sc2planner.com and click the bookmark. It should pop up a box with a printable version of the build order (and when you print it, the script hides everything except that printable build order so you don't get all the extra images, etc.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment