// ==UserScript==
// @name Stack Apps Listing Wizard
// @author Nathan Osman
// @version 1.0
// @namespace http://quickmediasolutions.com
// @description Simplifies the process of listing an application / library / UserScript on Stack Apps by asking you a set of questions.
// @include http://stackapps.com/questions/ask
// ==/UserScript==
// This function embeds code on the actual page.
function EmbedCodeOnPage(javascript_code) {
var code_element = document.createElement('script');
code_element.type = 'text/javascript';
code_element.textContent = javascript_code;
document.getElementsByTagName('head')[0].appendChild(code_element);
}
// This function allows us to embed a named function
// on the page that we can invoke from scripts in the page.
function EmbedFunctionOnPage(function_name, function_contents) {
EmbedCodeOnPage(function_contents.toString().replace(/function ?/, 'function ' + function_name));
}
// This function allows us to embed a function on the
// page that will immediately get executed.
function EmbedFunctionOnPageAndExecute(function_contents) {
EmbedCodeOnPage("(" + function_contents.toString() + ")()");
}
// This function executes the provided callback when the dependent
// script has finished loading.
EmbedFunctionOnPage('SAW_LoadDependentScript', function(script_filename, callback) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = script_filename;
script.onload = callback;
document.getElementsByTagName('head')[0].appendChild(script);
});
// This function adds an icon to the toolbar, playing nice with other scripts that
// may have also injected buttons on the toolbar
EmbedFunctionOnPage('SAW_AddToolbarButton', function(toolbar, icon, tooltip, callback) {
// First, retrieve the offset that this new button will assume
var left = toolbar.find('li:not(.wmd-help-button):last').css('left');
if(left !== null)
left = parseInt(left.replace(/\D/g, '')) + 50;
else
left = 400; // assume the default location of extra buttons
// Now create the new button
var button = $('
');
// ...set its click handler
button.click(callback);
// ...and append it to the toolbar
toolbar.append(button);
});
// Generate the listing from the contents of the form
EmbedFunctionOnPage('SAW_GenerateListing', function() {
// Retrieve the values of all of the form elements
var type = $('#saw_type').val();
var name = $('#saw_name').val();
var short_desc = $('#saw_short_desc').val();
var version = $('#saw_version').val();
var desc = $('#saw_desc').val();
var tag = $('#saw_tags').val();
var language = $('#saw_language').val();
var snippet = $('#saw_snippet').val();
var license = $('#saw_license :selected').text();
var license_url = $('#saw_license').val();
var license_other = $('#saw_license_other').val();
var download_url = $('#saw_download_url').val();
var code_url = $('#saw_code_url').val();
var screenshot = $('#saw_screenshot').val();
var thumbnail = $('#saw_thumbnail').val();
var email = $('#saw_email').val();
var extra = $('#saw_extra').val();
// Generate and set the title
var title = name + ' - ' + short_desc;
$('#title').val(title).focus();
// Generate the tags and fill them in
$('#post-form .tag-editor input').val(type + ' ' + tag + ' ').blur();
// Generate the post body
var body = '';
if(thumbnail != '') body += '\n';
if(version != '') body += '\n';
if(type == 'library') body += '\n## Code Snippet\n\n' + snippet + '\n';
else if(screenshot != '') body += '\n## Screenshot\n\n\n';
body += '\n## About\n\n' + desc + '\n\n';
if(extra != '') body += extra + '\n\n';
body += '### License\n\n';
if(license_url == '[os]') body += license_other;
else if(license_url == '[cs]') body += name + ' is closed source.';
else if(license_url == '[nf]') body += 'Commercial software - ' + license_other;
else body += name + ' is released under the ' + '[' + license + '](' + license_url + ').';
body += '\n\n### Download\n\nYou can download ' + name + ' here: \n' + download_url;
body += '\n\n## Contact\n\nI can be reached at `' + email + '`.\n\n';
body += '## Code\n\n' + name + ' is written in ' + language + '.';
if(code_url != '') body += '\n\nThe code can be retrieved from here: \n' + code_url;
// Set the body and refresh the preview
$('#wmd-input').val(body);
StackExchange.MarkdownEditor.refreshAllPreviews();
$('#saw_cover,#saw_window').fadeOut();
});
// Generates a row of buttons along the bottom of a page.
EmbedFunctionOnPage('SAW_GeneratePage', function(page_number, contents) {
// Generate the page DIV
var page_prefix = 'saw_page_';
var page = $('');
// Generate the row of buttons
var button_row = $('');
if(page_number > 1) {
var button_back = $('');
button_back.click(function() { $('#' + page_prefix + page_number).hide(); $('#' + page_prefix + (page_number - 1)).show() });
button_row.append(button_back);
};
var button_next = $('');
if(page_number < 3) button_next.click(function() { $('#' + page_prefix + page_number).hide(); $('#' + page_prefix + (page_number + 1)).show() });
else button_next.click(function() { SAW_GenerateListing(); });
button_row.append(button_next);
// Generate the (up to) two buttons that will be displayed at the bottom
return page.append(contents).append(button_row);
});
// Generates page 1 of the wizard
EmbedFunctionOnPage('SAW_GeneratePage1', function() {
return SAW_GeneratePage(1, '
General - Step 1 of 3
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
');
});
// Generates page 2 of the wizard
EmbedFunctionOnPage('SAW_GeneratePage2', function() {
return SAW_GeneratePage(2, '
Technical Details - Step 2 of 3
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
');
});
// Generates page 3 of the wizard
EmbedFunctionOnPage('SAW_GeneratePage3', function() {
return SAW_GeneratePage(3, '
Final Information - Step 3 of 3
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
');
});
// Generates the contents of the window
EmbedFunctionOnPage('SAW_GenerateWindowContents', function() {
// Begin by generating the close button and setting its action
var close_button = $('
X
');
close_button.click(function() {
// Close the window
$('#saw_cover,#saw_window').fadeOut();
});
// Generate the titlebar and add the close button to it
var title = $('').append(close_button).append('Stack Apps Listing Wizard');
// Generate each of the three pages for the wizard and append them
// (along with the title) to the page.
var window_contents = $('').append(title)
.append(SAW_GeneratePage1())
.append(SAW_GeneratePage2())
.append(SAW_GeneratePage3());
return window_contents;
});
// Prepares the event handlers
EmbedFunctionOnPage('SAW_SetupEventHandlers', function() {
// Set some event handlers for the