Silk Road forums

Discussion => Off topic => Topic started by: cbrdck on June 28, 2012, 12:12 pm

Title: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: cbrdck on June 28, 2012, 12:12 pm
Hi guys,

I was browsing the weed section and I thought it would be useful to see the prices in GBP as well as BTC. I started doing just that and from there it kinda got out of control a bit.

So I give you my greasemonkey script:
* Gets the exchange rates from intersango.com
* Adds paper currency column next to BTC price column on category listings.
* * Switch between GBP, EUR and USD by clicking the header link ("<->")
* Adds another column with <currency>/gram cost. This is picked up from title and may not always work, so do not trust implicitly.
* The above are also displayed in item detail page
* Saves the exchange rates and your currency preference in a cookie

Maybe someone else will also find this useful. If you have GreaseMonkey installed, get the script from here:

http://userscripts.org/scripts/show/137270

In case you missed it above, **THIS SCRIPT CREATES A COOKIE TO STORE THE CURRENT EXCHANGE RATE AND YOUR PREFERRED CURRENCY** 
Phew. I was required by UK law to let you know. And I'm all for the Law, guys, i am seriouslah.

I am releasing this code under DGAFv3. Feel free not to give a fuck either.

edit: new d/l link

edit 2: If the install button doesn't work for you, right click the install button, click View User Script Source, and you should see a yellow bar with an install button. Alternatively, save to somewhere and load manually.

* It goes without saying, don't visit unless over Tor. Kind of a dead giveaway.
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: Kappacino on June 28, 2012, 12:33 pm
Someone verify this shit, I'm too inept to give it a go
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: cbrdck on June 28, 2012, 01:16 pm
source
Code: [Select]
// ==UserScript==
// @name        Real world currency mod for SR
// @namespace   sr
// @description SR: convert BTC to GBP/EUR/USD. Calculate <CUR>/gram. Gets live exchange rate from intersango.com. Works on category and info pages and uses cookie to store exchange rate and prefs.
// @include     http://silkroadvb5piz3r.onion/silkroad/category/*
// @version     1.1
// ==/UserScript==


var active_cur='gbp';
var trading={};

function getSymbol(str)
{
switch(str)
{
case 'gbp': return '&pound;';break;
case 'usd': return '$';break;
case 'eur': return '&euro;';break;
}
}

function findGrams(body)
{
var matches = ( (/[^A-Za-z0-9](\d+[\.\d]*)+[\s]*(gram|grams|gr|g)+[^A-Za-z0-9]/i) ).exec(body);
if ( matches && matches.length )
return parseFloat(matches[1]);
var matches = ( (/[^A-Za-z0-9](\d+[\.\d]*)+g[^A-Za-z0-9]/i) ).exec(body);
if ( matches && matches.length )
return parseFloat(matches[1]);
return false;
}

function draw()
{
if (location.href.search('/item/')>-1)
{
var bs = document.getElementsByTagName('b');
var priceb = bs.item(0);
var body = document.getElementsByTagName('table').item(0).innerHTML;

var price = body.substr( body.search( 'Price' ) );
price = price.substr( 0, price.search('<br><br>') );
price = parseFloat(price.substr( price.lastIndexOf('>')+2 ));

var item = '<BR/>'+getSymbol(active_cur)+(price*trading[active_cur]).toFixed(2)

var g = findGrams(body);

if (g)
item+=' ('+(price*trading[active_cur]/g).toFixed(2)+' '+getSymbol(active_cur)+'/g)';

item += ' (<A href="#" onclick="changeCurrency()">change</a>)';

var first_b = document.getElementsByTagName('b').item(0);
if (window.original)
first_b.innerHTML=window.original;
else
window.original=first_b.innerHTML;
first_b.innerHTML += item;
return;
}
if (location.href.search('/category/')>-1)
{
var x = document.getElementById('table1');
if (window.before)
x.innerHTML = window.before;
else
window.before = x.innerHTML;
var trs = x.getElementsByTagName('tr');
for(var ri in trs)
{
if (ri==0){
var item = document.createElement('th');
item.innerHTML=active_cur+' <a href="#" onclick="changeCurrency()">&lt;-&gt;</a>';
trs[ri].insertBefore(item,trs[ri].childNodes[3]);
var item = document.createElement('th');
item.innerHTML=getSymbol(active_cur)+'/g';
trs[ri].insertBefore(item,trs[ri].childNodes[4]);
continue;
}
var item = document.createElement('td');
var pricetd = trs[ri].childNodes[2];
var btc_price = parseFloat(pricetd.innerHTML.substr(1));
var g = findGrams(trs[ri].innerHTML);

item.innerHTML=getSymbol(active_cur)+(btc_price*trading[active_cur]).toFixed(2);


var item2 = document.createElement('td');
item2.innerHTML= g ? (btc_price*trading[active_cur]/g).toFixed(2) : '?';
trs[ri].insertBefore(item2,trs[ri].childNodes[3]);

trs[ri].insertBefore(item,trs[ri].childNodes[3]);

}
}
}


//Utils

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getTradingPrices(resp)
{
var gbp = resp.responseText.substr( resp.responseText.search('British Pound') );
gbp = gbp.substr( 0, gbp.search('</dd>') );
gbp = parseFloat( gbp.substr( gbp.lastIndexOf('>')+1 ) );
trading.gbp=gbp;

var eur = resp.responseText.substr( resp.responseText.search('Euro') );
eur = eur.substr( 0, eur.search('</dd>') );
eur = parseFloat( eur.substr( eur.lastIndexOf('>')+1 ) );
trading.eur=eur;

var usd = resp.responseText.substr( resp.responseText.search('US Dollar') );
usd = usd.substr( 0, usd.search('</dd>') );
usd = parseFloat( usd.substr( usd.lastIndexOf('>')+1 ) );
trading.usd=usd;

setCookie('trading',JSON.stringify(trading));
draw();
}


var currencies=['gbp','eur','usd'];
unsafeWindow.currencies=currencies;
unsafeWindow.getSymbol=getSymbol;
unsafeWindow.findGrams=findGrams;
function changeCurrency()
{
var cur = currencies.indexOf( active_cur );
var next = ++cur % currencies.length;
active_cur = currencies[next];
draw();
trading.active_cur = active_cur;
setCookie('trading',JSON.stringify(trading));
}
unsafeWindow.changeCurrency=changeCurrency;
unsafeWindow.active_cur=active_cur;
unsafeWindow.trading=trading;

if(!getCookie('trading'))
{
console.log('Getting exchange rates from intersango.');
  GM_xmlhttpRequest({
  method: "GET",
  url: "http://www.intersango.com/",
  onload: getTradingPrices
});

}
else
{
console.log('Using stored exchange rates from intersango.');
eval('trading='+getCookie('trading'));
if (trading.active_cur)
active_cur = trading.active_cur;
draw();
}
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: cbrdck on June 29, 2012, 06:06 am
New version, new link due to idiocy (signed up with anonymous email address, didnt note it down, lost account)

 http://userscripts.org/scripts/show/137270

New features:
+cart page (same stuff as before, paper currency, cost/g, total cost in paper currency)
+most other pages (user, home): adds paper-currency equivalent next to BTC price.
+added footer to display current exchange rate & change currency, refresh links
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: cbrdck on June 29, 2012, 06:20 am
It doesn't, GreaseMonkey's GM_xmlhttpRequest doesn't set referer.

For extra peace of mind, I double-checked that on a server of my own just now, feel free to try it too. Search for GM_xmlhttpRequest in source.
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: Ballzinator on June 29, 2012, 07:23 am
Whoa, didn't expect fellow computer science nerds here :D
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: cbrdck on June 29, 2012, 09:36 am
You're more than welcome to rewrite it in Haskell, Dr. Shannon.
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: gtgeorgz on June 29, 2012, 09:49 am
Ok, so i press install on the download page and nothing happens?
I'm using the latest Tor browser included in the Tor Browser Bundle, and yes, i have got greasemonkey enabled and installed.
any tips?
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: cbrdck on June 29, 2012, 10:15 am
Hm, I'm getting the same thing. I suppose it has something to do with the TorBrowser restrictions, as I can't install any other scripts either.

This worked for me:

1) Right clicking "Install", click "View User Script Source". A new tab should open.
2) Click "Install" on the yellow bar that reads "This is a Greasemonkey user script. (...)"

If this doesn't work either, you can always save it to disk and load it manually.
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: Boris Badenov on June 29, 2012, 12:07 pm
This sounds like a dangerous script to me.

BB
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: Hamb999 on June 29, 2012, 04:23 pm
It would be awesome if you could then sort by $$ per mg, so you could see the most and least expensive.

Just a thought - I am not a computer geek so I don't know how difficult this would be to do.

Thanks cbrdck!  I would +1 you if I could.
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: cbrdck on June 29, 2012, 06:48 pm
This sounds like a dangerous script to me.

BB

Hi Boris,

While I understand your distrust, I can assure you that to the best of my personal and professional knowledge (am in related field), this script is not dangerous to you. It does exactly what I advertise, which can be verified by anyone with minimal JS understanding.

The one minor security consideration that _could_ _potentially_ affect this kind of script is leak the page you are on to intersango.com when making the exchange-rate request (http-referer*). However Greasemonkey does _not_ do this, something that I both knew already _and_ double-checked with this particular script (by querying a server of my own and checking that the referer is indeed blank). Again, verifiable by a simple tweak of the source.

Another consideration for the truly paranoid would be that a cookie is set (in order to avoid querying intersango.com on every page load and to store your currency preference). Personally I fail to see how this could be a problem, as SR already uses a cookie to store your session information (plus, the default TorBrowser behavior is to clear all cookies when quitting). Despite this, it still is a piece of information I'm storing in your browser, which is I mention this in the description twice - once IN CAPS!

The address queried at intersango.com is purposefully innocuous (just the homepage... intersango.com) in case a sudden surge of queries to some /weird/api?location= caused suspicions or could link you (even then, your Tor exit address) to usage of this script.

Please understand that I am _not_ trying to convince you to use my script. I don't profit from it in any way (apart from the satisfaction that someone else is finding my code useful) as there are no ads, no malicious side to it, no other hidden agenda, etc.** I didn't even put up a bitcoin address for donations, if you notice, nor will I.

I _do_ want to clarify that I have not -intentionally or not-  put out a script that could endanger you or compromise your anonymity or account. As a new guy here, I feel it is important to clarify this :)

But again, I understand your fears, so no offense taken. After all, I am using this bitch too, and I wouldn't needlessly fuck with my well-being. And am slightly paranoid.***

TL; DR: The most dangerous thing this does is load the intersango.com homepage, and this without exposing which page you are on - it will look like you just visited it via TOR. Need someone else to verify, shouldn't be that hard to find.

*Explanation, in case you're interested: When you make an HTTP(S) request to a server, there is an optional header that may be set by your browser, called "referer". This is set to the page that led you to the page you are viewing now. For example, if you click the first Google search result of any query, the referer header on the page you end up will read something like "http://www.google.com/search?q=blah+blah", so the server knows how you ended up there.
** Again, please see source for proof. Does nobody read JS here? (I'd advise dramamine before reading though... kinda messy)
*** I find it hard to believe this forum doesn't have a paranoid (-looking) emoticon.

PS: So much for not giving a fuck. Someone got high :D
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: cbrdck on June 29, 2012, 07:31 pm
With spectacular timing,

v1.3b: bugfix: category page did not draw footer info + links.

lols were had.
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: cbrdck on June 29, 2012, 07:41 pm
It would be awesome if you could then sort by $$ per mg, so you could see the most and least expensive.

Just a thought - I am not a computer geek so I don't know how difficult this would be to do.
Although it isn't impossible to do, I think it would be impractical over Tor.

In order for the sorted view to actually show the cheapest $/g, it would have to fetch all other pages in the background, so as to include all data in the table before sorting it (there is no guarrantee that the cheapest $/g will be on the first page of results). Over Tor, all these requests would take considerable time. It would also create a pretty huge table in popular categories, which wouldn't be great for slow computers, so you'd need to hide most of the stuff and paginate...

It would take a bit to do, I might get to it if I find some time.
Thanks cbrdck!  I would +1 you if I could.
You're very welcome.
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: vlad1m1r on June 30, 2012, 12:38 am
Hi guys,

I was browsing the weed section and I thought it would be useful to see the prices in GBP as well as BTC. I started doing just that and from there it kinda got out of control a bit.

So I give you my greasemonkey script:
* Gets the exchange rates from intersango.com
* Adds paper currency column next to BTC price column on category listings.
* * Switch between GBP, EUR and USD by clicking the header link ("<->")
* Adds another column with <currency>/gram cost. This is picked up from title and may not always work, so do not trust implicitly.
* The above are also displayed in item detail page
* Saves the exchange rates and your currency preference in a cookie

Maybe someone else will also find this useful. If you have GreaseMonkey installed, get the script from here:

http://userscripts.org/scripts/show/137270

In case you missed it above, **THIS SCRIPT CREATES A COOKIE TO STORE THE CURRENT EXCHANGE RATE AND YOUR PREFERRED CURRENCY** 
Phew. I was required by UK law to let you know. And I'm all for the Law, guys, i am seriouslah.

I am releasing this code under DGAFv3. Feel free not to give a fuck either.

edit: new d/l link

edit 2: If the install button doesn't work for you, right click the install button, click View User Script Source, and you should see a yellow bar with an install button. Alternatively, save to somewhere and load manually.

* It goes without saying, don't visit unless over Tor. Kind of a dead giveaway.

Hi Cyberduck,

Many thanks for this +1

I run a service selling Bitcoins for cash in the mail at Intersango rates and have decided to install this on the browser I use at the office so I can monitor prices (my customers will be pleased to hear I use a separate account to actually process and view orders!)

If you ever need any BTC and want to obtain them safely, feel free to send me a message.

All the best,

V.
Title: Re: Greasemonkey script to display paper currencies (USD,EUR,GBP) and cost/gram
Post by: username100 on August 22, 2012, 07:50 pm
OK, I have this installed but I don't see any footer to choose my currency.

edit: OK I found the "change paper currency" setting in "account" but it is still displaying price in bitcoins...