Skip to content

Instantly share code, notes, and snippets.

@yurighensev
Last active February 14, 2020 17:52
Show Gist options
  • Select an option

  • Save yurighensev/aa050af4a0526e404dfbb2171aa4c9ce to your computer and use it in GitHub Desktop.

Select an option

Save yurighensev/aa050af4a0526e404dfbb2171aa4c9ce to your computer and use it in GitHub Desktop.
BigQuery price to Reais
// ==UserScript==
// @name BigQuery price to Reais
// @namespace http://tampermonkey.net/
// @version 0.1
// @description nope
// @author You
// @match https://console.cloud.google.com/bigquery*
// @grant none
// @updateURL https://gist.github.com/yurighensev/aa050af4a0526e404dfbb2171aa4c9ce
// ==/UserScript==
(function() {
function fetchDolarPrice() {
var d = new Date()
var today = (d.getMonth() + 1) + "-" + d.getDate() + "-" + d.getFullYear()
var dolarUrl = "https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarDia(dataCotacao=@dataCotacao)?@dataCotacao='" + today + "'&$top=100&$format=json"
fetch(dolarUrl)
.then(function(r) { return r.json() } )
.then(function(data) { localStorage.setItem("dolar", data.value[0].cotacaoCompra) })
.then(observePriceChange)
}
function observePriceChange() {
setTimeout(function() {
var costField = document.querySelector('.custom-proccess-text')
if (costField) {
convertToReais()
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type == 'childList') {
convertToReais()
}
})
})
observer.observe(costField, { attributes: true, childList: true, characterData: true })
} else {
observePriceChange()
}
}, 500)
}
function convertToReais() {
var costField = document.querySelector('.custom-proccess-text')
var dolarValue = costField.innerText.match(/\$(\d\.\d+)/)[1]
var dolarToRealPrice = localStorage.getItem("dolar")
var newText = costField.innerText.replace('$' + dolarValue, ' R$ ' + (dolarToRealPrice * dolarValue).toFixed(4))
costField.innerText = newText
}
window.addEventListener('load', function() {
fetchDolarPrice()
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment