User:The Transhumanist/QuickPortal.js

This is an old revision of this page, as edited by The Transhumanist (talk | contribs) at 09:57, 18 February 2019 (fix typo). 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.
// <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 NOT
RUN 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, if
you ctrl-click on 20 such redlinks, you get 20 tabs each with a new
portal 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 a
portal 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.addPortletLink
	mw.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 title
		if (document.title.indexOf("Creating Portal:") != -1) {
			// But only if it is empty
			if ($('#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)
		var str1 = document.getElementById('mw-content-text');
		alert(str1.innerHTML);
		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 page
			if ((document.title.indexOf("/") == -1) && (document.title.indexOf("Search results") == -1)) {
			    //Create linked menu item on side bar menu (in toolbox section)
				var menuItem1 = 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 storage

					if (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();
					} else if (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 displayed
		if (mw.config.get('wgNamespaceNumber') === 0) {
			// But not for a list, index, outline, timeline, or subpage
			if ((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)
				var menuItem2 = 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 ==============

	function insertQuickPortal() {

		var wpTextbox1 = 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=852316213

    	document.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

	}
    
	function invokeEditPage() {

		// Get edit page
		window.___location = window.___location.href.substr(0, window.___location.href.indexOf('#'))+"?action=edit";

		// Development note: remove corresponding localstorage item 
		// localStorage.removeItem('myCat');
	}
	
	function matchingPortal() {

		// Development note: goto matching portal, otherwise, create it
		var currentPath = ___location.pathname;
		var regex = /wiki\//;
		var newPath = currentPath.replace(regex, 'wiki/Portal:');
		window.___location.assign(___location.protocol + "//" + ___location.host + newPath);
	}

	
}) ( mediaWiki, jQuery );       //the end of bodyguard function
// END OF PROGRAM

// </syntaxhighlight>