User:Proteins/formalstartnewarticle.js

// // Create a new article after filling in some forms // // To use this script, add "importScript('User:Proteins/formalstartnewarticle.js');" to your monobook.js subpage // under your user page, as you can see at User:Proteins/monobook.js

function createNewArticle { var alert_string = "";

alert_string = "This function will create a new article."; window.alert(alert_string); } // closes function createNewArticle

function formalStartNewArticle { var alert_string = "";

var child_node;

var user_name = ""; var user_page_link;

var new_article_name = ""; var new_article_prefix = ""; var new_article_title = "Sandbox";

var body_content; var first_paragraph_in_main_article;

var new_article_form; var new_article_form_div;

var temp_form_input; var num_new_article_form_inputs = 0; var new_article_form_input_index = 0; var new_article_form_inputs = new Array;

// Get the bodyContent node body_content = document.getElementById('bodyContent'); if (!body_content) { alert_string = "ERROR: There is no bodyContent node in this article."; window.alert(alert_string); return; }	main_article_paragraphs = body_content.getElementsByTagName("P"); if (!main_article_paragraphs) { alert_string = "ERROR: The bodyContent node has no paragraphs."; window.alert(alert_string); return; }	first_paragraph_in_main_article = main_article_paragraphs[0]; if (!first_paragraph_in_main_article) { alert_string = "ERROR: The main article has no first paragraph."; window.alert(alert_string); return; }

// Create the form and insert it into the present article new_article_form_div = document.createElement("DIV"); if (!new_article_form_div) { alert_string = "ERROR: Unable to create a new DIV element to hold the form."; window.alert(alert_string); return; }	new_article_form_div.className = "start-article-form"; body_content.insertBefore(new_article_form_div, first_paragraph_in_main_article);

alert_string = "Inserted a new DIV element ahead of the first paragraph in the main article."; //	window.alert(alert_string);

// Create a new form in that DIV element new_article_form = document.createElement("FORM"); if (!new_article_form) { alert_string = "ERROR: Unable to create a new FORM element to extract information to start article."; window.alert(alert_string); return; }	new_article_form_div.appendChild(new_article_form);

alert_string = "Inserted a new FORM element into the DIV element for creating a new article."; //	window.alert(alert_string);

// Create the inputs for the form num_new_article_form_inputs = 0;

temp_form_input = document.createElement("INPUT"); if (!temp_form_input) { num_new_article_form_inputs++; alert_string = "ERROR: Unable to create a new INPUT element " + num_new_article_form_inputs + "\n"; window.alert(alert_string); return; }	num_new_article_form_inputs++; temp_form_input.type = "button"; temp_form_input.value = "Start the new article!"; temp_form_input.onclick = "createNewArticle(this.form);"; temp_form_input.className = "input-" + num_new_article_form_inputs;

new_article_form.appendChild(temp_form_input); new_article_form_inputs.push(temp_form_input);

temp_form_input = document.createElement("INPUT"); if (!temp_form_input) { alert_string = "ERROR: Unable to create a new INPUT element 1 for the new article name."; window.alert(alert_string); return; }	num_new_article_form_inputs++; temp_form_input.type = "text"; temp_form_input.name = "article_title"; temp_form_input.className = "input-" + num_new_article_form_inputs;

new_article_form.appendChild(temp_form_input); new_article_form_inputs.push(temp_form_input);

// Get the user name user_page_link = document.getElementById('pt-userpage'); child_node = user_page_link.firstChild; if (!child_node) { alert_string = "ERROR: The pt-userpage link has no children."; window.alert(alert_string); return; }	user_name = child_node.innerHTML; new_article_prefix = "User:" + user_name;

// Get the new article name new_article_name = new_article_prefix + "/" + new_article_title;

// Acknowledgment to the user alert_string = "This script will create the article: " + new_article_name; window.alert(alert_string);

} // closes function formalStartNewArticle

$(function { mw.util.addPortletLink('p-navigation', 'javascript:formalStartNewArticle', 'New article by forms', 'ca-startforms', 'Create a new article after filling in some forms', '&', ''); }); //