-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.html
56 lines (48 loc) · 1.44 KB
/
new.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
---
layout: base.html
---
<main>
<button id="getgeo">Add GPS</button>
<form id="form">
<textarea name="body"></textarea>
<button type="submit">submit</button>
</form>
</main>
<script>
const form = document.querySelector("#form");
const button = document.querySelector("#getgeo");
let currentLocation = null;
let gps = false;
const handleGeoSuccess = (position) => {
gps = true;
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
currentLocation = [latitude, longitude].map((segment) =>
segment.toFixed(2)
);
button.textContent = currentLocation;
};
const handleGeoError = (err) => {
console.error(err);
button.textContent = `error`;
};
button.addEventListener("click", (e) => {
navigator.geolocation.getCurrentPosition(handleGeoSuccess, handleGeoError);
});
const handleSubmission = (e) => {
e.preventDefault();
const text = e.target.elements.body.value;
const date = new Date();
const template = `---
date: ${date.toISOString()}
${(gps && `gps: [${currentLocation}]`) || ""}
---
${text}`;
const message = `📄 - Create new post: ${date}`;
const ghQueryUrl = `https://github.com/xdesro/stillness/new/main/posts?filename=${date}.html&value=${encodeURIComponent(
template
)}&message=${encodeURIComponent(message)}`;
window.location.href = ghQueryUrl;
};
form.addEventListener("submit", handleSubmission);
</script>