forked from ritave/snap-passwordManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
147 lines (133 loc) · 4.17 KB
/
Copy pathindex.html
File metadata and controls
147 lines (133 loc) · 4.17 KB
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
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
<title>Hello, Snaps!</title>
<link rel="icon" type="image/svg" href="./images/icon.svg" />
</head>
<body>
<h1>Hello, Snaps!</h1>
<details>
<summary>Instructions</summary>
<ul>
<li>First, click "Connect". Then, try out the other buttons!</li>
<li>Please note that:</li>
<ul>
<li>
The <code>snap.manifest.json</code> and
<code>package.json</code> must be located in the server root
directory..
</li>
<li>
The Snap bundle must be hosted at the location specified by the
<code>location</code> field of <code>snap.manifest.json</code>.
</li>
</ul>
</ul>
</details>
<br />
<button class="connect">Connect</button>
<div>
<input type="text" placeholder="Website" id="save_website" />
<input type="text" placeholder="Username" id="save_username" />
<input type="password" id="save_password" />
<button id="save">Save password</button>
</div>
<div>
<button id="clear">Clear everything</button>
</div>
<div>
<input
type="text"
placeholder="Search passwords"
id="password_search"
/><br />
<ul id="password_list"></ul>
</div>
</body>
<script>
const snapId = `local:${window.location.href}`;
const connectButton = document.querySelector('button.connect');
const searchInput = document.querySelector('input#password_search');
const searchList = document.querySelector('ul#password_list');
const websiteInput = document.querySelector('input#save_website');
const usernameInput = document.querySelector('input#save_username');
const passwordInput = document.querySelector('input#save_password');
const saveButton = document.querySelector('button#save');
const clearButton = document.querySelector('button#clear');
connectButton.addEventListener('click', connect);
searchInput.addEventListener('input', (e) =>
searchDebounced(e.target.value.trim()),
);
saveButton.addEventListener('click', () =>
savePassword(
websiteInput.value.trim(),
usernameInput.value.trim(),
passwordInput.value.trim(),
),
);
clearButton.addEventListener('click', clear);
// here we get permissions to interact with and install the snap
async function connect() {
await ethereum.request({
method: 'wallet_enable',
params: [
{
wallet_snap: { [snapId]: {} },
},
],
});
await search();
alert('Connected');
}
function debounce(func, timeout = 250) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => func.apply(this, args), timeout);
};
}
async function search(pattern = searchInput.value) {
const results = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, { method: 'search', pattern }],
});
const searchHtml = results.reduce(
(acc, result) =>
(acc += `<li onclick="showPasssword('${result}')">${result}</li>\n`),
'',
);
searchList.innerHTML = searchHtml;
}
const searchDebounced = debounce(search);
async function savePassword(website, username, password) {
await ethereum.request({
method: 'wallet_invokeSnap',
params: [
snapId,
{ method: 'save_password', website, username, password },
],
});
await search();
alert('Password saved');
}
async function showPasssword(website) {
const result = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, { method: 'get_password', website }],
});
if (result !== undefined) {
alert(
`Website: "${website}", username: "${result.username}", password: "${result.password}"`,
);
}
}
async function clear() {
await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, { method: 'clear' }],
});
await search();
alert('Cleared');
}
</script>
</html>