The CoinTools Update – Alt + Q Inline Cryptocurrency Query


The CoinTools is a lightweight chrome extension that is made for Cryptocurrency fans. The CoinTools is fully open source at: https://github.com/DoctorLai/CoinTools

Install at Chrome Webstore

Chrome Webstore: https://chrome.google.com/webstore/detail/coin-tools/fmglcggbdcbkpkfapngjobfeakehpcgj

For Opera browsers, the workaround is to first install Chrome Extension Gadget. And similarly for Firefox, you can install Chrome Store Foxified before you install CoinTools.

Alt + Q Inline Cryptocurrency Query

Alt Q inline query to lookup any cryptocurrency pairs. Keyboard is always faster than mouse. If you just want to know the exchange rate between two pairs, you can Alt + Q to invoke the query dialog when you are at any web pages:

cryptocurrency-dialog The CoinTools Update - Alt + Q Inline Cryptocurrency Query chrome extension Cryptocurrency javascript

cryptocurrency-dialog

Press Enter and you should get the exchange rate immediately.

cryptocurrency-dialog-lookup The CoinTools Update - Alt + Q Inline Cryptocurrency Query chrome extension Cryptocurrency javascript

cryptocurrency-dialog-lookup

How to Implement The Inline Cryptocurrency Query in Chrome Extension using Javascript?

First, inject the script e.g. content.js in any webpages. This is to define the following in mainfest.json.

"content_scripts": [{
      "matches": ["<all_urls>"],
      "js":[
          "js/jquery-3.2.1.min.js",
          "js/content.js"
      ],
      "run_at":"document_start"
  }]
</all_urls>

Then, we need to listen to the keyboard (Alt + Q) and wait for the Promise to return from Cryptocompare API. The full Javascript source code (github) is as follows:

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
'use strict';
 
(function($) {
    // read as text
    const readResponseAsText = (response) => {
        return response.text();
    }
 
    // read as json
    const readResponseAsJSON = (response) => { 
        return response.json(); 
    } 
 
    // check if valid response
    const validateResponse = (response) => { 
        if (!response.ok) { 
            throw Error(response.statusText); 
        } 
        return response; 
    }
 
    const __isNumeric = (n) => {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
 
    // getting conversion from cryptocompare
    const CoinToools_GetPriceCC = (a, b) => {
        a = a.toUpperCase();
        if (a == 'SBD') {
            a = 'SBD*';
        }
        b = b.toUpperCase();
        if (b == 'SBD') {
            b = 'SBD*';
        }
        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);
            });
        });    
    }
    
    // Alt + Q
    $(document).keydown(function(e) {
        if (e && (e.key.toLowerCase() == "q") && e.altKey) {
            let s = prompt("(Cryptocompare) e.g. BTC STEEM, 10 BTC SBD, BTC 10 SBD", "");
            if (s != null) {
                s = s.trim();
                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);
                        });  
                    }
                }
            }
        }
    });
})(jQuery);
'use strict';

(function($) {
	// read as text
	const readResponseAsText = (response) => {
	    return response.text();
	}

	// read as json
	const readResponseAsJSON = (response) => { 
	    return response.json(); 
	} 

	// check if valid response
	const validateResponse = (response) => { 
	    if (!response.ok) { 
	        throw Error(response.statusText); 
	    } 
	    return response; 
	}

	const __isNumeric = (n) => {
	    return !isNaN(parseFloat(n)) && isFinite(n);
	}

	// getting conversion from cryptocompare
	const CoinToools_GetPriceCC = (a, b) => {
	    a = a.toUpperCase();
	    if (a == 'SBD') {
	        a = 'SBD*';
	    }
	    b = b.toUpperCase();
	    if (b == 'SBD') {
	        b = 'SBD*';
	    }
	    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);
	        });
	    });    
	}
	
	// Alt + Q
	$(document).keydown(function(e) {
	    if (e && (e.key.toLowerCase() == "q") && e.altKey) {
	    	let s = prompt("(Cryptocompare) e.g. BTC STEEM, 10 BTC SBD, BTC 10 SBD", "");
	    	if (s != null) {
	    		s = s.trim();
	    		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);
		    			});	 
	    			}
	    		}
	    	}
	    }
	});
})(jQuery);

Support me and my work as a witness by

Some of my contributions: SteemIt Tools, Bots, APIs and Tutorial

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
695 words
Last Post: Steem Witness Node updated to 0.19.3!
Next Post: The SteemJs Editor - Test Your SteemJs Code Snippet Easily

The Permanent URL is: The CoinTools Update – Alt + Q Inline Cryptocurrency Query

Leave a Reply