-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrp.js
112 lines (101 loc) · 2.79 KB
/
rp.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
function showMessage(message) {
const messageElement = document.getElementById('msg');
messageElement.innerHTML = message + '\n' + messageElement.innerHTML;
}
async function onFederatedSignin() {
try {
const request = new PaymentRequest([{
supportedMethods: 'https://kenrb.github.io/WebID_Strawman/',
data: {
},
}], {
total: {
label: 'N/A',
amount: {
currency: 'USD',
value: '0.00'
}
}
});
const response = await request.show();
document.getElementById('contents').innerHTML = 'You are now logged in as ' + response.details.username;
await response.complete('success');
} catch (e) {
showMessage(e.toString());
}
}
function check() {
if (!navigator.serviceWorker) {
showMessage('Your browser does not support service workers.');
return 'error';
}
navigator.serviceWorker.getRegistration('service_worker.js').then(registration => {
if (!registration) {
install();
return 'not installed';
}
return 'installed';
})
.catch(error => {
return 'error';
});
}
function install() {
navigator.serviceWorker.register('service_worker.js').then(() => {
return navigator.serviceWorker.ready;
})
.then(registration => {
if (!registration.paymentManager) {
showMessage('This browser does not support payment handlers');
return;
}
registration.paymentManager.instruments
.set('instrument-key', {
name: 'Chrome uses name and icon from the web app manifest',
method: window.location.href,
}).catch(error => {
showMessage('install() instrument setting error: ' + error);
});
}).catch(error => {
showMessage('install() registration error: ' + error);
});
}
function uninstall() {
navigator.serviceWorker
.getRegistration('service_worker.js')
.then(registration => {
registration
.unregister()
.then(result => {
if (!result) {
showMessage('Failed to uninstall service worker',);
}
})
.catch(error => {
showMessage(error);
});
})
.catch(error => {
showMessage(error);
});
}
function installIfNotPresent() {
check();
}
debug_open = false;
function toggleDebug() {
if (debug_open) {
document.getElementById('install_button').style.display = 'none';
document.getElementById('uninstall_button').style.display = 'none';
debug_open = false;
} else {
document.getElementById('install_button').style.display = 'block';
document.getElementById('uninstall_button').style.display = 'block';
debug_open = true;
}
}
if ((window.location.host === 'kenrb.github.io') &&
(window.location.protocol !== 'https:')) {
window.location.protocol = 'https:';
}
installIfNotPresent();