User:Lupo/monobook.js
From Wikipedia, the free encyclopedia
If a message on your talk page led you here, please be wary of who left it. The code below could contain malicious content capable of compromising your account; if your account appears to be compromised, it will be blocked. If you are unsure whether the code is safe, you can ask at the appropriate village pump.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
var months = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); var today = new Date (); importScript("User:Ilmari Karonen/edittoolstest.js"); window.testJsEdittools = true; /*** General functions: creating links */ function makerawlink(name, url) { /* Creates a link pointing to "url", displaying "name" */ var link = document.createElement('a'); link.setAttribute('href', url); link.appendChild(document.createTextNode(name)); return link; } function makewikilink (name, url) { return makerawlink (name, '/wiki/' + url); } function makeactivelink (name, script) { var link = makerawlink (name, '#'); link.setAttribute('onclick', 'javascript:' + script); return link; } /*** General functions: setting the edit summary */ function editSummary (text) { if (document.editform == null || document.editform.wpSummary == null) return; var summary = document.editform.wpSummary.value; if (summary == null || summary.length == 0) document.editform.wpSummary.value = text; else { document.editform.wpSummary.value = document.editform.wpSummary.value + " & " + new String (text.charAt (0)).toLowerCase () + text.substring (1); } } /*------------------------------------------------------------------------------------------ Edit lead section link ------------------------------------------------------------------------------------------*/ function getElementsByClassName (class_name) { /* Returns an array of all elements having the class "class_name". This simplified version works only for single classes. If the length of the returned array is zero, no such elements were found. */ var all_obj; var result=new Array(); var j = 0; if (document.all) /* For IE */ all_obj=document.all; else if (document.getElementsByTagName && !document.all) /* For Mozilla/Firefox */ all_obj=document.getElementsByTagName ("*"); for (i = 0; i < all_obj.length; i++) { if (all_obj[i].className == class_name) { result[j] = all_obj[i]; j++; } } return result; } function addLeadSectionTab () { /* Only add new tab if the page is editable and has section editing links. */ if (getElementsByClassName('editsection').length > 0) { var edit_tab = document.getElementById ('ca-edit'); if (edit_tab != null) { var href_for_lead = edit_tab.firstChild.getAttribute ('href') + "§ion=0"; var first_title = getElementsByClassName ('firstHeading'); if (first_title.length > 0) { /* We have a "firstHeading": put "[edit]" to its right */ var edit_div = document.createElement ('div'); edit_div.setAttribute ('class', 'editsection'); if (!document.getElementById ('featured-star')) { edit_div.setAttribute ('style', 'float:right;margin:5px 0px 0px 5px;'); } else { edit_div.setAttribute ('style', 'float:right;margin:5px 15px 0px 5px;'); } edit_div.appendChild (document.createTextNode ('[')); edit_div.appendChild (makerawlink ("edit", href_for_lead)); edit_div.appendChild (document.createTextNode (']')); first_title[0].parentNode.insertBefore (edit_div, first_title[0]); } else { /* No first title found: create a tab */ var new_tab = document.createElement ('li'); new_tab.id = 'ca-editlead'; var link = makerawlink ("Edit lead", href_for_lead); new_tab.appendChild (link); edit_tab.parentNode.appendChild (new_tab); } } } } function setLeadSectionEditSummary () { /* The edit summary is set to "Lead section" if we're editing only the lead section of an article. This is the case iff the URL of the document ends in "§ion=0". */ if ((document.location.href.indexOf ("§ion=0") + 10 == document.location.href.length) && (document.editform != null) && (document.editform.wpSummary != null)) { document.editform.wpSummary.value = "/* Lead section */ "; } } /*------------------------------------------------------------------------------------------ Add block tabs. Useful only for admins. Inspired by [[User:Korath/blockip.js]]. ------------------------------------------------------------------------------------------*/ function get_escaped_username () { var edit_tab = document.getElementById ('ca-edit'); if (edit_tab == null) return ""; var edit_lk = edit_tab.firstChild.getAttribute ('href'); /* Extract the part between "title=" up to "&action=edit" */ edit_lk = edit_lk.substring (edit_lk.indexOf ("title=") + 6); var idx = edit_lk.indexOf ("&") if (idx >= 0) edit_lk = edit_lk.substring (0, idx); /* Discard "User:" or "User talk:" */ idx = edit_lk.indexOf (":"); if (idx >= 0) edit_lk = edit_lk.substring (idx+1); /* If we're on a subpage, take only the part before the first slash. */ idx = edit_lk.indexOf ("/"); if (idx >= 0) edit_lk = edit_lk.substring (0, idx); return edit_lk; } function get_escaped_username_special () { /* Two possibilities: "Special:Contributions/X" or "Special:Contributions&target=X" */ var edit_lk = document.location.href; /* Extract the part after "Special:Contributions" */ edit_lk = edit_lk.substring (edit_lk.indexOf ("Special:Contributions") + 21); var idx = edit_lk.indexOf ("&target="); if (idx >= 0) { /* Second form */ edit_lk = edit_lk.substring (idx + 8); } else { idx = edit_lk.indexOf ("/"); if (idx >= 0) edit_lk = edit_lk.substring (idx + 1); } /* Discard anything that might follow. */ var idx = edit_lk.indexOf ("&") if (idx >= 0) edit_lk = edit_lk.substring (0, idx); return edit_lk; } function do_block_tab (a_tab, escaped_username) { if (a_tab == null) return; var href = "/w/index.php?title=Special%3ABlockip&ip=" + escaped_username; var new_tab = document.createElement ('li'); new_tab.id = 'ca-block'; new_tab.appendChild (makerawlink ("Block", href)); a_tab.parentNode.appendChild (new_tab); } function do_blocklog_tab (a_tab, escaped_username) { if (a_tab == null) return; var href = "/w/index.php?title=Special%3ALog&type=block&user=&page=User%3A" + escaped_username; var new_tab = document.createElement ('li'); new_tab.id = 'ca-blocklog'; new_tab.appendChild (makerawlink ("Block log", href)); a_tab.parentNode.appendChild (new_tab); } function add_inline_log (escaped_username) { /* Hmmm... doesn't work. For some reason, the href is not loaded into the iframe, but on top of the whole page. Also, sizing the iframe doesn't seem to work. Call is commented out below. */ var href = "/w/index.php?title=Special%3ALog&type=block&user=&page=User%3A" + escaped_username; var iframe = document.createElement ('iframe'); iframe.setAttribute ('src', href); iframe.setAttribute ('frameborder', "0"); iframe.style.width = 100+"%"; iframe.style.height = 400+"px"; var form = document.getElementById ('blockip'); if (form == null || form.nextSibling == null) return; form.parentNode.insertBefore (iframe, form.nextSibling); } function addBlockTabs () { if (document.getElementById ('ca-nstab-user') != null) { /* If there is a "user page" tab, we are on a user page, user talk page, subpage thereof, or editing one of these pages. */ var escaped_username = get_escaped_username (); var a_tab = document.getElementById ('ca-edit'); do_block_tab (a_tab, escaped_username); do_blocklog_tab (a_tab, escaped_username); } else if (document.title.indexOf ("User contributions") == 0) { var escaped_username = get_escaped_username_special (); var a_tab = document.getElementById ('ca-article'); do_block_tab (a_tab, escaped_username); do_blocklog_tab (a_tab, escaped_username); } else if (document.title.indexOf ("Block user") == 0) { var form = document.getElementsByName('wpBlockAddress'); if (form != null && form[0] != null) { /* If the form is null, we're on the result page after a user has been blocked! */ do_blocklog_tab (document.getElementById ('ca-article'), escape (form[0].value)); /* add_inline_log (escape (form[0].value)); */ } } } /*------------------------------------------------------------------------------------------ Personal toolbox ------------------------------------------------------------------------------------------*/ function myPortlet() { /* Put together a new portlet and insert it at the top of the left column. */ /* Note: \xa0 is and \xa9 is © */ var newportlet = document.createElement('div'); var content = document.createElement('div'); newportlet.setAttribute('class', 'portlet'); newportlet.setAttribute('id', 'p-lupo-links'); content.setAttribute('class', 'pBody'); content.appendChild (document.createTextNode('ML: ')); content.appendChild (makerawlink('(en)', 'http://mail.wikipedia.org/pipermail/wikien-l/' + (1900 + today.getYear ()) + '-' + months[today.getMonth()] + '/thread.html#end')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makerawlink('(c)', 'http://mail.wikipedia.org/pipermail/commons-l/' + (1900 + today.getYear ()) + '-' + months[today.getMonth()] + '/thread.html#end')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makerawlink('(f)', 'http://mail.wikipedia.org/pipermail/foundation-l/' + (1900 + today.getYear ()) + '-' + months[today.getMonth()] + '/thread.html#end')); content.appendChild (document.createElement('br')); content.appendChild (document.createTextNode('Logs: ')); content.appendChild (makewikilink ('P', 'Special:Log/protect')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('B', 'Special:Log/block')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('D', 'Special:Log/delete')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('I', 'Special:Log/upload')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('M', 'Special:Log/move')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('U', 'Special:Log/newusers')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('N', 'Special:Contributions/newbies')); content.appendChild (document.createElement('br')); content.appendChild (makewikilink ('AfD', "Wikipedia:Articles_for_deletion/Log/" + (1900 + today.getYear ()) + '_' + months[today.getMonth ()] + '_' + today.getDate ())); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('IFD', 'WP:IFD')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('AN', 'WP:AN')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('CPM', 'Wikipedia:How_to_fix_cut_and_paste_moves')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('PUI', 'WP:PUI')); content.appendChild (document.createElement('br')); content.appendChild (makewikilink ('news', 'WP:GO')); content.appendChild (document.createTextNode('\xa0')); content.appendChild (makewikilink ('paper', 'Wikipedia:Wikipedia_Signpost')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('FA', 'WP:FAC')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('FP', 'WP:FPC')); content.appendChild (document.createElement('br')); content.appendChild (makewikilink ('\xa9vios', 'WP:CP')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('\xa9tags', 'Wikipedia:Image_copyright_tags')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('PD', 'WP:PD')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makewikilink ('Upl', 'Special:Upload')); content.appendChild (document.createElement('hr')); content.appendChild (makeactivelink ('\xa9vio', "document.editform.wpTextbox1.value = document.editform.wpTextbox1.value + '\\n\\n\{\{subst:User:Lupo/Do_not_copyvio\}\} \~\~\~\~\'; editSummary (\'Do not copyvio, please\'); document.editform.wpTextbox1.focus();")); content.appendChild (document.createTextNode('\xa0')); content.appendChild (makeactivelink ('src', "document.editform.wpTextbox1.value = document.editform.wpTextbox1.value + '\\n\\n\{\{subst:User:Lupo/Img Src|img=[[:]]\}\} \~\~\~\~'; editSummary ('Info on image, please'); document.editform.wpTextbox1.focus();")) content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makeactivelink ('t', "document.editform.wpTextbox1.value = document.editform.wpTextbox1.value + '\\n\\n\{\{subst:test\}\} \~\~\~\~'; editSummary ('your test'); document.editform.submit ();")); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makeactivelink ('n', "document.editform.wpTextbox1.value = document.editform.wpTextbox1.value + '\\n\\n\{\{subst:test2\}\} \~\~\~\~'; editSummary ('do not post nonsense, please'); document.editform.submit ();")); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makeactivelink ('v', "document.editform.wpTextbox1.value = document.editform.wpTextbox1.value + '\\n\\nDo not [[Wikipedia:Vandalism|vandalize]] or you will be prevented from editing. \~\~\~\~'; editSummary ('stop vandalizing'); document.editform.submit ();")); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makeactivelink ('red', "document.editform.wpTextbox1.value = '#REDIRECT [[]]'; editSummary ('redir'); document.editform.wpMinoredit.checked=1; document.editform.wpTextbox1.focus();")); content.appendChild (document.createTextNode('\xa0')); content.appendChild (makeactivelink ('hi', "document.editform.wpTextbox1.value = document.editform.wpTextbox1.value + '\\n\\n\{\{subst:User:Lupo/Welcome\}\} \~\~\~\~'; editSummary ('Welcome'); document.editform.wpTextbox1.focus();")); content.appendChild (document.createElement('hr')); content.appendChild (makewikilink ('rechts', 'User:Lupo/rechts')); content.appendChild (document.createTextNode('\xa0')); content.appendChild (makerawlink ('(edit)', '/w/wiki.phtml?title=User:Lupo/rechts&action=edit')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makerawlink ('css', '/w/wiki.phtml?title=User:Lupo/monobook.css&action=edit')); content.appendChild (document.createTextNode('\xa0|\xa0')); content.appendChild (makerawlink ('js', '/w/wiki.phtml?title=User:Lupo/monobook.js&action=edit')); var title=document.createElement('h5'); title.setAttribute ('style', 'text-transform: none;'); title.appendChild (document.createTextNode (months[today.getMonth ()] + ' ' + today.getDate () + ', ' + (1900 + today.getYear ()))); newportlet.appendChild(title); newportlet.appendChild(content); /* Insert the newly created portlet in the left column. */ var leftcolumn = document.getElementById ('column-one'); leftcolumn.insertBefore (newportlet, leftcolumn.firstChild); } /*------------------------------------------------------------------------------------------ Other customizations: interwikis to top, bottom tabs ------------------------------------------------------------------------------------------*/ function languageAtTop () { /* Take the #p-lang (if there is one) out of the .portlet sidebar and put it physically into the main column. */ var contents = document.getElementById('column-content'); var langlinks = document.getElementById('p-lang'); if (langlinks != null) { langlinks.parentNode.removeChild (langlinks); contents.insertBefore(langlinks, contents.firstChild); } } function bottomTabs() { /* Duplicate the top "tabs" at the end of the content area. */ var tabs = document.getElementById('p-cactions').cloneNode(true); /* Give all the named items new ids to avoid id clashes with the existing top "tabs". */ tabs.id = 'mytabs'; var listitems = tabs.getElementsByTagName('LI'); for (i=0;i<listitems.length;i++) { if(listitems[i].id) listitems[i].id = 'mytabs-' + listitems[i].id; } document.getElementById('column-content').appendChild(tabs); } function topTabsToRightPlace() { /* Remove the top "tabs" from the .portlet side column (why were they ever put there?) and put them at the top of the content area, where they belong! (This allows me to use simple relative positioning to get a proper layout. I don't have to mess around with absolute positioning. This is necessary for my language links at the top to work properly, but it is cleaner anyway. */ var contents = document.getElementById('column-content'); var tabs = document.getElementById('p-cactions'); tabs.parentNode.removeChild (tabs); contents.insertBefore(tabs, contents.firstChild); } function rmLogo() { /* Physically remove the logo. My CSS doesn't display it anyway, but I don't know whether that is sufficient to prevent the browser loading it. */ var logo = document.getElementById('p-logo'); logo.parentNode.removeChild (logo); } /*------------------------------------------------------------------------------------------ Bug fixes ------------------------------------------------------------------------------------------*/ /* http://bugzilla.wikimedia.org/show_bug.cgi?id=2831 */ function hash_name_exists (hash_name) { if (hash_name == null || hash_name.length == 0) return false; /* Check if there is an element with that id */ if (document.getElementById (hash_name) != null) return true; /* If the hash starts with a non-alphanumeric character, check if there is an anchor with a matching name (the "name" attribute is not an ID...) */ if (/^\W.*/.test (hash_name)) { var anchors = document.getElementsByTagName ('a'); for (i = 0; i < anchors.length; i++) { var name = anchors[i].getAttribute ('name'); if (name != null && name == hash_name) return true; } } return false; } function bug2831Workaround () { /* Hash has a '#' at the beginning */ if (window.location.hash == null) return; var hash_name = window.location.hash.substring (1); var has_5B = /\.5[BD]/.test (hash_name); if (!has_5B && (hash_name.indexOf ('.7C') <= 0)) return; // None of the critical characters if (hash_name_exists (hash_name)) return; // Anchor exists var new_hash = null; if (!has_5B) { /* Only try replacing .7C (pipe) */ new_hash = hash_name.replace (/^.*\.7C(.*)/, '$1'); if (!hash_name_exists (new_hash)) { new_hash = null; } } else { /* First try replacing all */ var new_hash = hash_name.replace(/\.5[BD]/g, ''); if (!hash_name_exists (new_hash)) { /* Next, try replacing more selectively */ new_hash = hash_name.replace (/\.5B\.5B(.*)\.5D\.5D/g, '$1'); if (new_hash == hash_name) new_hash = hash_name.replace (/\.5B\.5B(.*?)(\.5D\.5D)?/g, '$1'); if (!hash_name_exists (new_hash)) { /* Try removing interwikis etc. */ if (new_hash.charAt (0) == ':') { new_hash = new_hash.substring (1); if (!hash_name_exists (new_hash)) { if (/^[a-z]+:/.test (new_hash)) { new_hash = new_hash.substring(new_hash.indexOf (':')+1); if (!hash_name_exists (new_hash)) new_hash = null; } else new_hash = null; } } else new_hash = null; if (new_hash == null) { /* Try to account for piped links */ new_hash = hash_name.replace (/\.5B\.5B.*\.7C(.*)\.5D\.5D/g, '$1'); if (!hash_name_exists (new_hash)) { /* http links with display text */ new_hash = hash_name.replace(/\.5B(?:http|ftp):.*?_(.+)\.5D/g, '$1'); if (!hash_name_exists (new_hash)) { /* Try the same with a greedy match (i.e., last underscore) */ new_hash = hash_name.replace(/\.5B(?:http|ftp):.*_(.+)\.5D/g, '$1'); if (!hash_name_exists (new_hash)) { new_hash = null; } } } } } /* Yes, I know the above regular expressions will fail if there is more than one link in the section title. Any idea to correct that? Note that anyway, there are still a few cases that aren't caught. Some cannot be caught at all (with reasonable effort); try a section heading like "[http://some.example.org]"! */ } } if (new_hash != null) window.location.hash = '#' + new_hash; } function reformatMyPage() { rmLogo(); addLeadSectionTab(); setLeadSectionEditSummary(); addBlockTabs(); myPortlet(); bottomTabs(); topTabsToRightPlace(); /* First the top "tabs"... */ languageAtTop(); /* ... and then the language links */ /* The order is important here: first move the tabs, then the language links, otherwise they will end up in the wrong order (tabs above language links). */ bug2831Workaround(); } if (window.addEventListener) window.addEventListener("load",reformatMyPage,false); else if (window.attachEvent) window.attachEvent("onload",reformatMyPage);

