User:Isaac (WMF)/WikidataTopic.js

// Based on https://en.wikipedia.org/wiki/User:EpochFail/DraftTopic.js // Adapted for https://tools.wmflabs.org/wiki-topic/ API_PREFIX = "https://wikipedia-topic.wmcloud.org/api/v1/topic?"; MIN_PROBA = 0.15;

var get_and_render_categories = function{ var lang = mw.config.get('wgContentLanguage'); var title = mw.config.get('wgPageName'); var url = API_PREFIX + "lang=" + lang + "&title=" + title + "&threshold=" + MIN_PROBA; var parserOutputNode = $('.mw-parser-output'); var topicsContainer = $(' ').addClass("wiki_page_topics"); parserOutputNode.prepend(topicsContainer); var render_categories = function(data) { //console.log(["rendering", data]); if ("Error" in data) { topicsContainer.append(" " + data["Error"] + " "); }		else { for (var cat in data["results"]) { //console.log(["looping", cat]); topicsContainer.append(data["results"][cat]["topic"] + " (" + Math.round(data["results"][cat]["score"]*100) + "%) "); }		}       topicsContainer.append($('').attr('href', url).text("more from the API").css("font-size", "80%")); }

$.ajax(url, {success: render_categories.bind(this),		        error: function(jqxmlhr, status, error){console.log(status + ": " + error)}}); }

if (mw.config.get('wgNamespaceNumber') === 0) { get_and_render_categories; }