Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brubsby/3b752f8281084d192f7c26e941e58cff to your computer and use it in GitHub Desktop.
Save brubsby/3b752f8281084d192f7c26e941e58cff to your computer and use it in GitHub Desktop.
rune_xp_diff.js
// ==UserScript==
// @name xp diff
// @namespace http://tampermonkey.net/
// @version 0.1
// @description paste the exact diff of your xp when looking at your runemetrics profile json
// @author brubsby
// @match https://apps.runescape.com/runemetrics/profile/profile*
// @icon https://www.google.com/s2/favicons?sz=64&domain=runescape.com
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @grant GM_log
// ==/UserScript==
//ctrl+shift+f to diff, ctrl+alt+f to refresh page with incremented activities
let performdiff = function() {
'use strict';
let skill_lookup = [
"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",
"Archaeology",
]
let int_to_fixed = to_fix => parseFloat(to_fix.toString().replace(/(.*)(.)$/,"$1\.$2"))
let json_to_xp_dict = json => Object.fromEntries(json.skillvalues ? json.skillvalues.map(dict => [skill_lookup[dict.id], dict.xp]) : [])
let diff_xp_dict = (dict1, dict2) => Object.fromEntries(skill_lookup.map(skill => [skill, Math.abs((dict1[skill]||0) - (dict2[skill]||0))]).filter(([skill, value]) => value != 0))
let lastjson = GM_getValue("rs3json",{})
let time = Date.now() / 1000
let json = JSON.parse(document.querySelector("body > pre").textContent)
let xp_dict = json_to_xp_dict(json)
let last_xp_dict = Object.entries(json).length ? json_to_xp_dict(lastjson) : {}
let xp_diff = diff_xp_dict(xp_dict, last_xp_dict)
if (Object.entries(xp_diff).length) {
GM_log(xp_diff)
GM_setValue("rs3json", json)
GM_setValue("rs3jsontime", time)
GM_setValue("rs3xpdiffhistory", {...GM_getValue("rs3xpdiffhistory", {}), ...{[time]: xp_diff}}, )
} else {
GM_log("no xp changes found this time, here's last three diffs:")
Object.entries(GM_getValue("rs3xpdiffhistory", {})).sort((a,b)=>b[0]-a[0]).slice(0,3).map(entry => GM_log(entry[1]))
}
};
GM_registerMenuCommand("xp diff", performdiff, "f");
performdiff()
document.addEventListener("keydown", (e) => {
let activity_regex = /(?<==)[0-9]+$/
if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.keyCode == 70) {
let activity = (parseInt(document.URL.match(activity_regex)) + 1) % 64
window.open(document.URL.replace(activity_regex, activity),"_self")
}
//clear history
if ((e.ctrlKey || e.metaKey) && e.altKey && e.keyCode == 88) {
GM_setValue("rs3xpdiffhistory", {})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment