Last active
August 29, 2015 13:57
-
-
Save goatandsheep/9596120 to your computer and use it in GitHub Desktop.
When finished, this Tampermonkey/Greasemonkey script should be able to convert any page from Dogecoins to USD
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 doge-usd | |
// @namespace | |
// @version 2014.03.16 | |
// @description converts pages wit dogecoin 2 USD. 4 u/thatmudkipguy. so birthday. much happy. | |
// @include * | |
// @grant GM_xmlhttpRequest | |
// @grant GM_log | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @copyright goatandsheep | |
// ==/UserScript== | |
// if u want the Ɖ | |
/* This will get the initial value of the exchange rate-it's faster if you just send one request, | |
then use the value for the rest*/ | |
function loadXMLDoc (urlAPI){ | |
var xmlhttp=new XMLHttpRequest(); | |
var txt; | |
xmlhttp.onreadystatechange=function() | |
{ | |
if (xmlhttp.readyState==4 && xmlhttp.status==200) | |
{ | |
txt = xmlhttp.responseText; | |
txt = eval ("(" + txt + ")"); | |
txt = txt.data.amount; | |
GM_log(txt); | |
GM_setValue('exchangeRate', txt); | |
console.log("bob begins"); | |
return | |
} | |
} | |
xmlhttp.open("GET", urlAPI,true); | |
xmlhttp.send(); | |
} | |
function woofRequest(shibe) { | |
for(i in shibe){ | |
shibe[i] = shibe[i]; | |
console.log(shibe[i]); | |
shibe[i] = shibe[i].replace(/\Ɖ|ᴆ| |,/g, ''); // remove commas and Ɖoge lettering | |
// so metric | |
// much extra inputs: | |
// ###k, ###K, ###M, ###T, ###G | |
var multiplier = 1.0; // for the value of kilo, mega, and other metric prefixes | |
var coinz = parseFloat(shibe[i]); | |
var letter = shibe[i].substring((coinz.toString()).length, (shibe[i].length));// take the letter at the end | |
if ((letter === 'k') || (letter === 'K')) {multiplier = 1000.0; } // kilo | |
else if (letter === 'M') {multiplier = 1000000.0; } // mega | |
else if (letter === 'G') {multiplier = 1000000000.0; } // giga | |
else if (letter === 'T') {multiplier = 1000000000000.0; } // tera | |
coinz = coinz * multiplier; | |
console.log("coinz: " + coinz); | |
shibe[i] = coinz * GM_getValue('exchangeRate'); | |
shibe[i] = (Math.round(shibe[i] * 100)) / 100; | |
shibe[i] = '$' + shibe[i].toString(); | |
console.log("This is " + i.toString()); | |
console.log(shibe[i]); | |
} | |
return shibe; | |
} | |
/** if u got sumthn 2 convert, m8, il teece u 2 use diffrent currencies, so u better reed how 2 change 2 a diffrent API | |
* if ur changin the url, check the parsin of the api and change the onload section rite | |
* info 4 dogeapi: https://www.dogeapi.com/api_documentation | |
* sample 4 u blokes: | |
* url: https://www.dogeapi.com/wow/v2/?a=get_current_price&convert_to=USD&amount_doge=1000 | |
* response: {"data":{"amount":0.00133}} | |
* | |
* P.S. dogeapi cud convert 2: | |
* * "USD" | |
* * "BTC" | |
**/ | |
loadXMLDoc('https://www.dogeapi.com/wow/v2/?a=get_current_price&convert_to=USD&amount_doge=1'); | |
console.log(GM_getValue('exchangeRate')); | |
// so modified | |
// OP very noob | |
// sauce: http://www.techradar.com/news/internet/the-beginner-s-guide-to-greasemonkey-scripting-598247/4#articleContent | |
var textNodes = document.evaluate("//text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); | |
var searchRE; | |
// add more urls here, sorry for the difficulty due to need to use regular expressions | |
// check your regex here: https://www.debuggex.com/#cheatsheet | |
/** if that is too difficult, put this: | |
* if ((document.location.href).substring(0,'%url%'.length)==='%url%') | |
**/ | |
// sample: 'http://www.reddit.com/r/dogemarket/comments/20k26i/sg_tf2_key_for_2400_doge/'.match(/((http|https):\/\/+(www\.reddit.com\/r\/dogemarket))?/) | |
if ((document.location.href).match(/((http|https):\/\/(www\.reddit\.com\/r\/dogemarket))/)) { | |
// same as below, but added capability for metric inputs: #k/K/M/T/G | |
searchRE = /(Ɖ([1-9]{1}\d{0,2})(,? ?\d{3})*(\.\d*)?)|(([1-9]{1}\d{0,2})(,? ?\d{3})*(\.\d*)?( doge| DOGE))|(([1-9]{1}\d{0,2})(,? ?\d{3})*(\.\d*)?ᴆ)|(([1-9]{1}\d{0,2})(,? ?\d{3})*(\.\d*)?([KMGT]|[k]))/gi; | |
} | |
else { | |
/** much inputs: | |
* #,###,###.# doge | |
* # DOGE | |
* Ɖ# | |
* #ᴆ | |
**/ | |
searchRE = /(Ɖ([1-9]{1}\d{0,2})(,? ?\d{3})*(\.\d*)?)|(([1-9]{1}\d{0,2})(,? ?\d{3})*(\.\d*)?( doge| DOGE))|(([1-9]{1}\d{0,2})(,? ?\d{3})*(\.\d*)?ᴆ)/gi; | |
} | |
var i; | |
// find and replace | |
for (i = 0; i < textNodes.snapshotLength; i++) { | |
var node = textNodes.snapshotItem(i); | |
if (node.data.match(searchRE)){ | |
console.log(node.data.match(searchRE)); | |
node.data = node.data.replace(searchRE, woofRequest(node.data.match(searchRE))); | |
} | |
} |
allows for spaces every 3 digits
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One of the problems is that the regex for Reddit is catching lowercase letters, resulting in many extra inputs even though I checked using the regex checker. Other than that, I don't know what's wrong.