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.
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.
// <syntaxhighlight lang="javascript">/* QuickPortal.js is intended to apply \{\{subst:bpsp\}\} to create a portal, or overwrite an existing portal.IMPORTANT: BEFORE YOU RUN THIS SCRIPT, DEACTIVATE wikEd. QuickPortal WILL NOTRUN RIGHT OTHERWISE.When you ctrl-click on a portal redlink (in Firefox or Chrome), a new portal is created in a new tab in preview mode waiting to be saved. So, ifyou ctrl-click on 20 such redlinks, you get 20 tabs each with a newportal in preview mode waiting for inspection. Regular clicking on a portal redlink creates a new portal for that subject in the current window.The script also presents two menu items:The "Restart portal" menu item appears when a portal base page is displayed. Clicking on that puts the portal in edit mode. Clicking it again (or when aportal is already in edit mode) will replace the portal's content with the new portal design, same as the feature that creates a new portal above.)The "Matching portal" menu item jumps to the like-named portal title.(Development note: if there isn't an existing portal, create it. See the page creation url.)*/// ============== Set up ==============// Start off with a bodyguard function to reserve the aliases mw and $ within // the scope of this function, so that we can rely on them meaning "mediawiki" // and "jQuery", respectively: these alias definitions follow this function's // closing curly bracket at the end of the script.(function(mw,$){// ============== Load dependencies ============== // For support of mw.util.addPortletLinkmw.loader.using(['mediawiki.util'],function(){// ============== ready() event listener/handler ==============// below is jQuery short-hand for $(document).ready(function() { ... });// it makes the rest of the script wait until the page's DOM is loaded and ready$(function(){// End of set up// ============== CORE PROGRAM ==============// ============== Conditionals ==============// Run the insertQuickPortal function if "Creating Portal:" is in the page titleif(document.title.indexOf("Creating Portal:")!=-1){// But only if it is emptyif($('#wpTextbox1').is(':empty')){insertQuickPortal();}}// Create the "Restart portal" menu item for when "Portal:" is not missing from the title,// and the portal is of the old style portal (has the old section "Selected article", that is index returns value for that section title)varstr1=document.getElementById('mw-content-text');alert(str1.html);if((document.title.indexOf("Portal:")!=-1)&&(str1.innerHTML.indexof("Selected article")!=-1)){// But not for a subpage (page with a forward slash in its title) or a search results pageif((document.title.indexOf("/")==-1)&&(document.title.indexOf("Search results")==-1)){//Create linked menu item on side bar menu (in toolbox section)varmenuItem1=mw.util.addPortletLink('p-tb','#','Restart portal','tb-restartportal','Apply this twice to restart portal from scratch');// Bind click handler$(menuItem1).click(function(event){event.preventDefault();// above line prevents any default action, // as we want only the following action to run:// Do some stuff when clicked...// Development note: store pagename in local storageif(document.title.indexOf("Editing Portal:")===0){// Invoke a function by its name// (The function itself is defined further down the page, using the word "function"). insertQuickPortal();}elseif(document.title.indexOf("Portal:")===0){// Invoke a function by its name; this one is like clicking "edit source"// (The function itself is defined further down the page, using the word "function"). invokeEditPage();}});}}// Development note:// Run the insertQuickPortal function if on editing page of portal// that we designated to be restarted in local memory. And we clear// that memory.// This section has 2 problems:// 1) It runs in a perpetual loop (check a timestamp, and run only if// a certain amount of time has elapsed?) (When and how do you remove the// localstorage item?)// 2) It runs on any portal edit page (needs to know which one)//if (document.title.indexOf("Editing Portal:") === 0) {// insertQuickPortal();//}// Create the "Matching portal" menu item for when an article is displayedif(mw.config.get('wgNamespaceNumber')===0){// But not for a list, index, outline, timeline, or subpageif((document.title.indexOf("List of ")==-1)&&(document.title.indexOf("Index of ")==-1)&&(document.title.indexOf("Outline of ")==-1)&&(document.title.indexOf("Timeline of ")==-1)&&(document.title.indexOf("/")==-1)){//Create linked menu item on side bar menu (in toolbox section)varmenuItem2=mw.util.addPortletLink('p-tb','#','Matching portal','tb-matchingportal','Jump to matching portal, or create one');// Bind click handler$(menuItem2).click(function(event){event.preventDefault();// above line prevents any default action, // as we want only the following action to run:// Do some stuff when clicked...// Development note: store pagename in local storage// Invoke a function by its name// (The function itself is defined further down the page, using the word "function"). matchingPortal();});}}});});// ============== Subroutines ==============functioninsertQuickPortal(){varwpTextbox1=document.getElementById('wpTextbox1');// Insert \{\{subst:bpsp\}\} into the editing box// The following is a modification from "Your first script" in// https://en.wikipedia.org/w/index.php?title=Wikipedia:User_scripts/Guide&oldid=852316213document.editform.wpTextbox1.value="{"+"{subst:bpsp}}"+"["+"[Category:Portals needing placement of incoming links]]";// Generate a preview of the page.$('#wpPreviewWidget > input').click();//document.editform.submit(); //this line saves the page}functioninvokeEditPage(){// Get edit pagewindow.___location=window.___location.href.substr(0,window.___location.href.indexOf('#'))+"?action=edit";// Development note: remove corresponding localstorage item // localStorage.removeItem('myCat');}functionmatchingPortal(){// Development note: goto matching portal, otherwise, create itvarcurrentPath=___location.pathname;varregex=/wiki\//;varnewPath=currentPath.replace(regex,'wiki/Portal:');window.___location.assign(___location.protocol+"//"+___location.host+newPath);}})(mediaWiki,jQuery);//the end of bodyguard function// END OF PROGRAM// </syntaxhighlight>