User:Phlsph7/MonthlyViewsAndWatchers.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Phlsph7/MonthlyViewsAndWatchers. |
// 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 + ")"
}
});
}
});
})();