-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenstreetmap.ts
209 lines (201 loc) · 7.29 KB
/
openstreetmap.ts
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
const presence = new Presence({
clientId: "983277228710297600",
}),
browsingTimestamp = Math.floor(Date.now() / 1000);
let locationcity: string,
locationstate: string,
locationcountry: string,
x: string,
y: string,
editedObject: string,
selectedObject: boolean;
setInterval(() => {
if (x && y) {
const fetchingGeo = fetch(
`https://nominatim.openstreetmap.org/reverse?lat=${x}&lon=${y}&format=json`
);
fetchingGeo.then(res => {
res.json().then(res => {
[locationcity, locationstate, locationcountry] = [
res.address.isolated_dwelling ??
res.address.hamlet ??
res.address.village ??
res.address.town ??
res.address.city,
res.address.state,
res.address.country,
];
});
});
fetchingGeo.catch();
}
}, 5000);
presence.on("UpdateData", async () => {
const privacySettings = await presence.getSetting<boolean>("privacy");
let presenceData: PresenceData = {
largeImageKey: "logo",
startTimestamp: browsingTimestamp,
details: "Viewing:",
};
const pages: Record<string, PresenceData> = {
"/traces": { state: "Public GPS tracks" },
"/traces/new": { state: "GPS tracks upload" },
"/diary": { state: "Users' Diaries" },
"/diary/new": { state: "Writing a new diary entry" },
"/help": { state: "Getting Help" },
"/welcome": { state: "OpenStreetMap basics guide" },
"/copyright": { state: "Copyright and License" },
"/about": { state: "About OpenStreetMap" },
"/dashboard": { state: "User Dashboard" },
"/preferences": { state: "User preferences" },
"/export": { state: "Map exports" },
"/directions": { state: "Planning a route" },
"/oauth2": { state: "User OAuth applications" },
"/history": { state: "Changeset history" },
"/account/edit": { state: "User settings" },
};
for (const [path, data] of Object.entries(pages)) {
if (document.location.pathname.includes(path))
presenceData = { ...presenceData, ...data };
}
const pagesSubdomain: Record<string, PresenceData> = {
dmca: { state: "Claim of Copyright Infringement" },
dns: { state: "GeoDNS Zones" },
git: { state: "Project Git" },
lists: { state: "Project Mailing Lists" },
welcome: { state: "Welcome Mat - Organizations" },
blogs: { state: "Community blogs" },
nominatim: { state: "Nominatim search engine" },
};
for (const [path, data] of Object.entries(pagesSubdomain)) {
if (document.location.hostname.startsWith(path))
presenceData = { ...presenceData, ...data };
}
if (document.location.hostname.startsWith("help")) {
presenceData.details = "Reading help:";
presenceData.state = document.title.split(" -")[0];
} else if (
document.location.hostname.startsWith("blog") &&
!document.location.hostname.startsWith("blogs")
) {
presenceData.details = "Reading a blog post:";
presenceData.state = document.title.split("|")[0];
presenceData.buttons = [
{ label: "Read this post", url: document.location.href },
];
} else if (document.location.hostname.startsWith("forum")) {
presenceData.details = "Reading a forum post:";
presenceData.state = document.title.split("/")[0];
presenceData.buttons = [
{ label: "Read this post", url: document.location.href },
];
} else if (document.location.hostname.startsWith("community")) {
presenceData.details = "Reading a community post:";
presenceData.state = document.title.split("-")[0];
presenceData.buttons = [
{ label: "Read this post", url: document.location.href },
];
} else if (
document.location.pathname.includes("/search") &&
!document.location.pathname.includes("nominatim")
) {
presenceData.details = "Search results for:";
presenceData.state = document.title.split("|")[0];
} else if (document.location.pathname.includes("/user/")) {
presenceData.details = `On ${document.title
.split("|")[0]
.trim()}'s profile:`;
const userEdits = document.querySelector(
"#content > div.content-heading > div > div > div.col > nav > ul > li:nth-child(1) > span"
).textContent,
mapperSince = document.querySelector(
"#content > div.content-heading > div > div > div.col > div > small > dl > dd"
).textContent;
presenceData.state = `${userEdits} edits since ${mapperSince}`;
presenceData.buttons = [
{ label: "Check this profile", url: document.location.href },
];
} else if (document.location.pathname.includes("/changeset/")) {
presenceData.details = `Watching changeset ${
document.location.href.split("/")[4]
}`;
presenceData.state = `"${document
.querySelector("#sidebar_content > div.browse-section > p.font-italic")
.textContent.trim()}"`;
presenceData.buttons = [
{ label: "Check the changeset", url: document.location.href },
];
} else if (document.location.pathname.includes("/way/")) {
const wayNumber = document
.querySelector(
"#sidebar_content > div.d-flex.w-100 > div.flex-grow-1.text-break > h2"
)
.textContent.split(":")[1];
presenceData.details = `Watching the way ${wayNumber}`;
presenceData.state = `From the changeset "${document
.querySelector("#sidebar_content > div.browse-section > p.font-italic")
.textContent.trim()}"`;
presenceData.buttons = [
{ label: "Check the way", url: document.location.href },
];
} else if (document.location.pathname.includes("/node/")) {
const wayNumber = document
.querySelector(
"#sidebar_content > div.d-flex.w-100 > div.flex-grow-1.text-break > h2"
)
.textContent.split(":")[1];
presenceData.details = `Watching the node ${wayNumber}`;
presenceData.state = `From the changeset ${
document.querySelector(
"#sidebar_content > div.browse-section > p.font-italic"
).textContent
}`;
presenceData.buttons = [
{ label: "Check the node", url: document.location.href },
];
} else if (document.location.pathname.includes("/edit")) {
try {
editedObject = document
.querySelector<HTMLIFrameElement>("#id-embed")
.contentDocument.title.split("–")[1]
.substring(1);
selectedObject = true;
} catch (e) {
selectedObject = false;
}
if (!selectedObject) presenceData.details = "On the map editor";
else presenceData.details = `Editing "${editedObject}"`;
x = document.location.href.split("/")[4];
y = document.location.href.split("/")[5];
if (typeof locationcountry === "undefined" || privacySettings)
presenceData.state = "Somewhere on planet earth";
else
presenceData.state = `Near ${locationcity}, ${locationstate}, ${locationcountry}`;
if (!privacySettings) {
presenceData.buttons = [
{ label: "Edit the map", url: document.location.href },
];
}
} else if (document.location.hash.includes("#map=")) {
presenceData.details = "Looking at the map";
x = document.location.href.split("/")[4];
y = document.location.href.split("/")[5];
if (typeof locationcountry === "undefined" || privacySettings)
presenceData.state = "Somewhere on planet earth";
else
presenceData.state = `Near ${locationcity}, ${locationstate}, ${locationcountry}`;
if (!privacySettings) {
presenceData.buttons = [
{ label: "Open the map", url: document.location.href },
];
}
} else if (document.location.pathname.startsWith("/wiki")) {
presenceData.details = "Reading the wiki";
presenceData.state = `"${document.title.split("—")[0]}"`;
presenceData.buttons = [
{ label: "Check the page", url: document.location.href },
];
}
if (presenceData.details) presence.setActivity(presenceData);
else presence.setActivity();
});