Jump to content

User:Proteins/switchboard.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Proteins (talk | contribs) at 02:13, 12 July 2009 (test switchboard script by removing myself). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<pre>
// If you are a known user, this script connects you with someone who will help you.
//
// To use this script, add "importScript('User:Proteins/switchboard.js');" to your monobook.js subpage 
// under your user page, as you can see at User:Proteins/monobook.js

var known_users = [
	"Awadewit", "Frank_Schulenburg", "Jennifer_Riggs", "John_Broughton", "SvHannibal", "TimVickers"
];

var known_helpers = [
	"Awadewit", "Frank_Schulenburg", "Jennifer_Riggs", "John_Broughton", "Proteins", "SvHannibal", "TimVickers"
];


// Future expansion idea
var user_descriptions = {

}; 

var helper_descriptions = {

}; 


// The main function

function switchboard() {
	var alert_string = "";
	var confirm_string = "";

	var user_name = "";
	var user_index = 0;
	var num_known_users = 0;
	var is_known_user = false;

	var random_helper = "";
	var num_known_helpers = 0;
 
	var random_double = 0.5;
	var random_helper_index = 0;
 
	var talk_page_href = "";

// Check that the invoker is on the list of known users
	is_known_user = false;
	num_known_users = known_users.length;
	for (user_index=0; user_index<num_known_users; user_index++) {
		if (wgUserName == known_users[user_index]) {
			is_known_user = true;
			break;
		}
	}
	if (is_known_user == false) {
		alert_string = "Sorry, I was unable to find you on the list of known users.\n\nExiting..."
		window.alert(alert_string);
		return;
	}
	user_name = known_users[user_index];

// Select a random helper
	random_helper = user_name;
	num_known_helpers = known_helpers.length;
	while (random_helper == user_name) {
 		random_double = num_known_helpers * Math.random();
		random_helper_index = Math.floor(random_double);
		if ((random_helper_index < 0) || (random_helper_index >= num_known_helpers)) { 
			alert_string = "Error in choosing a random helper to contact - sorry!";
			window.alert(alert_string);
			return;
		}
		random_helper = known_helpers[random_helper_index];
	}

// Confirm that the user wishes to contact this helper
	confirm_string = "Welcome to the Help switchboard, " + user_name + "!\n\nWould you like to leave a message for " + random_helper + "?";
	if (window.confirm(confirm_string) == false) {
		alert_string = "OK, I won't connect you with " + random_helper + ".\n\nPlease click \"help me!\" again if you still need help.  Thanks!";
		window.alert(alert_string);
		return;
	}

// Open the User talk page of the helper
	talk_page_href = wgServer + wgScript + "?title=User_talk:" + random_helper + "&action=edit&section=new&preloadtitle=Help%20request%20from%20an%20KFE%20contributor&preload=User:Proteins/Personal_help_request";
	window.open(talk_page_href);

	return;
} // closes function switchboard()

addOnloadHook(function () {
            addPortletLink('p-cactions', 'javascript:switchboard()', 'help me!', 'ca-switchboard', 'Contact someone who will help you and answer your questions', 'g', '');
});
 
//</pre>