-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
349 lines (308 loc) · 12.3 KB
/
index.html
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
<title>nosweet</title>
<style>
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #800080, #FFE400);
background-size: 200% 200%;
animation: colorShift 20s ease infinite;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
color: white;
overflow: hidden;
flex-direction: column;
text-align: center;
padding: 0 50px;
}
@keyframes colorShift {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
.title {
font-size: 2.5rem;
background-color: #00000060;
border-radius: 15px;
padding: 15px;
font-weight: bold;
margin-bottom: 20px;
color: white;
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.8);
}
.container {
background-color: #00000060;
border-radius: 15px;
padding: 30px;
text-align: center;
max-width: 400px;
width: 100%;
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.8);
}
h1, label, p {
color: white;
}
input[type="text"], input[type="password"] {
width: calc(100% - 20px);
padding: 10px;
margin: 10px 0;
border: none;
border-radius: 8px;
background-color: rgba(0, 0, 0, 0.528);
color: white;
font-size: 1rem;
text-align: center;
}
button {
padding: 12px 20px;
margin-top: 15px;
border: none;
border-radius: 16px;
font-size: 1.2rem;
font-weight: bold;
cursor: pointer;
background-color: #00000060;
color: white;
transition: 0.3s ease, color 0.3s ease;
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.8);
}
button:disabled {
opacity: 0.7;
cursor: not-allowed;
}
.small-button {
width: 48%;
font-size: 1rem;
}
.button-group {
display: flex;
justify-content: space-between;
margin-top: 10px;
}
.github-button {
margin-top: 20px;
display: inline-block;
width: 200px;
background-color: #00000060;
color: white;
padding: 12px 20px;
text-align: center;
font-size: 0.9rem;
font-weight: bold;
border-radius: 16px;
text-decoration: none;
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.8);
transition: 0.3s ease;
}
.info-text {
margin-top: 20px;
font-size: 0.75rem;
color: white;
opacity: 0.8;
max-width: 400px;
text-align: center;
}
@media (max-width: 768px) {
.small-button {
display: none;
}
.title {
font-size: 2rem;
}
h1 {
font-size: 1.5rem;
}
label {
font-size: 1rem;
}
button {
font-size: 1rem;
}
button[type="submit"], .github-button {
width: 100%;
}
.container {
max-width: 100%;
padding: 30px;
}
}
</style>
<script type="module">
import { nip19, finalizeEvent, getPublicKey } from 'https://cdn.jsdelivr.net/npm/nostr-tools/+esm';
import { Relay, useWebSocketImplementation } from 'https://cdn.jsdelivr.net/npm/nostr-tools/relay/+esm';
useWebSocketImplementation(WebSocket);
const relays = [
'wss://strfry.iris.to',
'wss://relay.damus.io',
'wss://relay.nostr.band',
'wss://relay.snort.social',
'wss://vitor.nostr1.com',
'wss://nos.lol',
'wss://untreu.me'
];
async function encryptNsec(nsec, password) {
const enc = new TextEncoder();
const encodedNsec = enc.encode(nsec);
const encodedPassword = enc.encode(password);
const key = await crypto.subtle.digest('SHA-256', encodedPassword);
const encryptedNsec = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv: encodedPassword.slice(0, 12) },
await crypto.subtle.importKey('raw', key, 'AES-GCM', false, ['encrypt']),
encodedNsec
);
return btoa(String.fromCharCode(...new Uint8Array(encryptedNsec)));
}
async function decryptNsec(encryptedNsec, password) {
const dec = new TextEncoder();
const encodedPassword = dec.encode(password);
const key = await crypto.subtle.digest('SHA-256', encodedPassword);
const encryptedBuffer = Uint8Array.from(atob(encryptedNsec), c => c.charCodeAt(0));
try {
const decryptedNsec = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv: encodedPassword.slice(0, 12) },
await crypto.subtle.importKey('raw', key, 'AES-GCM', false, ['decrypt']),
encryptedBuffer
);
return new TextDecoder().decode(decryptedNsec);
} catch (e) {
throw new Error('Decryption failed');
}
}
async function publishToRelays(signedEvent) {
const successfulRelays = [];
for (const relayUrl of relays) {
try {
const relay = await Relay.connect(relayUrl);
await relay.publish(signedEvent);
successfulRelays.push(relayUrl);
} catch (error) {
console.error(`Error publishing to relay ${relayUrl}:`, error);
}
}
if (successfulRelays.length === 0) {
throw new Error('Failed to publish to any relay');
}
return successfulRelays;
}
document.addEventListener('DOMContentLoaded', async function() {
const savedNsec = localStorage.getItem('encryptedNsec');
const saveButton = document.getElementById('saveNsec');
const resetButton = document.getElementById('resetNsec');
const nsecInput = document.getElementById('nsec');
if (savedNsec) {
const password = prompt('Please enter your password to decrypt your nsec:');
try {
const nsec = await decryptNsec(savedNsec, password);
nsecInput.value = nsec;
nsecInput.disabled = true;
saveButton.disabled = true;
} catch (error) {
alert('Wrong password or decryption failed!');
}
} else {
resetButton.disabled = true;
}
});
document.getElementById('saveNsec').addEventListener('click', async function() {
const nsec = document.getElementById('nsec').value;
const password = prompt('Please enter a password to encrypt your nsec:');
if (nsec && password) {
const encryptedNsec = await encryptNsec(nsec, password);
localStorage.setItem('encryptedNsec', encryptedNsec);
alert('Your nsec has been encrypted and saved.');
document.getElementById('saveNsec').disabled = true;
document.getElementById('resetNsec').disabled = false;
document.getElementById('nsec').disabled = true;
}
});
document.getElementById('resetNsec').addEventListener('click', function() {
localStorage.removeItem('encryptedNsec');
document.getElementById('nsec').value = '';
alert('Stored nsec has been removed.');
document.getElementById('resetNsec').disabled = true;
document.getElementById('saveNsec').disabled = false;
document.getElementById('nsec').disabled = false;
});
document.getElementById('tweetForm').addEventListener('submit', async function(e) {
e.preventDefault();
const button = e.target.querySelector('button[type="submit"]');
button.innerHTML = 'Sharing...';
button.disabled = true;
const tweetUrl = document.getElementById('tweetUrl').value;
const nsec = document.getElementById('nsec').value;
try {
const { type, data: sk } = nip19.decode(nsec);
if (type !== 'nsec') throw new Error('Invalid nsec key format');
const pk = getPublicKey(sk);
const response = await fetch('https://gutolcam.com:3000/scrape-tweet', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ tweetUrl })
});
const result = await response.json();
if (response.ok) {
let combinedContent = result.content;
if (result.mediaUrl) {
combinedContent += `\n${result.mediaUrl}`;
}
if (combinedContent.endsWith('No media found')) {
combinedContent = combinedContent.replace('No media found', '').trim();
}
const eventTemplate = {
kind: 1,
pubkey: pk,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: combinedContent
};
const signedEvent = finalizeEvent(eventTemplate, sk);
const successfulRelays = await publishToRelays(signedEvent);
document.getElementById('result').textContent = 'Note published successfully!';
const relayList = document.createElement('p');
relayList.textContent = `Published to: ${successfulRelays.join(', ')}`;
document.getElementById('result').appendChild(relayList);
} else {
document.getElementById('result').textContent = `Error: ${result.error}`;
}
} catch (error) {
document.getElementById('result').textContent = `Error: ${error.message}`;
} finally {
button.disabled = false;
button.innerHTML = 'Share note';
}
});
</script>
</head>
<body>
<div class="title">nosweet.net 🦩</div>
<div class="container">
<h1>Share a tweet on Nostr ⚙️</h1>
<form id="tweetForm">
<label for="tweetUrl">Tweet URL 🐦:</label>
<input type="text" id="tweetUrl" placeholder="https://twitter.com/example/status/1234567890" required>
<label for="nsec">Your nsec key 🔑:</label>
<input type="password" id="nsec" placeholder="nsec123..." required>
<button type="submit">Share note 🗒️</button>
<div class="button-group">
<button type="button" id="saveNsec" class="small-button">Save nsec 💾</button>
<button type="button" id="resetNsec" class="small-button">Delete nsec 🗑️</button>
</div>
</form>
<p id="result"></p>
</div>
<a href="https://github.com/untreu2/nosweet." class="github-button">Source code on GitHub 🔨</a>
<p class="info-text">Your nsec key is only stored locally in your browser. The signing and broadcasting of the tweet are also done entirely on your local machine.</p>
</body>
</html>