User:Proteins/switchboard.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Proteins/switchboard. |
//<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", "John_Broughton", "Proteins", "SvHannibal", "TimVickers"
];
var known_helpers = [
"Awadewit", "John_Broughton", "Proteins", "SvHannibal", "TimVickers"
];
var user_descriptions = { //future expansion idea
};
var helper_descriptions = { //future expansion idea
};
// The main function
function switchboard() {
var alert_string = "";
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 = "I'm sorry, but I was unable to find you on the list of known users.\n\nExiting..."
window.alert(alert_string);
return;
} else {
alert_string = "Welcome to the switchboard, User:" + known_users[user_index] "!";
window.alert(alert_string);
}
// Select a random helper
num_known_helpers = known_helpers.length;
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];
// Check that the user wishes to contact this helper
confirm_string = "Welcome to the Help switchboard, " + known_users[user_index] + "!\n\nWould you like to leave a message for " + random_helper + "?";
if (!window.confirm(confirm_string)) {
alert_string = "As you requested, I've cancelled the request.\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 = "http://en.wikipedia.org/w/index.php?title=User_talk:" + random_helper + "&action=edit§ion=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>