MediaWiki:Gadget-wikisync-dev-core.js: Difference between revisions

no edit summary
 
No edit summary
 
Line 1:
"use strict";
 
/**
* WikiSync:
Line 17 ⟶ 19:
QC_ACTIVE: "qc-active",
QC_INPUT: "qc-input",
QC_ICON: "rs-qc-icon",
};
 
var ENDPOINTS = {
osrs: "https://sync.runescape.wiki/runelite/player/username/STANDARD",
shatteredrelics: "https://sync.runescape.wiki/runelite/player/username/SHATTERED_RELICS_LEAGUE" //use actual url
shatteredrelics:
"https://sync.runescape.wiki/runelite/player/username/SHATTERED_RELICS_LEAGUE", //use actual url
};
 
 
var questCorrections = {
// Add corrections for API quest names -> wiki names here.
// API quest name is the key and the wiki page name is the value.
"Desert Treasure II - The Fallen Empire": "Desert Treasure II",
"Recipe for Disaster - Another Cook's Quest" : "Recipe for Disaster/Another Cook's Quest",
"Recipe for Disaster - Culinaromancer" : "Recipe for Disaster/Defeating the Culinaromancer",
"Recipe for Disaster - Mountain Dwarf" : "Recipe for Disaster/Freeing the Mountain Dwarf",
"Recipe for Disaster - Wartface & Bentnoze" : "Recipe for Disaster/Freeing the Goblin generals",
"Recipe for Disaster - Pirate Pete" : "Recipe for Disaster/Freeing Pirate Pete",
"Recipe for Disaster - Lumbridge Guide" : "Recipe for Disaster/Freeing the Lumbridge Guide",
"Recipe for Disaster - Evil Dave" : "Recipe for Disaster/Freeing Evil Dave",
"Recipe for Disaster - King Awowogei" : "Recipe for Disaster/Freeing King Awowogei",
"Recipe for Disaster - Sir Amik Varze" : "Recipe for Disaster/Freeing Sir Amik Varze",
"Recipe for Disaster - Skrach Uglogwee" : "Recipe for Disaster/Freeing Skrach Uglogwee"
},
icons = {
yes: ' <span class="' + CLASSES.QC_ICON + '"><img class="qc-complete" src="//oldschool.runescape.wiki/images/Yes_check.svg?00000" width="15px" ></span>',
no: ' <span class="' + CLASSES.QC_ICON + '"><img class="qc-not-started" src="//oldschool.runescape.wiki/images/X_mark.svg?00000" width="13px" ></span>',
};
 
var wikisync = {
/**
* Startup method
*/
init: function init() {
wikisync.createFields();
},
setCheckboxText: function setCheckboxText(text) {
 
$(".rs-wikisync-hide-completed .oo-ui-labelElement-label").each(function () {
setCheckboxText: function (text) {
$(this).text(text);
$(".rs-wikisync-hide-completed .oo-ui-labelElement-label").each(
function (}) {;
$(this).text(text);
}
);
},
hideCompletedEntries: function hideCompletedEntries() {
 
hideCompletedEntries: function () {
var selected = wikisync.hideCompletedCheckbox.isSelected();
if (selected) {
Line 69 ⟶ 62:
}
},
 
/**
* Creates the input fields for the user's RSN
*/
createFields: function createFields() {
var name;
if (rs.hasLocalStorage() === true) {
$.removeCookie("RSN", { path: "/" }); // remove any existing cookies using jQuery, will return false if it doesn't exist so it's fine
path: "/"
}); // remove any existing cookies using jQuery, will return false if it doesn't exist so it's fine
name = localStorage.getItem("rsn");
} else {
name = wikisync.getCookie("RSN");
}
 
var gamemode = "osrs";
 
$("." + CLASSES.QC_INPUT).each(function () {
var input1 = new OO.ui.TextInputWidget({
placeholder: "Display name",
id: "rs-qc-rsn",
});
 
if (name) {
// Set input to cookie/localStorage value.
input1.setValue(name);
}
 
var button1 = new OO.ui.ButtonInputWidget({
label: new OO.ui.HtmlSnippet('<img src="' + rs.getFileURLCached("RuneLite icon.png") + '" width=' + '"20px" />' + " Look up"),
'<img src="' +
rs.getFileURLCached("RuneLite icon.png") +
'" width=' +
'"20px" />' +
" Look up"
),
title: "Look up player data using RuneLite",
flags: ["primary", "progressive"],
classes: ["wikisync-lookup-button"],
});
 
var leagueOnly = $(this).hasClass("league-only");
if (leagueOnly) {
gamemode = "shatteredrelics";
}
var button1action = function button1action() {
 
var button1action = function () {
if (rs.hasLocalStorage() === true) {
localStorage.setItem("rsn", input1.value); // save in localStorage
Line 123 ⟶ 105:
button1.on("click", button1action);
input1.on("enter", button1action);
 
var helpModalBtn = new OO.ui.ButtonWidget({
label: "Learn how to enable WikiSync",
href: "/w/RuneScape:WikiSync",
});
 
var hideCompleted = false;
if (rs.hasLocalStorage()) {
Line 136 ⟶ 116:
}
wikisync.hideCompletedCheckbox = new OO.ui.CheckboxInputWidget({
selected: hideCompleted,
});
wikisync.hideCompletedCheckbox.on("change", function () {
Line 145 ⟶ 125:
$(".wikisync-completed").toggle(!selected);
});
 
var fieldset = new OO.ui.FieldsetLayout({
id: "rs-qc-form",
});
var fieldSetItems = [new OO.ui.FieldLayout(input1, {
 
var fieldSetItems =label: ["Username:",
new OO.ui.FieldLayout(input1, { label: "Username:", align: "inline" }),
})];
 
fieldSetItems.push(button1);
fieldset.addItems([new OO.ui.HorizontalLayout({
newitems: OO.ui.HorizontalLayout({fieldSetItems
}), new OO.ui.FieldLayout(helpModalBtn, {
items: fieldSetItems,
label: "No data found. To use this, enable the WikiSync plugin in RuneLite.",
}),
newalign: OO.ui.FieldLayout(helpModalBtn"inline", {
classes: label:["rs-wikisync-help"]
}), new OO.ui.LabelWidget({
"No data found. To use this, enable the WikiSync plugin in RuneLite.",
label: "Missing some data from RuneLite for this page. Please log in to the game to re-sync.",
align: "inline",
classes: ["rs-wikisync-helpmissingdata"],
}), new OO.ui.FieldLayout(wikisync.hideCompletedCheckbox, {
}),
newlabel: OO.ui.LabelWidget({"Hide completed",
align: label:"inline",
classes: ["rs-wikisync-hide-completed"]
"Missing some data from RuneLite for this page. Please log in to the game to re-sync.",
})]);
classes: ["rs-wikisync-missingdata"],
}),
new OO.ui.FieldLayout(wikisync.hideCompletedCheckbox, {
label: "Hide completed",
align: "inline",
classes: ["rs-wikisync-hide-completed"],
}),
]);
 
if ($(this).hasClass("lighttable")) {
// If it's a lighttable, insert the fieldset before the table
Line 192 ⟶ 162:
}
});
 
if (name) {
// If there is a saved name, load the data for it.
Line 198 ⟶ 167:
}
},
 
/**
* Updates the status text
*/
updateStatus: function updateStatus(text) {
mw.notify(text, { tag: "wikisync" });
tag: "wikisync"
});
},
 
/**
* Sets a cookie
*/
setCookie: function setCookie(name, value, days) {
var expires = "";
if (days) {
Line 218 ⟶ 187:
document.cookie = name + "=" + (value || "") + expires + "; path=/";
},
 
/**
* Returns the value of a cookie, or null if it doesn't exist
*/
getCookie: function getCookie(name) {
var cookie = new RegExp("^(?:.*;)?\\s*" + name + "\\s*=\\s*([^;]+)(?:.*)?$"),
"^(?:.*;)?\\s*" + name + "\\s*=\\s*([^;]+)(?:.*)?$"
),
match = document.cookie.match(cookie);
 
if (match !== null) {
return match[1];
Line 234 ⟶ 199:
}
},
 
/**
* Load data
*/
loadData: function loadData(rsn, gamemode) {
if (!rsn) {
wikisync.updateStatus("Invalid RSN");
return;
}
 
var endpoint = ENDPOINTS[gamemode || "osrs"] || ENDPOINTS.osrs;
// Hide help text if it is showing.
Line 249 ⟶ 212:
$(".rs-wikisync-missingdata").hide();
$(".wikisync-success").remove();
 
$.ajax({
// Get the quest data
Line 255 ⟶ 217:
url: endpoint.replace("username", rsn),
dataType: "json",
success: function success(msg) {
var userQuests = {};
Object.entries(msg.quests).forEach(function (q) {
Line 277 ⟶ 239:
$(".wikisync-completed").show();
$(".wikisync-completed").removeClass("wikisync-completed");
$("<img>").attr("src", "//oldschool.runescape.wiki/images/f/fb/Yes_check.svg?00000").addClass("wikisync-success").css("width", "20px").css("height", "20px").css("position", "relative").insertAfter(".wikisync-lookup-button");
$("<img>")
var hasAllData = [wikisync.addQuestIcons(userQuests), wikisync.addQuestTable(userQuests, userSkills, msg.achievement_diaries), wikisync.addDiaryTable(msg.achievement_diaries), wikisync.addSkillTable(userSkills), wikisync.addSkillIcons(userSkills), wikisync.addMusicTracks(msg.music_tracks), wikisync.addCombatAchievementTasks(msg.combat_achievements)].every(function (result) {
.attr(
"src",
"//oldschool.runescape.wiki/images/f/fb/Yes_check.svg?00000"
)
.addClass("wikisync-success")
.css("width", "20px")
.css("height", "20px")
.css("position", "relative")
.insertAfter(".wikisync-lookup-button");
 
var hasAllData = [
wikisync.addQuestIcons(userQuests),
wikisync.addQuestTable(
userQuests,
userSkills,
msg.achievement_diaries
),
wikisync.addDiaryTable(msg.achievement_diaries),
wikisync.addSkillTable(userSkills),
wikisync.addSkillIcons(userSkills),
wikisync.addMusicTracks(msg.music_tracks),
wikisync.addCombatAchievementTasks(msg.combat_achievements),
].every(function (result) {
return result;
});
 
if (!hasAllData) {
$(".rs-wikisync-missingdata").show();
}
},
error: function error(req) {
$("." + CLASSES.QC_ICON).remove();
wikisync.setCheckboxText("Hide completed");
if (req.responseJSON && req.responseJSON.code && req.responseJSON.code === "NO_USER_DATA") {
if (
req.responseJSON &&
req.responseJSON.code &&
req.responseJSON.code === "NO_USER_DATA"
) {
$(".rs-wikisync-help").show();
} else {
wikisync.updateStatus("There was a problem loading data for " + rsn);
}
},
});
},
 
/**
* Clicks the Combat Achievement rows
*/
addCombatAchievementTasks: function addCombatAchievementTasks(combatAchievements) {
var combatAchievementTable = $('table.' + CLASSES.QC_ACTIVE + '.ca-tasks');
if (combatAchievementTable.length === 0) {
Line 333 ⟶ 267:
return true;
}
 
if (combatAchievementTable === null) {
return false;
}
 
var seen = {};
combatAchievements.forEach(function (taskId) {
Line 343 ⟶ 275:
});
combatAchievementTable.each(function () {
$(this).find("tr[data-ca-task-id]").each(function () {
var task_id = $(this).finddata("tr[data-ca-task-id]");
.eachif (function!!seen[task_id] !== $(this).hasClass("highlight-on")) {
var task_id = $(this).dataclick("ca-task-id");
}
if (!!seen[task_id] !== $(this).hasClass("highlight-on")) {
if $(thisseen[task_id]).click(); {
}$(this).addClass("wikisync-completed");
if (seen[task_id]) {}
});
$(this).addClass("wikisync-completed");
}
});
});
 
return true;
},
 
/**
* Clicks the music track rows
*/
addMusicTracks: function addMusicTracks(musicTracks) {
var musicTable = $("table." + CLASSES.QC_ACTIVE + ".music-tracks");
if (musicTable.length === 0) {
Line 368 ⟶ 296:
return true;
}
 
if (musicTracks === null) {
// Missing data
return false;
}
 
var total = 0;
var completed = 0;
musicTable.each(function () {
$(this).find("tr[data-music-track-name]").each(function () {
$(this)
var music_track_name = $(this).finddata("tr[data-music-track-name]");
if (!!musicTracks[music_track_name] !== $(this).hasClass("highlight-on")) {
.each(function () {
var music_track_name = $(this).dataclick("music-track-name");
}
if (!!musicTracks[music_track_name] !== $(this).hasClass("highlight-on")) {
if (musicTracks[music_track_name]) {
$(this).click();
}$(this).addClass("wikisync-completed");
if (musicTracks[music_track_name]) {completed++;
}
$(this).addClass("wikisync-completed");
completedtotal++;
});
total++;
});
});
wikisync.setCheckboxText("Hide unlocked tracks (" + completed + "/" + total + " unlocked)");
"Hide unlocked tracks (" + completed + "/" + total + " unlocked)"
);
wikisync.hideCompletedEntries();
return true;
},
 
/**
* Append a checkmark/X icon to `element`.
*/
append_icon: function append_icon(element, completed) {
if (completed) {
$(element).append(icons.yes);
Line 408 ⟶ 329:
}
},
 
/**
* Clicks the rows in a table of question and diary tiers. Also appends icons to rows dedicated to skill training
*/
addQuestTable: function addQuestTable(quests, skills, achievementDiaries) {
function splitOnLastOccurence(str, splitOn) {
var index = str.lastIndexOf(splitOn);
return {
return { before: str.slice(0, index), after: str.slice(index + 1) };
before: str.slice(0, index),
after: str.slice(index + 1)
};
}
 
Line 422 ⟶ 345:
var rowID = $(this).data("rowid");
var isAchievementDiary = rowID.includes("Diary#");
 
if (isAchievementDiary) {
// Achievement diary rowIDs are formatted as "$NAME Diary#$TIER", where "$NAME" may contain spaces.
Line 428 ⟶ 350:
var diaryName = splitOnLastOccurence(rowID, " ").before;
var diaryTier = splitOnLastOccurence(rowID, "#").after;
if (diaryName in achievementDiaries && diaryTier in achievementDiaries[diaryName] && "complete" in achievementDiaries[diaryName][diaryTier]) {
if (
var diaryCompleted = achievementDiaries[diaryName][diaryTier].complete;
diaryName in achievementDiaries &&
diaryTier in achievementDiaries[diaryName] &&
"complete" in achievementDiaries[diaryName][diaryTier]
) {
var diaryCompleted =
achievementDiaries[diaryName][diaryTier].complete;
if (diaryCompleted !== null) {
if (diaryCompleted !== $(this).hasClass("highlight-on")) {
Line 453 ⟶ 370:
 
// Skill training complete
$("table." + CLASSES.QC_ACTIVE + ".oqg-table tr[data-skill][data-skill-level]").each(function () {
$(
"table." + CLASSES.QC_ACTIVE + ".oqg-table tr[data-skill][data-skill-level]"
).each(function () {
var skillName = $(this).data("skill");
skillName = skillName.charAt(0).toUpperCase() + skillName.slice(1);
Line 464 ⟶ 379:
return true;
},
 
// Clicks cells/rows in a table based on skill levels.
addSkillTable: function addSkillTable(skills) {
$("table." + CLASSES.QC_ACTIVE + ".skill-table [data-skill][data-skill-level]").each(function () {
$(
"table." + CLASSES.QC_ACTIVE + ".skill-table [data-skill][data-skill-level]"
).each(function () {
var skillName = $(this).data("skill");
var skillLevel = $(this).data("skill-level");
Line 479 ⟶ 391:
return true;
},
 
// Clicks rows in a table based on achievement diary task completion
addDiaryTable: function addDiaryTable(achievementDiaries) {
var hasAllData = true;
$("table." + CLASSES.QC_ACTIVE + ".diary-table[data-diary-name][data-diary-tier]").each(function () {
$(
"table." + CLASSES.QC_ACTIVE + ".diary-table[data-diary-name][data-diary-tier]"
).each(function () {
var task_index = -1;
var diaryName = $(this).data("diary-name");
var diaryTier = $(this).data("diary-tier");
$(this).find("tr").each(function () {
.findif ("tr"task_index < 0) {
.each(function () {task_index += 1;
if (task_index < 0) {return;
task_index += 1;}
if (diaryName in achievementDiaries && diaryTier in achievementDiaries[diaryName] && achievementDiaries[diaryName][diaryTier].tasks.length > task_index) {
return;
var task_completed = achievementDiaries[diaryName][diaryTier].tasks[task_index];
}
if (task_completed !== null) {
if (task_completed !== $(this).hasClass("highlight-on")) {
diaryName in achievementDiaries &&
diaryTier in achievementDiaries[diaryName] &&$(this).click();
achievementDiaries[diaryName][diaryTier].tasks.length > task_index
) {
var task_completed =
achievementDiaries[diaryName][diaryTier].tasks[task_index];
 
if (task_completed !== null) {
if (task_completed !== $(this).hasClass("highlight-on")) {
$(this).click();
}
} else {
hasAllData = false;
}
} else {
hasAllData = false;
}
task_index += 1;}
})task_index += 1;
});
});
return hasAllData;
},
 
/**
* Adds the icons next to respective quests
*/
addQuestIcons: function addQuestIcons(quests) {
$("." + CLASSES.QC_ACTIVE + " a").each(function () {
if ($(this).html().toLowerCase() != "expand" || $(this).html().toLowerCase() != "collapse") {
if (
$(this).html().toLowerCase() != "expand" ||
$(this).html().toLowerCase() != "collapse"
) {
var questTitle = $(this).text().trim();
if (questTitle in quests) {
Line 535 ⟶ 432:
return true;
},
 
/**
* Adds the icons next to respective skills
*/
addSkillIcons: function addSkillIcons(userLevels) {
$("." + CLASSES.QC_ACTIVE + " .scp").each(function () {
var level = $(this).data("level");
Line 549 ⟶ 445:
});
return true;
},
};
 
$(wikisync.init);