Jump to content

User:Kevin SF/category-stats.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
/* jshint esversion: 8, esnext: false */
console.log("Loaded category-stats.js");
// Show link to the "Massviews Analysis" on category pages
if (mw.config.get("wgCanonicalNamespace") === "Category" && mw.config.get("wgAction") === "view")
{
	let initPromises = [
        mw.loader.using('mediawiki.util'),
        new Promise((resolve) => {
            if (document.readyState === "complete")
            {
                return resolve();
            }
            window.addEventListener("load", () => resolve());
            document.addEventListener("DOMContentLoaded", () => resolve());
        })
    ];
    Promise.allSettled(initPromises).then(() => {
        let baseMassviewLink = "https://pageviews.wmcloud.org/massviews/?platform=all-access&agent=user&source=category&range=latest-60&subjectpage=0&subcategories=1";
        
        mw.util.addPortletLink(
            'p-cactions', // portletId
            baseMassviewLink.concat(`&target=${location.href}`), // href
            'Category Stats', // Text
            'ca-category-page-statistics', // element id
            'Shows pages in this category with statistics, such as view counts', // Tooltip text
            null, // keyboard shortcut key
            // 'nextnode' // Element to be added in front of
        );
    });
}