-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwm-test.js
64 lines (57 loc) · 1.96 KB
/
wm-test.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
var progress_done = 0;
var pending_done = 0;
var payment_sum = 0;
function logMessage(message, event) {
console.log(message);
let li = document.createElement("li");
li.textContent = message;
if (event) {
console.log(payment_sum, event.detail.amount);
if (event.detail.amount){
payment_sum += parseInt(event.detail.amount);
}
let pre = document.createElement("pre");
let code = document.createElement("code");
code.textContent = JSON.stringify(event.detail, null, 2);
pre.appendChild(code);
li.appendChild(pre);
}
document.getElementById("info").appendChild(li);
}
if (document.monetization) {
logMessage('Browser supports WebMonetization');
} else {
logMessage('Browser does not support WebMonetization');
}
function startEventHandler(event) {
let message = "WM started: ";
logMessage(message, event);
}
function pendingEventHandler(event) {
pending_done += 1;
let message = "WM pending: ";
logMessage(message, event);
if (pending_done > 4) {
document.monetization.removeEventListener('monetizationprogress', progressEventHandler);
}
}
function progressEventHandler(event) {
progress_done += 1;
let message = "WM progress: " + progress_done + " ";
if (progress_done < 6) {
logMessage(message, event);
}
if (progress_done <= 100) {
if (progress_done % 10 === 0) {
logMessage(message, event);
}
}
if (progress_done >= 100) {
document.monetization.removeEventListener('monetizationprogress', progressEventHandler);
message = 'WM sent ' + payment_sum + ' in ' + progress_done + ' iterations.';
logMessage(message);
}
}
document.monetization.addEventListener('monetizationstart', startEventHandler);
document.monetization.addEventListener('monetizationpending', pendingEventHandler);
document.monetization.addEventListener('monetizationprogress', progressEventHandler);