Last active
October 26, 2020 08:21
-
-
Save boomboompower/de91285a0d758eb0189c77d5dddd60a7 to your computer and use it in GitHub Desktop.
A Tampermonkey script to convert prices from USD to AUD on the Unity Asset Store.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Unity Asset Store USD -> AUD | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Converts prices on the unity asset store to AUD | |
// @author boomboompower | |
// @license CC BY | |
// @match https://assetstore.unity.com/ | |
// @match https://assetstore.unity.com/* | |
// @grant GM_getValue | |
// @grant GM_setValue | |
// @grant GM.xmlHttpRequest | |
// @run-at document-end | |
// ==/UserScript== | |
(async function() { | |
'use strict'; | |
// The interval between checking if prices on the page has changed | |
// this only matters if you are doing everything on the same tab. | |
// Obviously a lower value means a more frequent price update, but | |
// may cause some lower-end PCs to lag. | |
const priceChangeCheckInterval = 10000; | |
// 1 USD = this much AUD | |
// | |
// By default it queries a currency conversion API and | |
// caches the value for 5 minutes. | |
const USDtoAUDExchange = await getExchangeRate(); | |
async function getExchangeRate() { | |
let oops = GM_getValue('dollar_exchange_rate', null); | |
if (oops != null) { | |
if (oops.includes(':')) { | |
let splet = oops.split('\:'); | |
if (new Date().getTime() - parseFloat(splet[1]) < 1000 * 60 * 5) { | |
console.log('Using cached exchange value. '); | |
return parseFloat(splet[0]); | |
} | |
} | |
} | |
let incData = await GM.xmlHttpRequest({ | |
method: 'GET', | |
url: 'https://api.exchangeratesapi.io/latest?base=USD' | |
}); | |
if (!incData || !incData.responseText) { | |
return 1.3982793522; | |
} | |
let value = JSON.parse(incData.responseText); | |
if (!value || !value.rates || !value.rates.AUD) { | |
return 1.3982793522; | |
} | |
let audRate = value.rates.AUD; | |
GM_setValue('dollar_exchange_rate', audRate + ":" + new Date().getTime()); | |
console.log('Retrieved a new exchange rate.'); | |
return audRate; | |
} | |
function convertAll() { | |
// On the main page | |
convert(document.getElementsByClassName('mErEH')); | |
// On an item page | |
if (document.URL.includes('packages')) { | |
convert(document.querySelectorAll('._3Yjml > span')); | |
} | |
// On the cart page | |
if (document.URL.includes('cart')) { | |
convert(document.querySelectorAll('._3SApE > span')); | |
convert(document.querySelectorAll('.tVMIV > span')); | |
convert(document.getElementsByClassName('price_label')); | |
} | |
function convert(arrayIn) { | |
for (let i = 0; i < arrayIn.length; i++) { | |
let e = arrayIn[i]; | |
if (e.innerText === e.prevInnerText) continue; | |
if (e.innerText.toLowerCase().includes("free")) continue; | |
let price = parseFloat(e.innerText.substring(1)); | |
let converted = (USDtoAUDExchange * price).toFixed(2); | |
// I did something wrong :( | |
if (price == NaN || converted == NaN) continue; | |
e.innerText += " USD\n$" + converted + " AUD"; | |
e.style.fontSize = "0.8em"; | |
e.prevInnerText = e.innerText; | |
} | |
} | |
} | |
window.addEventListener('load', function() { | |
convertAll(); | |
setInterval(function() { convertAll(); }, priceChangeCheckInterval); | |
}, false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment