Skip to content

Instantly share code, notes, and snippets.

@iboss-ptk
Created October 7, 2018 06:04
Show Gist options
  • Select an option

  • Save iboss-ptk/ec9a007d9148c66714e88efefd7be30f to your computer and use it in GitHub Desktop.

Select an option

Save iboss-ptk/ec9a007d9148c66714e88efefd7be30f to your computer and use it in GitHub Desktop.
observer || observer.cleanUp()
observer = (() => {
// start config
const THRESHOLD = 3000000
// end config
const getBigTransaction = (threshold) =>
Array.from(document.querySelectorAll('market-ticker-page-row'))
.map(mt => {
const lis = mt.querySelectorAll('li')
return [
lis[0].innerText,
parseFloat(lis[2].innerText.replace(',', '')) * parseFloat(lis[3].innerText),
new Date().toLocaleDateString(),
new Date().toLocaleTimeString(),
]
})
.filter(([name, transactionValue]) => name != '' && transactionValue != NaN)
.filter(([_, transactionValue]) => transactionValue >= threshold)
const getStore = () => {
let store
try {
store = JSON.parse(localStorage.getItem('bigTransactions')) || []
} catch {
console.error('Stored transactions are malformed')
store = []
}
return store
}
const updateStore = (bigTransactions) => {
const store = getStore()
const updatedStore = store
.concat(bigTransactions)
.filter((value, index, self) => {
const storeIndexedByDateAndValue = self.map(([n, txn, date]) => [n, txn, date].toString())
const itemWithOnlyDateAndValue = [value[0], value[1], value[2]].toString()
return storeIndexedByDateAndValue.indexOf(itemWithOnlyDateAndValue) === index
})
localStorage.setItem('bigTransactions', JSON.stringify(updatedStore))
localStorage.setItem('bigTransactionUpdatedAt', new Date().toLocaleString())
}
const interval = setInterval(() => {
const bigTransactions = getBigTransaction(THRESHOLD)
updateStore(bigTransactions)
}, 1000 * 10)
const cleanUp = () => clearInterval(interval)
const display = () => {
const store = getStore()
console.table(
store
.map(([name, transactionValue, date, time]) => ({ name, transactionValue, date, time })))
}
const download = () => {
const csvContent = 'data:text/csv;charset=utf-8,' + getStore().map((row) => row.join(',')).join('\r\n')
const encodedUri = encodeURI(csvContent)
const link = document.createElement('a')
link.setAttribute('href', encodedUri)
link.setAttribute('download', `big_transactions ${new Date().toLocaleString()}.csv`)
link.click()
link.remove()
}
return { display, download, cleanUp }
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment