User:Evad37/CatMainTalkSwitch.js

// This script is derived from Wikipedia user Equazcion's script "CatListMainTalkLinks" ( https://en.wikipedia.org/wiki/User:Equazcion/CatListMainTalkLinks.js ), which is released under the Creative Commons Attribution-Share-Alike License 3.0 ( http://creativecommons.org/licenses/by-sa/3.0/ ) // See also https://en.wikipedia.org/wiki/User:Equazcion/CatListMainTalkLinks for documentation of the original script $( function($) {   if(wgNamespaceNumber != 14) {        // only operate in category namespace        return;    }    var portletlink = mw.util.addPortletLink('p-tb', '#', 'Switch links to talk namespace', 'ca-cattalkswitch');    $(portletlink).click( function(e) {    // SWITCH SUBJECT NAMESPACES TO TALK NAMESPACES

// Grab page list elements var pages = $("#mw-pages li"); // Create page var var page; $.each(pages, function(k, page) { // Iterate through list elements; each step below runs on each list element           // Work with the link within each list element from now on           page = $(page).find('a');

// Clone the link var pageClone = page.clone(true);

// If the link isn't a talk page, start constructing the talk page link if (page.attr('href').indexOf("talk:") == -1 && pageClone.attr('href').indexOf("Talk:") == -1){

// Hide original link page.setAttribute('style', 'display:none;'); // Set talk page link text pageClone.append(" (talk)"); // Do a dumb replace of the original URL and tooltip pageClone.attr('title', pageClone.attr('title').replace("Wikipedia:", "Wikipedia talk:")); pageClone.attr('title', pageClone.attr('title').replace("User:", "User talk:")); pageClone.attr('title', pageClone.attr('title').replace("Template:", "Template talk:")); pageClone.attr('title', pageClone.attr('title').replace("Portal:", "Portal talk:")); pageClone.attr('title', pageClone.attr('title').replace("Category:", "Category talk:")); pageClone.attr('title', pageClone.attr('title').replace("MediaWiki:", "MediaWiki talk:")); pageClone.attr('title', pageClone.attr('title').replace("Help:", "Help talk:")); pageClone.attr('title', pageClone.attr('title').replace("Book:", "Book talk:")); pageClone.attr('title', pageClone.attr('title').replace("Module:", "Module talk:")); pageClone.attr('href', pageClone.attr('href').replace("Wikipedia:", "Wikipedia talk:")); pageClone.attr('href', pageClone.attr('href').replace("User:", "User talk:")); pageClone.attr('href', pageClone.attr('href').replace("Template:", "Template talk:")); pageClone.attr('href', pageClone.attr('href').replace("Portal:", "Portal talk:")); pageClone.attr('href', pageClone.attr('href').replace("Category:", "Category talk:")); pageClone.attr('href', pageClone.attr('href').replace("MediaWiki:", "MediaWiki talk:")); pageClone.attr('href', pageClone.attr('href').replace("Help:", "Help talk:")); pageClone.attr('href', pageClone.attr('href').replace("Book:", "Book talk:")); pageClone.attr('href', pageClone.attr('href').replace("Module:", "Module talk:")); // If none of those caught it, it must be an article space link, so add "Talk:" before the page title if (pageClone.attr('href').indexOf("talk:") == -1){ pageClone.attr('title', "Talk:" + pageClone.attr('title')); pageClone.attr('href', page.attr('href').replace("wiki/", "wiki/Talk:")); }

// Link is done, just need to check if the target exists so we can turn it red if not. // Grab link tooltip (as it now conveniently contains the page title alone, which we need for the Ajax query) var cloneTitle = pageClone.attr('title'); // Perform Ajax query (using jQuery's awesomely-simple Ajax function) to get target's "title" data from MediaWiki API $.ajax({                url:"http://en.wikipedia.org/w/api.php?action=query&titles=" + encodeURIComponent(cloneTitle) + "&format=xml",                  dataType: "xml",                  type: "GET",                 success:parseIt               }); // If Ajax result shows the target doesn't exist, add "new" class to the link to turn it red function parseIt (xml){ var missing = $(xml).find('page').attr('missing');         // Grab the "missing" attribute from the "page" xml tag that's included in "title" data // The "missing" field is only defined (as empty string) in the API when the page doesn't exist, // ...so if "missing" is NOT undefined, the target IS missing. Turn the link red. if (typeof missing != "undefined") pageClone.addClass('new'); }

// Insert the constructed link after the existing one pageClone.insertAfter(page); } // End 'If the link isn't a talk page...'

}); // End 'Iterate through list elements...'

}); // End 'SWITCH SUBJECT NAMESPACES...'

var portletlinktwo = mw.util.addPortletLink('p-tb', '#', 'Switch links to main namespace', 'ca-catmainswitch'); $(portletlinktwo).click( function(e) {   // SWITCH TALK NAMESPACES TO SUBJECT NAMESPACES

// Grab page list elements var pages = $("#mw-pages li"); // Create page var var page; $.each(pages, function(k, page) { // Iterate through list elements; each step below runs on each list element           // Work with the link within each list element from now on           page = $(page).find('a');

// Clone the link var pageClone = page.clone(true);

// If the link isn't a talk page, start constructing the talk page link if (page.attr('href').indexOf("talk:") != -1 || pageClone.attr('href').indexOf("Talk:") != -1){

// Hide original link page.setAttribute('style', 'display:none;'); // Set talk page link text pageClone.append(" (main)"); // Do a dumb replace of the original URL and tooltip pageClone.attr('title', pageClone.attr('title').replace("Talk:", "")); pageClone.attr('title', pageClone.attr('title').replace("Wikipedia talk:", "Wikipedia:")); pageClone.attr('title', pageClone.attr('title').replace("User talk:", "User:")); pageClone.attr('title', pageClone.attr('title').replace("Template talk:", "Template:")); pageClone.attr('title', pageClone.attr('title').replace("Portal talk:", "Portal:")); pageClone.attr('title', pageClone.attr('title').replace("Category talk:", "Category:")); pageClone.attr('title', pageClone.attr('title').replace("MediaWiki talk:", "MediaWiki:")); pageClone.attr('title', pageClone.attr('title').replace("Help talk:", "Help:")); pageClone.attr('title', pageClone.attr('title').replace("Book talk:", "Book:")); pageClone.attr('title', pageClone.attr('title').replace("Module talk:", "Module:")); pageClone.attr('href', pageClone.attr('href').replace("Talk:", "")); pageClone.attr('href', pageClone.attr('href').replace("Wikipedia_talk:", "Wikipedia:")); pageClone.attr('href', pageClone.attr('href').replace("User_talk:", "User:")); pageClone.attr('href', pageClone.attr('href').replace("Template_talk:", "Template:")); pageClone.attr('href', pageClone.attr('href').replace("Portal_talk:", "Portal:")); pageClone.attr('href', pageClone.attr('href').replace("Category_talk:", "Category:")); pageClone.attr('href', pageClone.attr('href').replace("MediaWiki_talk:", "MediaWiki:")); pageClone.attr('href', pageClone.attr('href').replace("Help_talk:", "Help:")); pageClone.attr('href', pageClone.attr('href').replace("Book_talk:", "Book:")); pageClone.attr('href', pageClone.attr('href').replace("Module_talk:", "Module:")); // Link is done, just need to check if the target exists so we can turn it red if not. // Grab link tooltip (as it now conveniently contains the page title alone, which we need for the Ajax query) var cloneTitle = pageClone.attr('title'); // Perform Ajax query (using jQuery's awesomely-simple Ajax function) to get target's "title" data from MediaWiki API $.ajax({                url:"http://en.wikipedia.org/w/api.php?action=query&titles=" + encodeURIComponent(cloneTitle) + "&format=xml",                  dataType: "xml",                  type: "GET",                 success:parseIt               }); // If Ajax result shows the target doesn't exist, add "new" class to the link to turn it red function parseIt (xml){ var missing = $(xml).find('page').attr('missing');         // Grab the "missing" attribute from the "page" xml tag that's included in "title" data // The "missing" field is only defined (as empty string) in the API when the page doesn't exist, // ...so if "missing" is NOT undefined, the target IS missing. Turn the link red. if (typeof missing != "undefined") pageClone.addClass('new'); }

// Insert the constructed link after the existing one pageClone.insertAfter(page); } // End 'If the link is a talk page...'

}); // End 'Iterate through list elements...'

}); // End 'SWITCH TALK NAMESPACES...'

}); // End function