User:Isaac (WMF)/quality viz.js

// Avoid triggering: https://en.wikipedia.org/wiki/Special:WantedTemplates // Code largely borrowed from: https://en.wikipedia.org/wiki/User:TayIorRobinson/wikigender.js

/** * @typedef ClickstreamLink * @typedef ClickstreamData */ //

(async => {   const QUALITY_API = "https://bulk-quality.wmcloud.org/api/v1/details?";

/**    * Requests the clickstream data for the current page. * @param {String} lang Wikipedia language to query. * @returns {Promise} The latest clickstream data for the page. */   async function fetchData(lang) { let title = mw.config.get("wgPageName"); let response = await fetch(QUALITY_API + "lang=" + lang + "&title=" + title); let data = await response.json; return data; }   /**     * Requests the clickstream data for the current page. * @param {Number} num_views Number of clicks to that link. * @param {Number} max_views Maximum number of clicks to any link. * @returns {String} Alpha value as string based on proportion of pageviews via link. [0.2 - 1]    */    function qualToAlpha(qual_float) { if (qual_float == 0) { return '0'; } else { let alpha = 0.1 + (0.9 * (1 - qual_float)); return alpha.toFixed(2); }   }

/**    * Applies colour to links on the page * @param {ClickstreamLink[]} links The links to colour. */   function applyLinkColours(links) { let pageLinks = document.querySelector("#bodyContent").querySelectorAll("a[href^=\"/wiki/\"]"); // Create an object of all the links on the page. let linksOnPage = {}; for (let link of pageLinks) { let linkText = decodeURIComponent(link.href.split("/")[4].toLowerCase.replace(/_/g, " ")); // Build array of all instances of a link appearing on page if (!(linkText in linksOnPage)) { linksOnPage[linkText] = []; }           linksOnPage[linkText].push(link); }       // Colour the links. for (let link of links) { let linkOnPage = linksOnPage[link.title.toLowerCase.replace(/_/g, " ")]; if (!linkOnPage) continue; let alpha = qualToAlpha(link.qual); for (let l of linkOnPage) { l.style.backgroundColor = "rgba(216,127,165," + alpha + ")"; }       }    }

// article but not Main Page if (mw.config.get('wgNamespaceNumber') == 0 && mw.config.get('wgWikibaseItemId') != 'Q5296') { let lang = mw.config.get("wgDBname").replace("wiki",""); let mwIndicator = document.createElement("div"); mwIndicator.className = "mw-indicator"; mwIndicator.id = "mw-indicator-linkquality"; document.querySelector(".mw-indicators").appendChild(mwIndicator); let link = document.createElement("a"); link.href = "javascript:void(0)"; link.onclick = async => { link.remove; let data = await fetchData(lang); applyLinkColours(data.details); };	   link.title = "Visualize the quality of outgoing links on the page"; mwIndicator.appendChild(link); let img = document.createElement("img"); img.alt = "Featured Article icon"; img.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Featured_article_star.svg/20px-Featured_article_star.svg.png"; img.decoding = "async"; img.height = 20; img.width = 20; link.appendChild(img); } });