Jump to content

User:The Transhumanist/P-link.js

From Wikipedia, the free encyclopedia
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">

/* 

P-link.js

Development notes:

What this script will do is create 3 menu items:

P link on category - if on category edit page it places corresponding portal link, 
                if on category page it starts category edit page,
                otherwise it jumps to like-named category page
P link on template - if on template edit page it places corresponding portal link, 
                 if on template page it starts template edit page,
                 otherwise it jumps to like-named template page
P link on root - if on article edit page it places corresponding portal link, 
                 if on article page it starts article edit page,
                 otherwise jumps to like-named article page

The script will work on portal, category, template, and article pages.

This is a rudimentary version, and so it will require that the desired menu item be clicked on as a separate action
up to 3 times.

*/

// ============== 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 ==============
		// ============== Check program status ===========
		// Development notes:
		// if on category edit page and local storage says status 1
			// jumpAndEdit()
		// else if on category page and local storage says status 2
			// jumpAndEdit()
		// else if on non-category page and local storage says status 3
			// jumpAndEdit()

		// ============== When started fresh (not a reiteration)
		if ((document.title.indexOf("Portal:") != -1) || (document.title.indexOf("Category:") != -1) || (document.title.indexOf("Template:") != -1) || (mw.config.get('wgNamespaceNumber') === 0)) {
			pLinkOnTemp();
			pLinkOnRoot();
			pLinkOnCat();
		}



		// ============== End of conditionals ==============
		// ============== END OF CORE PROGRAM ==============

    } );
    } );

// ============== Subroutines ==============
		function pLinkOnCat() {
		    //Create linked menu item on side bar menu (in toolbox section)
			var menuItemCat = mw.util.addPortletLink( 'p-tb', '', 'P link on category', 'tbPLinkOnCat', 'on category page places portal link, otherwise jumps to like-named category page', '', 'tbPLinkOnRoot');
			// Bind click handler
			$( menuItemCat ).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...
				jumpAndEdit();
			});
		}

		function pLinkOnRoot() {
		    //Create linked menu item on side bar menu (in toolbox section)
			var menuItemRoot = mw.util.addPortletLink( 'p-tb', '', 'P link on root', 'tbPLinkOnRoot', 'on article page places portal link, otherwise jumps to like-named article page', '', 'tbPLinkOnTemp');
			// Bind click handler
			$( menuItemRoot ).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...
				if (mw.config.get('wgNamespaceNumber') === 0) {
	    			// Invoke a function by its name
   					// (The function itself is defined further down the page, using the word "function"). 
					insertPLinkOnRoot();
				} else if (document.title.indexOf("Category:") === 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();
				// If in root namespace, jump to like-named category page, otherwise replace prefix and jump
				// We have to handle two kinds of pages: articles (no prefix) and prefixed (Portal: or Template:)
				} else if (mw.config.get('wgNamespaceNumber') === 0) {
					jumpToCatFromRoot();
				} else {
					jumptoCatFromPrefix();
				}
			});
		}
	
		function pLinkOnTemp() {
		    //Create linked menu item on side bar menu (in toolbox section)
			var menuItemCat = mw.util.addPortletLink( 'p-tb', '', 'P link on template', 'tbPLinkOnTemp', 'on template page places portal link, otherwise jumps to like-named template page', '', 't-whatlinkshere');
			// Bind click handler
			$( menuItemCat ).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...
				jumpAndEdit();
			});
		}

		function jumpAndEdit() {
			if (document.title.indexOf("Editing Category:") != -1) {
    			// Invoke a function by its name
				// (The function itself is defined further down the page, using the word "function"). 
				insertPLinkOnCat();
			} else if (document.title.indexOf("Category:") === 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();
			// If in root namespace, jump to like-named category page, otherwise replace prefix and jump
			// We have to handle two kinds of pages: articles (no prefix) and prefixed (Portal: or Template:)
			} else if (mw.config.get('wgNamespaceNumber') === 0) {
				jumpToCatFromRoot();
			} else {
				jumptoCatFromPrefix();
			}
		}
		
		function insertPLinkOnCat() {
		var wpTextbox1 = document.getElementById('wpTextbox1');

		// Insert portal box 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 = "{" + "{Portal|" + "{" + "{subst:PAGENAME}}}}" + wpTextbox1.value;

		// fill edit summary
		document.getElementById('wpSummary').value += "add portal link";

		// Generate a preview of the page.
		
		$('#wpPreviewWidget > input').click();
		}
	
		function insertPLinkOnRoot() {
		var wpTextbox1 = document.getElementById('wpTextbox1');

		// Using regex, insert portal box into the editing box
		// (See redlinkremover on how to do this, or figure out another way)

    	// ?????? Development note: This needs to replace ==See also== and the 
    	// material that follows it with material including the new list item
		// Developoment note: See AWB for the regexes used 
    	document.editform.wpTextbox1.value = "{" + "{Portal|" + "{" + "{subst:PAGENAME}}}}" + wpTextbox1.value;

		// fill edit summary
		document.getElementById('wpSummary').value += "add inline list item portal link";

		// Generate a preview of the page.
		
		$('#wpPreviewWidget > input').click();
		}
	
		function invokeEditPage() {
		// Get edit page on screen
		window.location = window.location.href.substr(0, window.location.href.indexOf('#'))+"?action=edit";
		}
	
		function jumpToCatFromRoot() {
			var currentPath = location.pathname;
			var regex = /wiki\//;
			var newPath = currentPath.replace(regex, 'wiki/Category:');
			window.location.assign(location.protocol + "//" + location.host + newPath);
		}
	
		function jumptoCatFromPrefix() {
			var currentPath2 = location.pathname;
			var regex2 = /wiki\/.*?:/;
			var newPath2 = currentPath2.replace(regex2, 'wiki/Category:');
			window.location.assign(location.protocol + "//" + location.host + newPath2);
		}

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

// </syntaxhighlight>