Skip to content

Instantly share code, notes, and snippets.

@tarekis
Last active February 2, 2024 21:48
Show Gist options
  • Save tarekis/3f7820b6fcf3812b57de6831011d9858 to your computer and use it in GitHub Desktop.
Save tarekis/3f7820b6fcf3812b57de6831011d9858 to your computer and use it in GitHub Desktop.
TemperMonkey: Path of Exile Trade Site | Chaos to Compound Calculator / Message Generator
// ==UserScript==
// @name Chaos to Compound Calculator / Message Generator
// @namespace https://www.pathofexile.com/trade/exchange
// @version 1.2
// @description Lets you calculate pure chaos to compound value, displays what pure chaos amounts to and copies an info text to the seller.
// @author Clemens Himmer
// @homepage https://himmer.software/
// @match *://www.pathofexile.com/trade/exchange*
// @grant GM.xmlHttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @require https://cdn.jsdelivr.net/npm/@trim21/gm-fetch@0.1.15/dist/gm_fetch.min.js
// @require https://code.jquery.com/jquery-3.7.1.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @run-at document-end
// @connect poe.ninja
// ==/UserScript==
(async function() {
'use strict';
// GGG please don't break this thanks
const mode = 'div'; // ex for exalted
// Get major currency value
const currencyDictionary = {
div: [
'divines',
'divine-orb',
'd'
],
ex: [
'exalts',
'exalted-orb',
'ex'
]
}
const majorCurrencyName = currencyDictionary[mode][0];
const majorCurrencyId = currencyDictionary[mode][1];
const majorCurrencyShort = currencyDictionary[mode][2];
const majorCurrencyValueCacheKey = 'CHIMMER_CHAOSTOCOMPOUND_MAJOR_CURRENCY_VALUE';
const lastCachedTimeCacheKey = 'CHIMMER_CHAOSTOCOMPOUND_MAJOR_CACHE_TIME';
const now = new Date();
const lastCachedTime = GM_getValue(lastCachedTimeCacheKey);
let majorCurrencyValue;
if (now.setHours(now.getHours() - 3) > lastCachedTime) {
console.log(`Geting new value, last fetch from ${new Date(lastCachedTime).toLocaleString()}, considering this stale.`);
const league = window.location.pathname.split('/')[3];
const url = `https://poe.ninja/api/data/currencyoverview?league=${league}&type=Currency`;
const res = await GM_fetch(url);
const prices = await res.json();
majorCurrencyValue = prices.lines.find(currency => currency.detailsId === majorCurrencyId).receive.value;
GM_setValue(majorCurrencyValueCacheKey, majorCurrencyValue);
GM_setValue(lastCachedTimeCacheKey, new Date().getTime());
console.log(`Saving ${majorCurrencyName} value at ${majorCurrencyValue} to cache.`);
} else {
majorCurrencyValue = GM_getValue(majorCurrencyValueCacheKey);
console.log(`Last value ${majorCurrencyValue} is fresh, from ${new Date(lastCachedTime).toLocaleString()}, re-using.`);
}
// Display status message
function displayMessage(button, messageText, color, duration) {
const top = button.offset().top - 40;
const left = button.offset().left;
const popUp = $(`<div>${messageText}</div>`);
popUp.css({
backgroundColor: color || 'white',
padding: '10px',
position: 'absolute',
top,
left
});
$('html').append(popUp);
setTimeout(() => {
popUp.fadeOut(300, function() { $(this).remove(); })
}, duration || 2000)
}
// Handle button clicks
function getCompound(self) {
const textArea = self.parentsUntil('.contact').children('.slider-middle').children();
const whisperText = textArea.val();
const words = whisperText.split(' ');
const numerics = words.filter(v => !isNaN(+v));
const chaos = +numerics[numerics.length - 1];
if (chaos) {
if (chaos > majorCurrencyValue) {
const overshoot = Math.ceil(chaos%majorCurrencyValue);
const majorCurrencyAmount = Math.ceil((chaos - overshoot) / majorCurrencyValue);
const compoundText = `${majorCurrencyAmount}${majorCurrencyShort}${overshoot}c`;
return [ chaos, compoundText, words[0] ];
} else {
return chaos;
}
} else {
return null;
}
};
function handleDemoClick() {
const self = $(this);
const compound = getCompound(self);
if (compound) {
if (Array.isArray(compound)) {
const [ chaos, text ] = compound;
displayMessage(self, `Success! ${chaos}c -> ${text}.`, '#66bb6a');
} else {
displayMessage(self, `Chaos too low for ${majorCurrencyName} conversion`, '#fff176');
}
} else {
displayMessage(self, 'No chaos value found.', '#e57373');
}
};
function handleWhisperClick() {
const self = $(this);
const compound = getCompound(self);
if (compound) {
if (Array.isArray(compound)) {
const [ chaos, text, recipient ] = compound;
const compountTextWhisper = `${recipient} Will be paying in ${majorCurrencyName}, ${chaos}c at current poe.ninja rate ${majorCurrencyValue}c/${majorCurrencyShort} is ${text}.`;
navigator.clipboard.writeText(compountTextWhisper);
displayMessage(self, `Success! Copied text! ${text}`, '#66bb6a');
} else {
displayMessage(self, `Chaos too low for ${majorCurrencyName} conversion, no action.`, '#fff176');
}
} else {
displayMessage(self, 'No chaos value found, no action.', '#e57373');
}
};
// Add buttons to created buttons
function addButtons(whisperButton) {
console.log('Found multi-item bulk trade.', whisperButton.parents('.exchange')[0]);
const isPureChaosTrade = whisperButton.parents('.slider').siblings().children('.slider-right').children('img').toArray().every(img => img.alt === 'chaos');
if (isPureChaosTrade) {
console.log('Found button, adding functionality.');
const parent = whisperButton.parent();
const whisperDemoButton = $('<button class="btn btn-default btn-xs" style="margin-right: 5px;">?</button>');
const whisperCompoundButton = $('<button class="btn btn-default btn-xs" style="color: #e9cf9f;">Copy Whisper</button>');
parent.css({
flexDirection : 'row',
alignItems: 'center'
});
parent.append(whisperDemoButton);
parent.append(whisperCompoundButton);
whisperButton.css({
marginRight: '20px'
});
whisperDemoButton.on("click", handleDemoClick);
whisperCompoundButton.on("click", handleWhisperClick);
} else {
console.log('Trade is not for pure chaos, functionality not added.');
}
};
waitForKeyElements("#trade .slider .direct-btn ", addButtons);
console.info(`Compound Price Calculator running with ${majorCurrencyValue}c / ${majorCurrencyName} rate`);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment