-
Notifications
You must be signed in to change notification settings - Fork 14
/
popup.js
137 lines (120 loc) · 6.14 KB
/
popup.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log("received message in popup!!!", request);
if(request.popup_timer) {
$('#timer_area').text(request.popup_timer);
return
}
if(request.popup_status) {
var s = $('#status');
if(request.fail) {
s.css({background: 'darkred', color: 'white'});
s.html("<strong>Error:</strong><br>" + request.popup_status);
} else {
s.css({background: 'lightgreen', color: 'black'});
s.text(request.popup_status);
}
}
});
setTimeout(function(){
// delay this popup creation process until the chrome popup fade-in effect has finished.
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
var tab_id = tabs[0].id;
chrome.runtime.sendMessage({get_tips: true, tab: tab_id}, function(response) {
// when the popup is launched, get list of tips found on the page
$("#whitelist_blacklist_button_area").show().append(response.button);
$(".remove, .add").on('click', function() {
console.log('clicked domain list button');
$(this).hide();
if($(this).hasClass('add')) {
var add_or_remove = 'add'
} else {
var add_or_remove = 'remove'
}
chrome.runtime.sendMessage({
domain: $(this).data('domain'),
add_or_remove: add_or_remove
});
});
$("#options_link").attr('href', chrome.extension.getURL("options.html"));
var tips = response.tips || [];
console.log("tips ->", tips);
chrome.storage.sync.get({
daily_limit_start: null,
usd_tipped_so_far_today: null,
daily_tip_limit: null,
dollar_tip_amount: null,
all_tipped_addresses_today: null,
when_to_send: null,
pub_key: null
}, function(items) {
$("#qr").qrcode({width: 100, height: 100, text: items.pub_key});
$("#tip_history").attr('href', 'https://blockchain.info/address/' + items.pub_key)
var all_tip_ratios = 0;
$.each(tips, function(i, tip) {
all_tip_ratios += tip.ratio;
});
var dollar_tip_amount = items.dollar_tip_amount;
var total_dollar_tip_amount = dollar_tip_amount * all_tip_ratios;
if(tips.length == 1) {
var button_text = "Send $" + total_dollar_tip_amount.toFixed(2) + " to this address";
} else if (tips.length > 1){
var button_text = "Send $" + total_dollar_tip_amount.toFixed(2) + " to these " + tips.length + " addresses";
} else {
var button_text = "No addresses yet";
$("#tip_button").attr('disabled', 'disabled');
}
$("#tip_button").val(button_text).click(function() {
// when the 'tip now' buton is clicked, tell the background to send the tips.
// and prime the status box.
$(this).attr('disabled', 'disabled');
$('#status').show().text("Creating Transaction...").css({background: 'lightgreen', color: 'black'});
chrome.runtime.sendMessage({end_5min_timer: true});
chrome.runtime.sendMessage({perform_tip: 'manual', tips: tips, tab_id: tab_id});
});
chrome.runtime.sendMessage({get_btc_price: true}, function(response) {
var btc_price = response.price;
if(!btc_price) {
//when call to winkdex fails, null is returned
$('#tipping_stats').html("Network Error: Could not get pirce from winkdex");
return
}
$.ajax({
url: "https://blockchain.info/rawaddr/" + items.pub_key,
type: "get",
success: function(response) {
var deposit_btc = response['final_balance'] / 1e10;
var deposit_usd = (deposit_btc * btc_price).toFixed(2);
var msg = "Tipped so far today: <strong>$" + items.usd_tipped_so_far_today.toFixed(2) + "</strong>";
msg += "<br><strong>$" + deposit_usd + "</strong> Remaining on deposit address";
$('#tipping_stats').html(msg);
$('#tipping_stats .spinner').hide();
},
error: function(xhr, status, error) {
$('#tipping_stats').html("Network Error: " + status + error);
}
});
$.each(tips, function(index, tip) {
var img = "<img src='" + get_icon_for_currency(tip.currency || 'btc') + "' width='50px', height='50px'>";
var recipient = "";
if(tip.ratio <= 1) {
var ratio = Number(tip.ratio * 100).toFixed(1) + "%";
} else {
var ratio = Number(tip.ratio.toFixed(3)) + "x";
}
if(tip.recipient) {
recipient = "<big>" + tip.recipient + " (" + ratio + ")</big>";
}
var dollar_ratio = dollar_tip_amount * tip.ratio;
var satoshis = Math.floor(dollar_ratio / btc_price * 1e10)
var tip_html = "<table class='tip_table'><tr><td>" + img + "</td><td>" + recipient + "<br><small>"
+ tip.address + "</small></td><td>$" + dollar_ratio.toFixed(2) + "<br>(" + satoshis + ")</td></tr></table>";
$("#tip_address").append(tip_html);
});
});
});
});
});
}, 100);