Jump to content

User:Luxfx/Noarticletext.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.
importStylesheet("User:Luxfx/Noarticletext.css");

var noArticleOnboard = (function(jQuery, window, mw) {
	var targetPage = "User:Luxfx/Noarticletext";
	function isCorrectPage() {
		return (mw.config.values['wgPageName'] == targetPage);
	}
	function checkBacklinks() {
		if (jQuery("#naBacklinks").length > 0) {
			var pagename = mw.config.values['wgPageName'];
			
			// overwrite pagename to provide sample results
			pagename = "Livingstone Gold Medal";
			
			var apiCall = jQuery.getJSON('/w/api.php'
				,{
					'action': 'query'
					,'list': 'backlinks'
					,'format': 'json'
					,'bltitle': pagename
					,'bllimit': 10
					,'blnamespace': '0'
				}
				, function(data) {
					if (data && data.query && data.query.backlinks && data.query.backlinks.length > 0) {
						var numlinks = data.query.backlinks.length;
						jQuery("#naBacklinks").html("Quick scan has found " + numlinks + " link(s)!");
					} else {
						jQuery("#naBacklinks").html("No links found by quick scan.");
					}
				}
			);
		}
	}
	function quickSearch() {
		if (jQuery("#naSearch").length > 0) {
			var pagename = mw.config.values['wgPageName'];
			
			// overwrite pagename to provide sample results
			pagename = "Otto Becher";
			
			var apiCall = jQuery.getJSON('/w/api.php'
				,{
					'action': 'query'
					,'list': 'search'
					,'format': 'json'
					,'srsearch': pagename
					,'srlimit': 10
					,'srnamespace': '0'
				}
				, function(data) {
					if (data && data.query && data.query.searchinfo && data.query.searchinfo.totalhits > 0) {
						var numhits = data.query.searchinfo.totalhits;
						jQuery("#naSearch").html("There are at least " + numhits + " articles mentioning this topic.");
					} else {
						jQuery("#naSearch").html("No articles found by quick scan mention this topic.");
					}
				}
			);
		}
	}
	function showScriptArea() {
		jQuery("#naNoscript").hide();
		jQuery("#naWithscript").show();
	}
	return {
		init: function() {
			if (!isCorrectPage()) return false;
			
			showScriptArea();
			checkBacklinks();
			quickSearch();
		}
	};
}(jQuery, window, mw));

addOnloadHook(noArticleOnboard.init);