-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscanner.user.js
More file actions
426 lines (374 loc) · 16.2 KB
/
Copy pathscanner.user.js
File metadata and controls
426 lines (374 loc) · 16.2 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// ==UserScript==
// @name Waze Scan Closures
// @namespace https://github.com/WazeDev/waze-scan-closures
// @version 0.0.31
// @description Passively scans for user-generated/reported road closures in WME and sends Discord/Slack notifications when new closures are reported.
// @author Gavin Canon-Phratsachack (https://github.com/gncnpk)
// @match https://beta.waze.com/*editor*
// @match https://www.waze.com/*editor*
// @exclude https://www.waze.com/*user/*editor/*
// @exclude https://www.waze.com/discuss/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=waze.com
// @license MIT
// @contributionURL https://github.com/WazeDev/Thank-The-Authors
// @grant GM_xmlhttpRequest
// @grant unsafeWindow
// @connect wsc.gc-p.zip
// ==/UserScript==
/* global WMEWAL */
(function () {
'use strict';
unsafeWindow.SDK_INITIALIZED.then(init);
let sdk;
let userReportedClosures = [];
let trackedClosures = [];
let wazeEditorName;
let url = localStorage.getItem("waze-scan-closures-url") || "https://wsc.gc-p.zip";
let endpoints = {
"TRACKED_CLOSURES": `${url}/trackedClosures`,
"UPLOAD_CLOSURES": `${url}/uploadClosures`
}
// Status message element reference
let statusMsgEl = null;
function titleCase(s) {
return s.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}
async function init() {
sdk = unsafeWindow.getWmeSdk({
scriptId: 'wme-scan-closures',
scriptName: 'Waze Scan Closures'
});
while (sdk.State.getUserInfo() === null) {
console.log("Waze Scan Closures: Waiting for user to be logged in...");
await new Promise(resolve => setTimeout(resolve, 1000));
}
wazeEditorName = sdk.State.getUserInfo().userName;
console.log(`Waze Scan Closures: Logged in as ${wazeEditorName}`);
getTrackedClosures();
sdk.Events.trackDataModelEvents({
dataModelName: "roadClosures"
});
sdk.Events.on({
eventName: "wme-data-model-objects-added",
eventHandler: updateRoadClosures
});
userReportedClosures = filterUserClosures(sdk.DataModel.RoadClosures.getAll());
sdk.Sidebar.registerScriptTab().then(async (res) => {
res.tabLabel.innerText = "WSC";
// Create text area for inputting url, update variable when value changes
res.tabPane.innerHTML = `
<div>
<label for="WSCApiUrl">API URL:</label>
<input type="text" id="WSCApiUrl" value="${url}" style="width: 100%;" />
</div>
<div id="WSCStatusMsg" style="margin-top:8px;color:#007700;font-weight:bold;"></div>
`;
res.tabPane.querySelector("#WSCApiUrl").addEventListener("input", (e) => {
url = e.target.value;
localStorage.setItem("waze-scan-closures-url", url);
endpoints["TRACKED_CLOSURES"] = `${url}/trackedClosures`;
endpoints["UPLOAD_CLOSURES"] = `${url}/uploadClosures`;
});
statusMsgEl = res.tabPane.querySelector("#WSCStatusMsg");
});
checkWAL();
console.log(`Waze Scan Closures: Initialized!`);
}
// check if WAL is running and if so, set its flag to keep closures layer enabled during scans
function checkWAL(tries = 1) {
if (typeof WMEWAL == 'object' && typeof WMEWAL.alwaysLoadClosures == 'boolean') {
WMEWAL.alwaysLoadClosures = true;
}
else if (tries < 60)
setTimeout(function () {checkWAL(++tries);}, 1000);
}
// Helper to set status message
function setStatusMsg(msg, color = '#007700') {
if (statusMsgEl) {
statusMsgEl.textContent = msg;
statusMsgEl.style.color = color;
}
}
function getTrackedClosures() {
if (url === "" || wazeEditorName === undefined || wazeEditorName === null) {
console.error("Waze Scan Closures: URL not set!");
setStatusMsg("Upload failed: URL not set!", '#bb0000');
return;
}
let data = {
userName: wazeEditorName,
env: sdk.Settings.getRegionCode()
}
let details = {
method: "POST",
data: JSON.stringify(data),
url: endpoints["TRACKED_CLOSURES"],
headers: {
"Content-Type": "application/json"
},
onload: function (response) {
let responseData = JSON.parse(response.responseText);
// Handle both old format (array) and new format (object)
if (Array.isArray(responseData)) {
trackedClosures = responseData;
} else {
trackedClosures = responseData.trackedClosures || [];
}
console.log(`Waze Scan Closures: Retrieved ${trackedClosures.length} tracked closures!`);
},
onerror: function () {
setStatusMsg("Failed to retrieve tracked closures!", '#bb0000');
}
};
console.log(`Waze Scan Closures: Retriving tracked closures...`);
GM_xmlhttpRequest(details);
}
// Allowed durations (in ms):
const ALLOWED_DURATIONS = [
30 * 60 * 1000, // 30 minutes
1 * 60 * 60 * 1000, // 1 hour
5 * 60 * 60 * 1000, // 5 hours
16 * 60 * 60 * 1000, // 16 hours
72 * 60 * 60 * 1000 // 72 hours
];
// Margin of error around each target (1 minute = 60 000 ms)
const MARGIN = 60 * 1000;
function filterUserClosures(closures) {
return closures.filter(c => {
// must have no description, valid dates, not already tracked
if (
c.description ||
!c.startDate ||
!c.endDate ||
trackedClosures.includes(c.id)
) {
return false;
}
// compute actual duration in ms
const duration =
new Date(c.endDate).getTime() -
new Date(c.startDate).getTime();
// check if it matches any allowed duration within the margin
return ALLOWED_DURATIONS.some(
target => Math.abs(duration - target) <= MARGIN
);
});
}
// Function to find adjacent closures by traversing connected segments
function findAdjacentClosures(closureSegmentId, allClosures, visited = new Set()) {
// Avoid infinite loops
if (visited.has(closureSegmentId)) {
return [];
}
visited.add(closureSegmentId);
const adjacentClosures = [];
const segment = sdk.DataModel.Segments.getById({ segmentId: closureSegmentId });
if (!segment) {
return adjacentClosures;
}
// Get nodes connected to this segment
const nodeIds = [segment.fromNodeId, segment.toNodeId].filter(id => id !== null);
for (const nodeId of nodeIds) {
const node = sdk.DataModel.Nodes.getById({ nodeId: nodeId });
if (!node) continue;
// Get all segments connected to this node
const connectedSegmentIds = node.connectedSegmentIds || [];
for (const connectedSegmentId of connectedSegmentIds) {
// Skip the original segment
if (connectedSegmentId === closureSegmentId) continue;
// Check if this connected segment has a closure
const closureOnSegment = allClosures.find(closure => closure.segmentId === connectedSegmentId);
if (closureOnSegment) {
adjacentClosures.push({
closure: closureOnSegment,
connectionNodeId: nodeId,
segmentId: connectedSegmentId
});
// Recursively find closures adjacent to this one
const furtherAdjacent = findAdjacentClosures(connectedSegmentId, allClosures, visited);
adjacentClosures.push(...furtherAdjacent);
}
}
}
return adjacentClosures;
}
// Function to group closures by adjacency
function groupClosuresByAdjacency(closures) {
const groups = [];
const processed = new Set();
for (const closure of closures) {
if (processed.has(closure.id)) continue;
const group = [closure];
processed.add(closure.id);
// Find all adjacent closures for this closure
const adjacent = findAdjacentClosures(closure.segmentId, closures);
for (const adj of adjacent) {
if (!processed.has(adj.closure.id)) {
group.push(adj.closure);
processed.add(adj.closure.id);
}
}
groups.push({
closures: group,
isGroup: group.length > 1,
groupId: group.map(c => c.id).sort().join('-')
});
}
return groups;
}
function removeObjectProperties(obj, props) {
for (var i = 0; i < props.length; i++) {
if (obj.hasOwnProperty(props[i])) {
delete obj[props[i]];
}
}
};
function updateRoadClosures() {
let currentUserReportedClosures = filterUserClosures(
sdk.DataModel.RoadClosures.getAll()
);
if (currentUserReportedClosures.length !== 0) {
userReportedClosures = currentUserReportedClosures;
console.log(
`Waze Scan Closures: Found ${userReportedClosures.length} user reported ` +
'closures!'
);
setStatusMsg(`Found ${userReportedClosures.length} user reported closures!`, '#0055bb');
// Group closures by adjacency
const closureGroups = groupClosuresByAdjacency(userReportedClosures);
console.log(`Waze Scan Closures: Grouped into ${closureGroups.length} closure groups`);
// helper: convert ms → "Xh Ym"
const formatDuration = ms => {
const totalMin = Math.round(ms / 60000);
const hrs = Math.floor(totalMin / 60);
const mins = totalMin % 60;
let str = '';
if (hrs) str += `${hrs}h`;
if (mins) str += `${hrs ? ' ' : ''}${mins}m`;
return str || '0m';
};
// Process all closures and add adjacency information
const allProcessedClosures = [];
for (const group of closureGroups) {
// Filter out closures without valid segments first
const validClosures = group.closures.filter(closure => {
if (closure.segmentId !== null) {
closure.segment = sdk.DataModel.Segments.getById({
segmentId: closure.segmentId
});
}
if (!closure.segment) {
console.log(`Waze Scan Closures: Skipping closure ${closure.id} - no segment found`);
return false;
}
return true;
});
validClosures.forEach(i => {
// track
trackedClosures.push(i.id);
if (i.segment) {
i.roadType =
I18n.t('segment.road_types')[i.segment.roadType];
i.roadTypeEnum = i.segment.roadType;
i.lon = i.segment.geometry.coordinates
.reduce((s, c) => s + c[0], 0) /
i.segment.geometry.coordinates.length;
i.lat = i.segment.geometry.coordinates
.reduce((s, c) => s + c[1], 0) /
i.segment.geometry.coordinates.length;
}
// Get address using the SDK method
const address = sdk.DataModel.Segments.getAddress({ segmentId: i.segmentId });
// build human-readable location using address components
const location = [];
if (address && !address.isEmpty) {
if (address.street && address.street.name.trim() !== '') {
location.push(address.street.name);
}
if (address.city && address.city.name.trim() !== '') {
location.push(address.city.name);
}
if (address.state && address.state.name.trim() !== '') {
location.push(address.state.name);
}
if (address.country && address.country.name.trim() !== '') {
location.push(address.country.name);
}
}
i.location = location.join(', ');
// metadata
i.createdBy = i.modificationData.createdBy;
i.createdOn = i.modificationData.createdOn;
i.direction = i.isForward ? 'A➜B' : 'B➜A';
i.status = titleCase(i.status);
// ← NEW: compute duration
const durationMs =
new Date(i.endDate).getTime() -
new Date(i.startDate).getTime();
i.durationMs = durationMs;
i.duration = formatDuration(durationMs);
// Add adjacency information
i.isPartOfGroup = group.isGroup;
i.groupId = group.groupId;
i.groupSize = group.closures.length;
if (group.isGroup) {
i.adjacentClosureIds = group.closures
.filter(c => c.id !== i.id)
.map(c => c.id);
}
// strip out unneeded props before upload
removeObjectProperties(i, [
'isPermanent',
'description',
'endDate',
'modificationData',
'startDate',
'isForward',
'segment',
'trafficEventId'
]);
});
allProcessedClosures.push(...validClosures);
}
const uploadData = {
// bbox: sdk.Map.getMapExtent(),
userName: wazeEditorName,
zoomLevel: sdk.Map.getZoomLevel(),
closures: allProcessedClosures
};
sendClosures(uploadData);
} else {
console.log('Waze Scan Closures: No new closures found...');
setStatusMsg('No new closures found...', '#e6b800');
}
}
function sendClosures(uploadData) {
if (url === "" || wazeEditorName === undefined || wazeEditorName === null) {
console.error("Waze Scan Closures: URL not set!");
setStatusMsg("Upload failed: URL not set!", '#bb0000');
return;
}
// use GM_xmlhttpRequest(details)
let details = {
method: "POST",
url: endpoints["UPLOAD_CLOSURES"],
data: JSON.stringify(uploadData),
headers: {
"Content-type": "application/json; charset=UTF-8"
},
onload: function (response) {
setStatusMsg(`${uploadData.closures.length} closures uploaded successfully!`, '#007700');
getTrackedClosures();
},
onerror: function () {
setStatusMsg("Upload failed: Network error", '#bb0000');
}
};
setStatusMsg(`Uploading ${uploadData.closures.length} closures...`, '#0055bb');
GM_xmlhttpRequest(details);
getTrackedClosures();
}
})();