User:Danski454/cat-next.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.
var cat_next = {};
(function (){
	
	function setCategory(category){
		mw.cookie.set("catNextCat", category);
	}
	
	function getCategory(){
		return mw.cookie.get("catNextCat");
	}
	
	function loadInteface(){
		$("#ca-cat-next").remove();//only run once
		var category = getCategory();
		if(!category){
			category = "";
		}
		category = decodeURIComponent(category).replace(/_/g, " ");
		
		var body;
		if (mw.config.get("skin") === "cologneblue") {
			body = $("#mw-content-text");
		} else {
			body = $("#bodyContent");
		}
		
		body.before('<div id="cat-next-config" style="background-color:LightGray;border:1px solid black;padding:3px;">Category: <input type="text" id="cat-next-cat" value="' + category + '" size="40"><button id="cat-next-confirm" onclick="cat_next.setCategoryUnencoded();">Confirm</button></div>');
		
		if (mw.config.get("wgNamespaceNumber") === 14) {
			$("#cat-next-config").append('<button onclick="cat_next.thisCat();" id="cat-next-this-cat">Use current category</button>');
		}
		if (category !== "") {
			$("#cat-next-config").append('<button onclick="cat_next.clearCat();" id="cat-next-clear">Reset</button>');
		}
	}
	
	function setCategoryUnencoded(){
		var category = $("#cat-next-cat").val();
		category = encodeURIComponent(category.replace(/ /g, "_"));
		setCategory(category);
		if (category === "") {
			$("#cat-next-next, #cat-next-clear").remove();
			return;
		}
		if ($("#cat-next-next").length === 0) {
			$("#ca-watch, #ca-unwatch").before('<li id="cat-next-next" class="collapsible"><span><a href="" title="Go to the next page in Category:' 
				+ $("#cat-next-cat").val() + '">Next</a></span></li>');
			$("#cat-next-config").append('<button onclick="cat_next.clearCat();" id="cat-next-clear">Reset</button>');
		}
		$("#cat-next-next a").attr("href", mw.config.get("wgArticlePath").replace("$1", "Special:RandomInCategory/" + category));
		$("#cat-next-next a").attr("title", 'Go to the next page in Category:' + $("#cat-next-cat").val());
	}
	
	function clearCat(){
		$("#cat-next-cat").val("");
		setCategory("");
		$("#cat-next-next, #cat-next-clear").remove();
	}
	
	function thisCat(){
		$("#cat-next-cat").val(mw.config.get("wgTitle"));
		setCategoryUnencoded();
	}
	
	$(document).ready(function(){
		if (mw.config.get("skin") === "minerva") {
			return; //does not work with minerva
		}
		var category = getCategory();
		if (category) {
			$("#ca-watch, #ca-unwatch").before('<li id="cat-next-next" class="collapsible"><span><a href="" title="Go to the next page in Category:' 
				+ decodeURIComponent(category).replace(/_/g, " ") + '">Next</a></span></li>');
			$("#cat-next-next a").attr("href", mw.config.get("wgArticlePath").replace("$1", "Special:RandomInCategory/" + category));
		}
		var portletLink = mw.util.addPortletLink('p-cactions', '#', 'Configure cat next', 'ca-cat-next', 'Configure category next button', '');
		$(portletLink).click(loadInteface);
	});
	
	cat_next.loadInterface = loadInteface;
	cat_next.setCategoryUnencoded = setCategoryUnencoded;
	cat_next.clearCat = clearCat;
	cat_next.thisCat = thisCat;
	
})();

//[[Category:Wikipedia scripts]]