User:Phlsph7/MonthlyViewsAndWatchers.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.
// shows monthly views, watchers, size, and word count on every page
// it is a modified version of [[User:Þjarkur/Show_number_of_active_watchers_%26_monthly_views_on_every_page.js]]

(function () {
  // get page views
  if (mw.config.get('wgAction') !== 'view') return;
  if (mw.config.get('wgPageName').includes("Special")) return;
  var infoPage = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'info', useLang: 'en' });
  $.ajax({
    url: infoPage,
    type: 'get',
    xhrFields: { withCredentials: true},
    success: function (html) {
      var text = $(html).find('table').text();
      var monthlyViews = text.match(/Page views in the past 30 days(.+)/)[1];
      var totalWatchers = text.match(/Number of page watchers(.+)/)[1];
      var activeWatchers = 0;
      if(text.includes("Number of page watchers who visited recent edits"))
	      activeWatchers = text.match(/Number of page watchers who visited recent edits(.+)/)[1];
      var sideSub = document.getElementById('siteSub');
      sideSub.innerHTML = '(' + monthlyViews + ' monthly views; ' + activeWatchers + '/' + totalWatchers + ' watchers)';
      console.log('views and watchers added');

	  // get length and word count
	  var pageName = mw.config.get('wgPageName')
	  var langCode = "en"
	  var queryURL = "https://" + langCode + ".wikipedia.org/w/api.php?format=xml&action=query&list=search&srsearch=" + pageName + "&srlimit=1"
	  $.ajax({
	    url: queryURL,
	    type: 'get',
	    xhrFields: { withCredentials: true},
	    success: function (html) {
		  var searchQuery = html.documentElement.children[1].children[1].children[0]
	      var size = searchQuery.getAttribute('size')
	      var wordCount = searchQuery.getAttribute('wordcount')
		  var sideSub = document.getElementById('siteSub');
	      sideSub.innerHTML += " (size: " + size + "; wordCount: " + wordCount + ")"
	    }
	  });
    }
  });
})();