This repository has been archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
90 lines (84 loc) · 3.2 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
var currentUrl;
function formatDate(msAgo) {
let value = msAgo;
function toString(unit) {
return Math.floor(value) + unit + ' ago';
}
if (value < 1000) { return toString('ms'); }
value/=1000;
if (value < 60) { return toString('s'); }
value/=60;
if (value < 60) { return toString('m'); }
value/=60;
if (value < 24) { return toString('h'); }
value/=24;
if (value < 365) { return toString('d'); }
value/=365;
return toString('y');
}
function displayLedger(ledger) {
let html = '<table><tr><td><h2>Balances:</h2><ul>';
console.log(ledger.balances);
for (let peerName in ledger.balances) {
html += `<li>${peerName}: ` +
ledger.balances[peerName].current + ' +[' +
ledger.balances[peerName].receivable + '] -[' +
ledger.balances[peerName].payable + ']</li>';
}
html += '</ul></td><td><h2>Transaction:</h2>';
console.log(html, ledger.transactions);
for (let channelName in ledger.transactions) {
html += '<h3>' + channelName + '</h3><ul>';
for (let msgId in ledger.transactions[channelName]) {
const transaction = ledger.transactions[channelName][msgId];
if (transaction.status == 'accepted') {
html += `<li>${transaction.request.msgId}: ${transaction.request.amount} (${transaction.request.unit})</li>`;
} else if (transaction.status == 'pending') {
html += `<li><em>[${transaction.request.msgId}: ${transaction.request.amount} (${transaction.request.unit})]</em></li>`;
}
}
html += '</ul>';
}
html += '</td></tr></table>';
document.getElementById('ledger').innerHTML = html;
// for(i in clickers) {
// document.getElementById(`link-${i}`).onclick = function() {
// currentUrl = clickers[i]
// chrome.tabs.update({ url: currentUrl });
// displayLedger(ledgers, currentUrl);
// };
// }
}
function pay() {
const amount = parseFloat(document.getElementById('amount').value);
const recurring = document.getElementById('recurring').checked;
console.log('Pay!', { amount, recurring, currentUrl });
chrome.runtime.sendMessage({ cmd: 'pay', amount, recurring, currentUrl }, function (response) {
console.log('Paid!', { amount, recurring, response, currentUrl });
});
};
function request() {
const amount = parseFloat(document.getElementById('amount').value);
const recurring = document.getElementById('recurring').checked;
console.log('Requesting!', { amount, recurring, currentUrl });
chrome.runtime.sendMessage({ cmd: 'request', amount, recurring, currentUrl }, function (response) {
console.log('Requested!', { amount, recurring, response, currentUrl });
});
};
function updateUI() {
console.log('updating ui!');
chrome.runtime.sendMessage({ cmd: 'getLedger' }, function (response) {
console.log(response);
displayLedger(response.ledger);
});
}
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('pay-button').onclick = pay;
document.getElementById('request-button').onclick = request;
document.getElementById('ledger').innerHTML = 'popup script loaded';
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {
currentUrl = tabs[0].url;
console.log({ currentUrl });
setInterval(updateUI, 1000);
});
});