User:Dagger/Widget drafts/API recipe unlocks.js
From Guild Wars 2 Wiki
Jump to navigationJump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
$(function() { if (!window.API || $(".api-unlockedon").length == 0) return; $.when(API.fetch("characters")).then(function(data) { $(".api-unlockedon").each(function() { var recipeIds = this.getAttribute("data-recipeid").split(",").map(function(id) { return parseInt(id); }); if (recipeIds.some(isNaN)) { return; } // If they were specified, parse out the recipe's disciplines. var recipeJobs = this.getAttribute("data-disciplines") || []; if (recipeJobs.length) recipeJobs = recipeJobs.replace(/\s/g, "").toLowerCase().split(/,/); // Construct a list of characters with this recipe unlocked. var characters = []; recipeIds.forEach(function(recipeId) { data.forEach(function(char) { if (char.recipes.indexOf(recipeId) == -1) return; // Mark whether the character has the discipline active. This only works when there's only // one recipeId (because otherwise we'd need some way to specify the crafting discipline // for each recipeId -- or look it up on the API, but that's even more queries to do.). var hasActive = true; if (recipeIds.length == 1 && recipeJobs.length) hasActive = recipeJobs.some(function(job) { return char.activeCrafting.indexOf(job) != -1; }); // Add this character to the list, but only if they're not already in it. if (!characters.some(function(charEntry) { return charEntry.name == char.name })) characters.push({name: char.name, age: char.age, active: hasActive, }); }); }); // Add the list to the page. if (characters.length) { var fragment = document.createDocumentFragment(); // Sort chars that have the crafting profession active to the top. characters.sort(function(a, b) { return b.active - a.active || b.age - a.age; }).forEach(function(character) { $("<span/>", { class: character.active ? "api-craftingactive" : "api-craftinginactive", text: character.name, }).appendTo(fragment); $("<br/>").appendTo(fragment); }); $(this).empty().append(fragment); } else { $(this).html("<i>No characters</i>"); } }); }, function onFail(f) { $(".api-unlockedon").html("<i>API Error</i>").attr("title", JSON.stringify(f)); API.log("Error:", f); }); });