The ChromeBookmark Cryptocurrency Lookup Tool in Javascript


The CoinTools Chrome Extension has an important feature which is the Alt+Q query of the cryptocurrency. This feature can be easily extracted to a Chrome Bookmark Tool.

The Cryptocurrency Lookup is a lightweight Chrome Bookmark Tool that you can add to your bookmarket bar. Once launched, the tool will prompt a dialog that allows your to query the following conversion rates between cryptocurrency/fiat pairs.

Repository: https://github.com/DoctorLai/ChromeBookMark_CryptocurrencyLookup

Cryptocurrency Query Syntax

  • BTC SBD – to get the exchange rate between BTC and SBD
  • 2 BTC LTC – to see how many LTC can we get out of 2 BTC
  • LTC 6 BTC – to see how many LTC are equal to 6 BTC
  • the above pairs can also be fiats e.g. USD EUR

How to Install the ChromeBookmark Cryptocurrency Lookup Tool?

Create a Bookmark and paste the source in the URL:

chrome-bookmark The ChromeBookmark Cryptocurrency Lookup Tool in Javascript chrome browser Cryptocurrency javascript

chrome-bookmark

Then, when you click the bookmark, you can issue the query commands:

screenshots The ChromeBookmark Cryptocurrency Lookup Tool in Javascript chrome browser Cryptocurrency javascript

screenshots of cryptocurrency queries

Javascript Source Code of Cryptocurrency Bookmark

Full source code at github.
Copy to the URL part of the bookmark editor. Avoid using // comments which will break (new lines will be removed). The last query is saved in the local storage of the Chrome browser (current active sessions).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
javascript:
(function() {
    /* read as text */
    function __readResponseAsText(response) {
        return response.text();
    }
 
    /* read as json */
    function __readResponseAsJSON(response) { 
        return response.json(); 
    } 
 
    /* check if valid response */
    function __validateResponse(response) { 
        if (!response.ok) { 
            throw Error(response.statusText); 
        } 
        return response; 
    }
 
    function __isNumeric(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
 
    /* getting conversion from cryptocompare */
    function CoinToools_GetPriceCC(a, b) {
        a = a.toUpperCase();
        if (a == 'SBD') {
            a = 'SBD*';
        }
        b = b.toUpperCase();
        if (b == 'SBD') {
            b = 'SBD*';
        }
        if (a == 'SP') {
            a = 'STEEM';
        }
        if (b == 'SP') {
            b = 'STEEM';
        }
        let api = "https://min-api.cryptocompare.com/data/price?fsym=" + a + "&tsyms=" + b;
        return new Promise((resolve, reject) => {
            fetch(api, {mode: 'cors'})
            .then(__validateResponse)
            .then(__readResponseAsJSON)
            .then(function(result) {
                if (result[b]) {
                    resolve(result[b]);
                } else {
                    reject("Error: " + a + ", " + b);
                }
            }).catch(function(error) {
                reject(error);
            });
        });    
    }
 
    let query = localStorage.getItem("CryptocurrencyLookupQuery");
    if (!query) {
        query = "";
    }
    let s = prompt("(Cryptocompare) e.g. BTC STEEM, 10 BTC SBD, BTC 10 SBD", query);
    if (s != null) {
        s = s.trim();
        localStorage.setItem("CryptocurrencyLookupQuery", s);
        let arr = s.split(' ');
        if (arr.length == 2) { /* BTC SBD */
            CoinToools_GetPriceCC(arr[0], arr[1]).then((x) => {
                alert("1 " + arr[0].toUpperCase() + " = " + x + " " + arr[1].toUpperCase());
            }).catch((error) => {
                alert(error);
            });
        } else if (arr.length == 3) { /* 2 BTC STEEM */
            if (__isNumeric(arr[0])) {
                CoinToools_GetPriceCC(arr[1], arr[2]).then((x) => {
                    alert(arr[0] + " " + arr[1].toUpperCase() + " = " + (x * arr[0]) + " " + arr[2].toUpperCase());
                }).catch((error) => {
                    alert(error);
                });                     
            } else if (__isNumeric(arr[1])) { /* SBD 3 STEEM */
                CoinToools_GetPriceCC(arr[2], arr[0]).then((x) => {
                    alert((arr[1] * x) + " " + arr[0].toUpperCase() + " = " + arr[1] + " " + arr[2].toUpperCase());
                }).catch((error) => {
                    alert(error);
                });  
            }
        }
    }
})();
javascript:
(function() {
	/* read as text */
	function __readResponseAsText(response) {
	    return response.text();
	}

	/* read as json */
	function __readResponseAsJSON(response) { 
	    return response.json(); 
	} 

	/* check if valid response */
	function __validateResponse(response) { 
	    if (!response.ok) { 
	        throw Error(response.statusText); 
	    } 
	    return response; 
	}

	function __isNumeric(n) {
	    return !isNaN(parseFloat(n)) && isFinite(n);
	}

	/* getting conversion from cryptocompare */
	function CoinToools_GetPriceCC(a, b) {
	    a = a.toUpperCase();
	    if (a == 'SBD') {
	        a = 'SBD*';
	    }
	    b = b.toUpperCase();
	    if (b == 'SBD') {
	        b = 'SBD*';
	    }
	    if (a == 'SP') {
	    	a = 'STEEM';
	    }
	    if (b == 'SP') {
	    	b = 'STEEM';
	    }
	    let api = "https://min-api.cryptocompare.com/data/price?fsym=" + a + "&tsyms=" + b;
	    return new Promise((resolve, reject) => {
	        fetch(api, {mode: 'cors'})
	        .then(__validateResponse)
	        .then(__readResponseAsJSON)
	        .then(function(result) {
	            if (result[b]) {
	                resolve(result[b]);
	            } else {
	                reject("Error: " + a + ", " + b);
	            }
	        }).catch(function(error) {
	            reject(error);
	        });
	    });    
	}

	let query = localStorage.getItem("CryptocurrencyLookupQuery");
	if (!query) {
		query = "";
	}
	let s = prompt("(Cryptocompare) e.g. BTC STEEM, 10 BTC SBD, BTC 10 SBD", query);
	if (s != null) {
		s = s.trim();
		localStorage.setItem("CryptocurrencyLookupQuery", s);
		let arr = s.split(' ');
		if (arr.length == 2) { /* BTC SBD */
			CoinToools_GetPriceCC(arr[0], arr[1]).then((x) => {
				alert("1 " + arr[0].toUpperCase() + " = " + x + " " + arr[1].toUpperCase());
			}).catch((error) => {
				alert(error);
			});
		} else if (arr.length == 3) { /* 2 BTC STEEM */
			if (__isNumeric(arr[0])) {
				CoinToools_GetPriceCC(arr[1], arr[2]).then((x) => {
					alert(arr[0] + " " + arr[1].toUpperCase() + " = " + (x * arr[0]) + " " + arr[2].toUpperCase());
				}).catch((error) => {
					alert(error);
				});	    				
			} else if (__isNumeric(arr[1])) { /* SBD 3 STEEM */
				CoinToools_GetPriceCC(arr[2], arr[0]).then((x) => {
					alert((arr[1] * x) + " " + arr[0].toUpperCase() + " = " + arr[1] + " " + arr[2].toUpperCase());
				}).catch((error) => {
					alert(error);
				});	 
			}
		}
	}
})();

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
623 words
Last Post: How to Determine the Version of Microsoft Word using VBScript/JScript?
Next Post: Steem Witness Replay Time Takes Longer and Longer

The Permanent URL is: The ChromeBookmark Cryptocurrency Lookup Tool in Javascript

Leave a Reply