Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

first commit

  • Loading branch information...
commit 723b907daf33be0576eab298981faeac61fdd695 0 parents
freeborn freeborn authored
23 README
... ... @@ -0,0 +1,23 @@
  1 +yoinked from here
  2 +
  3 +http://sanescreen.org/converter/
  4 +
  5 +modified bitcoin_rate.js to use CAD instead of USD.
  6 +
  7 +have not chenaged bitcoin_converter.js to represent CAD instead of USD
  8 +
  9 +
  10 +
  11 +USAGE:
  12 +edit cadjsonparse.py to create bitcoin_rate.js under accessable web dir
  13 +
  14 +add a cron job for
  15 +python cadjsonparse.py
  16 +
  17 +
  18 +place bitcoin_converter.js in accessable webdir
  19 +
  20 +call bitcoin_converter.js from html
  21 +
  22 +<script type="text/javascript" src="http://meme.hackspace.ca/bitcoin_converter.js"></script>
  23 +Membership costs <span class="usd_to_btc">50</span> BTC per month
96 bitcoin_converter.js
... ... @@ -0,0 +1,96 @@
  1 +/*
  2 +Author : ThomasV
  3 +Licence : GPL v3
  4 +Support : 12oabCifvHuxzXtYVGhkxVfWZDvKcU743s
  5 +*/
  6 +
  7 +function getElementsByClassName(oElm, strTagName, strClassName) {
  8 + var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
  9 + var arrReturnElements = new Array();
  10 + strClassName = strClassName.replace(/\-/g, "\\-");
  11 + var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  12 + var oElement;
  13 + for( var i=0; i< arrElements.length; i++ ) {
  14 + oElement = arrElements[i];
  15 + if(oRegExp.test(oElement.className)){
  16 + arrReturnElements.push(oElement);
  17 + }
  18 + }
  19 + return (arrReturnElements);
  20 +}
  21 +
  22 +
  23 +/* A timestamp is added to the url, to prevent cache effects */
  24 +function add_xc_script(){
  25 + var timestamp = new Date().getTime();
  26 + var request_url = "http://meme.hackspace.ca/bitcoin_rate.js?h="+timestamp;
  27 + var scriptObj = document.createElement("script");
  28 + scriptObj.setAttribute("type", "text/javascript");
  29 + scriptObj.setAttribute("src", request_url);
  30 + scriptObj.setAttribute("id", "xc");
  31 + var headLoc = document.getElementsByTagName("head").item(0);
  32 + headLoc.appendChild(scriptObj);
  33 +}
  34 +
  35 +
  36 +function bitcoin_converter_callback( ) {
  37 +
  38 + var c = getElementsByClassName(document,"span","usd_to_btc");
  39 + for(var i=0; i<c.length; i++) {
  40 + address = c[i].title;
  41 + var pstr = c[i].innerHTML.split(" ");
  42 + var usd_price = parseFloat(pstr[0]);
  43 + var add_unit = false;
  44 + if(pstr.length>1) {
  45 + if( pstr[1]=="USD") {
  46 + add_unit = true;
  47 + } else {
  48 + alert("error");
  49 + return;
  50 + }
  51 + }
  52 + var price = Math.round(100*usd_price/self.btc_usd)/100.;
  53 + var price_u = "" + price;
  54 + if(add_unit) price_u = price_u + " BTC";
  55 + if(address) {
  56 + price_u = "<a href='javascript:show_bitcoin_address("+price+",\""+address+"\");'>" + price_u + "</a>";
  57 + }
  58 + c[i].innerHTML = price_u ;
  59 + c[i].title = usd_price + " USD";
  60 + }
  61 +
  62 + var c = getElementsByClassName(document,"span","btc_to_usd");
  63 + for(var i=0; i<c.length; i++) {
  64 + address = c[i].title;
  65 + var pstr = c[i].innerHTML.split(" ");
  66 + var btc_price = parseFloat(pstr[0]);
  67 + var add_unit = false;
  68 + if(pstr.length>1) {
  69 + if( pstr[1]=="BTC") {
  70 + add_unit = true;
  71 + } else {
  72 + alert("error");
  73 + return;
  74 + }
  75 + }
  76 + var price = btc_price*self.btc_usd;
  77 + if(price < 1) precision = 1000.; else precision = 100.;
  78 + price = Math.round(precision*price)/precision;
  79 + var price_u = "" + price;
  80 + if(add_unit) price_u = price_u + " USD";
  81 + if(address) {
  82 + price_u = "<a href='javascript:show_bitcoin_address("+price+",\""+address+"\");'>" + price_u + "</a>";
  83 + }
  84 + c[i].innerHTML = price_u ;
  85 + c[i].title = btc_price + " BTC";
  86 + }
  87 +
  88 +
  89 +}
  90 +
  91 +function show_bitcoin_address(amount, address){
  92 + alert("This item can be purchased with Bitcoins.\nPrice: "+amount+" Bitcoins\nAddress: "+address+"\n\nVisit http://www.bitcoin.org for more information.");
  93 +}
  94 +
  95 +/* change this if you use addLoadEvent */
  96 +window.onload = add_xc_script;
3  bitcoin_rate.js
... ... @@ -0,0 +1,3 @@
  1 +var btc_usd=13.0603;
  2 +var btc_timestamp=1311817403.57;
  3 +bitcoin_converter_callback( );
23 cadjsonparse.py
... ... @@ -0,0 +1,23 @@
  1 +import json
  2 +import urllib2
  3 +import time
  4 +import sys
  5 +
  6 +try:
  7 + data = urllib2.urlopen("http://bitcoincharts.com/t/weighted_prices.json")
  8 +except IOError, (errno):
  9 + print "%s" % (errno)
  10 + sys.exit()
  11 +
  12 +j = json.load(data)
  13 +CADdata = j["CAD"]["24h"]
  14 +
  15 +timestamp = str(time.time())
  16 +
  17 +text_file = open("bitcoin_rate.js", "w")
  18 +
  19 +text_file.write("var btc_usd=" + CADdata + ";\n")
  20 +text_file.write("var btc_timestamp=" + timestamp + ";\n")
  21 +text_file.write("bitcoin_converter_callback( );\n")
  22 +
  23 +text_file.close()
2  mytest.htm
... ... @@ -0,0 +1,2 @@
  1 +<script type="text/javascript" src="http://meme.hackspace.ca/bitcoin_converter.js"></script>
  2 +Membership costs <span class="usd_to_btc">50</span> BTC per month

0 comments on commit 723b907

Please sign in to comment.
Something went wrong with that request. Please try again.