Skip to content

Instantly share code, notes, and snippets.

@neckothy
Last active September 14, 2023 15:44
Show Gist options
  • Save neckothy/708efc65d23cac0513d948533838b006 to your computer and use it in GitHub Desktop.
Save neckothy/708efc65d23cac0513d948533838b006 to your computer and use it in GitHub Desktop.
Include tax in OSRS Wiki Prices margin and profit calculations
// ==UserScript==
// @name Do Ur Taxes
// @namespace Violentmonkey Scripts
// @match https://prices.runescape.wiki/osrs*
// @grant GM_xmlhttpRequest
// @version 1.44
// @author neck
// @description Include tax in OSRS Wiki Prices margin and profit calculations
// @run-at document-start
// ==/UserScript==
// this script will only work in firefox
// https://developer.mozilla.org/en-US/docs/Web/API/Element/beforescriptexecute_event
// script interception code
// https://stackoverflow.com/a/43678483
function editScript(text) {
// https://oldschool.runescape.wiki/w/Grand_Exchange#Exempt_from_tax
// Watering can (0) has no listing on prices.runescape.wiki
// [Old school bond, Chisel, Gardening trowel, Glassblowing pipe, Hammer, Needle, Pestle and mortar, Rake, Saw, Secateurs, Seed dibber, Shears, Spade]
const taxExempt = "[13190,1755,5325,1785,2347,1733,233,5341,8794,5329,5343,1735,952]";
// regex (guh) to avoid relying on random minified function & variable names
const marginFuncRe = /(([a-zA-Z]{2})=function\(([a-zA-Z]),([a-zA-Z]))(\){return (\3&&\4))(\?\3-\4:null})/;
const roiRe = /(([a-zA-Z])=(\(([a-zA-Z])=([a-zA-Z]).high,([a-zA-Z])=\5.low,)(\(\4-\6\))(\/\6\*100\)))/;
const marginFuncName = text.match(marginFuncRe)[2];
const idToMarginRe = new RegExp("(" + marginFuncName + "\\(([a-zA-Z])\\.high,\\2\\.low)\\)", "g");
const highAlchMarginRe = new RegExp(marginFuncName + "\\((([a-zA-Z])\\.highalch),(\\2\\.high\\+[a-zA-Z].high)\\)");
text = text.replace(marginFuncRe, `$1,i$5&&!${taxExempt}.includes(i)?$3-$4-(Math.min(Math.floor($3*0.01),5000000)):$6&&${taxExempt}.includes(i)$7`);
text = text.replace(roiRe, `$2=!${taxExempt}.includes($5.id)?$3($7-(Math.min(Math.floor($4*0.01),5000000)))$8:$1`);
text = text.replace(idToMarginRe, "$1,$2.id)");
text = text.replace(highAlchMarginRe, "$1-($3)");
return text;
}
function addScript(text) {
const newScript = document.createElement("script");
const head = document.getElementsByTagName("head")[0];
newScript.type = "text/javascript";
newScript.textContent = editScript(text);
head.appendChild(newScript);
}
window.addEventListener("beforescriptexecute", function(e) {
const src = e.target.src;
if (src && src.startsWith("https://prices.runescape.wiki/osrs/static/js/main")) {
e.preventDefault();
e.stopPropagation();
GM_xmlhttpRequest({
method: "GET",
url: src,
onload: function(response) {
addScript(response.responseText);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment