Skip to content

Commit 66de125

Browse files
committed
Added skill requirements and a safeguard in case the RS API returns empty data.
1 parent f40b444 commit 66de125

File tree

2 files changed

+78
-11
lines changed

2 files changed

+78
-11
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
"name": "RS Wiki Quest Checker",
55
"description": "This extension adds icons next to the quest requirements of a quest, to show whether or not they are completed.",
6-
"version": "0.1.0",
6+
"version": "0.2.0",
77
"author": "Midas Lambrichts",
88

99

src/main.js

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
function addCompletedChecks(userQuests){
1+
var skills = ["Attack","Defence","Strength","Constitution","Ranged","Prayer","Magic","Cooking","Woodcutting","Fletching","Fishing","Firemaking","Crafting","Smithing","Mining","Herblore","Agility","Thieving","Slayer","Farming","Runecrafting","Hunter","Construction","Summoning","Dungeoneering","Divination","Invention"
2+
];
3+
4+
function addQuestCompletedChecks(userQuests){
25
$(".questreq a").each(function(index){
36
if ($(this).html().toLowerCase() != "expand" || $(this).html().toLowerCase() != "collapse"){
47
var questTitle = $(this).html();
@@ -12,18 +15,82 @@ function addCompletedChecks(userQuests){
1215
});
1316
}
1417

15-
function loadUserData(username){
16-
$.ajax({
18+
function addLevelDetailChecks(userLevels){
19+
$(".questdetails-info > ul > li").each(function(index){
20+
var textArr = $(this).text().split(" ");
21+
var level = textArr[0];
22+
if (isNaN(level)){ //Check if it is a number.
23+
return;
24+
}
25+
var skillElement = $(this).find("a").filter(function(index){
26+
//Find the link element which has a skill name.
27+
return ($(this).text() in userLevels)
28+
});
29+
var skill = skillElement.text();
30+
31+
if (level != "" && skill != ""){
32+
if (userLevels[skill] >= level) {
33+
skillElement.append(' <img src=' + chrome.extension.getURL('assets/images/check.svg') + '>');
34+
} else {
35+
skillElement.append(' <img src=' + chrome.extension.getURL('assets/images/cross.svg') + ' style="width:15px">');
36+
}
37+
}
38+
39+
});
40+
}
41+
42+
function loadUserData(username, tries){
43+
loadUserQuests(username, tries);
44+
loadUserStats(username, tries);
45+
}
46+
47+
function loadUserQuests(username, tries){
48+
$.ajax({ // Get the quest data
1749
type:"GET",
1850
url:"https://apps.runescape.com/runemetrics/quests?user=" + username,
1951
success: function(msg){
20-
var userQuests = [];
21-
msg["quests"].forEach(function(item, index){
22-
userQuests[item["title"]] = item["status"];
23-
});
24-
addCompletedChecks(userQuests);
52+
if (msg["quests"].length == 0){
53+
if (tries <= 0){
54+
console.log("Could not fetch quest data!");
55+
} else {
56+
console.log("Trying quests again!");
57+
loadUserData(username, tries-1);
58+
}
59+
} else {
60+
var userQuests = [];
61+
msg["quests"].forEach(function(item, index){
62+
userQuests[item["title"]] = item["status"];
63+
});
64+
addQuestCompletedChecks(userQuests);
65+
}
66+
2567
}
26-
})
68+
});
69+
}
70+
71+
function loadUserStats(username, tries){
72+
$.ajax({ // Get the skill data.
73+
type:"GET",
74+
url:"https://apps.runescape.com/runemetrics/profile/profile?user=" + username + "&activities=0",
75+
success: function(msg){
76+
if ("error" in msg){
77+
if (tries <= 0){
78+
console.log("Could not fetch skills data!");
79+
} else {
80+
Console.log("Trying stats again!");
81+
loadUserStats(username, tries-1);
82+
}
83+
} else {
84+
var userLevels = [];
85+
msg["skillvalues"].forEach(function(item, index){
86+
// console.log(skills[item["id"]] + ": " +item["level"]);
87+
userLevels[skills[item["id"]]] = item["level"];
88+
89+
});
90+
addLevelDetailChecks(userLevels);
91+
}
92+
}
93+
});
2794
}
2895

2996
function getConfig(func){
@@ -34,7 +101,7 @@ function getConfig(func){
34101

35102
function init(){
36103
getConfig(function(config){
37-
loadUserData(config.username);
104+
loadUserData(config.username, 1);
38105
});
39106
}
40107

0 commit comments

Comments
 (0)