User:So9q/FixRedlinks.js

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * User script to fix the redlinks of categories so they never link to the edit
 * page
 * (c) 2021 by [[User:So9q]]. GPLv3 or later.
 */
//(function () {
//( function ( mw, $ ) {
	console.log("redlinks running");
	var namespace = mw.config.get( 'wgNamespaceNumber' );
    if ( namespace == 6 // File: namespace 
    	|| namespace == 14 // Category: namespace 
    	) {
    	var category = []; // array
    	var links = $('a.new');
    	$.each(links, function(){
    		var test = $(this).attr("href").includes("Category:");
    		console.log("category found: "+ test);
    		if (test){
	    		category.push($(this));
    		}
    	});
        console.log("found links:" + links);
        $.each(links, function(){
        	var linktext = $(this).text();
        	$(this).attr("href", 'https://commons.wikimedia.org/wiki/Category:' + linktext);
        });
    }
//});