diff --git a/polyglot.js b/polyglot.js index 29e7b42..dff8cae 100644 --- a/polyglot.js +++ b/polyglot.js @@ -504,8 +504,97 @@ jQuery(document).arrive('a[title="Leaders"]', { existing: true }, function() { elem.attr("href", "/users/leaderboard/kata"); }); -jQuery(document).arrive("tr.is-current-player", { existing: true }, function() { - if (!isElementInViewport(this)) { - this.scrollIntoView(); + +function decorateLeaderboardRow(tableRow, boardEntry) { + jQuery(tableRow.children[0]).text(boardEntry.position); + jQuery(tableRow.children[1]).text(boardEntry.username); + jQuery(tableRow.children[2]).text(''); + jQuery(tableRow.children[3]).text(boardEntry.score); +} + +function leaderboardDownloaded(resp) { + if (resp.readyState !== 4) return; + let cwResp = resp.response; + console.info(cwResp); + + const leaderboardEntries = cwResp.data; + const leaderboardRows = jQuery('tr[data-username]'); + + const maxRow = Math.min(leaderboardEntries.length, leaderboardRows.length); + + for(let rowNum = 0; rowNum < maxRow; ++rowNum) { + decorateLeaderboardRow(leaderboardRows[rowNum], leaderboardEntries[rowNum]); + } +} + +function buildLeaderboard(lang) { + + let url = "/api/v1/leaders/ranks/" + lang; + function fetchAborted() { + jQuery.notify("Fetch aborted.", "info"); + } + function fetchError() { + jQuery.notify("ERROR!", "error"); } + let opts = { + method: "GET", + url: url, + onreadystatechange: leaderboardDownloaded, + onabort: fetchAborted, + onerror: fetchError, + context: lang, + responseType: "json" + }; + GM_xmlhttpRequest(opts); +} + +function languageChanged() { + const selectedLanguage = jQuery('#languagesDropdown').val(); + buildLeaderboard(selectedLanguage); +} + +function buildLanguagesDropdown() { + let url = "/api/v1/languages"; + function fetchAborted() { + jQuery.notify("Fetch aborted.", "info"); + } + function fetchError() { + jQuery.notify("ERROR!", "error"); + } + + function makeLangItems(langItems) { + return langItems.map(({id, name}) => ``) + } + + function languagesDownloaded(resp) { + if (resp.readyState !== 4) return; + jQuery('div.leaderboard').prepend(''); + jQuery('div.leaderboard').prepend('
Note from Polyglot script: The feature of language leaderboards is still work in progress. Expect it to be buggy or incomplete.
') + jQuery('#languagesDropdown').change(languageChanged); + } + + let opts = { + method: "GET", + url: url, + onreadystatechange: languagesDownloaded, + onabort: fetchAborted, + onerror: fetchError, + responseType: "json" + }; + GM_xmlhttpRequest(opts); +} + +function buildLanguagesLeaderboardTab() { + jQuery('dl.tabs').append('