-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWME-MT-GIS-Map.js
147 lines (130 loc) · 5.33 KB
/
WME-MT-GIS-Map.js
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
/* eslint-disable max-len */
/* eslint-disable max-classes-per-file */
// ==UserScript==
// @name WME MT GIS Map
// @namespace https://greasyfork.org/users/166860
// @version 2025.01.23.00
// @description Open a MT GIS map in another window, at the same location as the WME map. Keeps the location of the GIS map synced to WME.
// @author MapOMatic, MacroNav
// @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
// @include /^https?:\/\/www\.arcgis\.com\/home\/webmap\/viewer\.html\?webmap=27012261362d484db5d24c5046f0ec05.*/
// @license GNU GPLv3
// @require https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js
// ==/UserScript==
/* global W */
/* global turf */
(function main() {
'use strict';
class WMECode {
static mapWindow;
static getMercatorMapExtent() {
const wgs84Extent = W.map.getExtent();
const wgs84LeftBottom = [wgs84Extent[0], wgs84Extent[1]];
const wgs84RightTop = [wgs84Extent[2], wgs84Extent[3]];
const mercatorLeftBottom = turf.toMercator(wgs84LeftBottom);
const mercatorRightTop = turf.toMercator(wgs84RightTop);
return [mercatorLeftBottom[0], mercatorLeftBottom[1], mercatorRightTop[0], mercatorRightTop[1]];
}
static onButtonClick() {
const wazeExt = this.getMercatorMapExtent();
let url = 'http://www.arcgis.com/home/webmap/viewer.html?webmap=27012261362d484db5d24c5046f0ec05&extent=';
url += `${wazeExt[0]}%2C${wazeExt[1]}%2C${wazeExt[2]}%2C${wazeExt[3]}%2C102113`;
if (!this.mapWindow || this.mapWindow.closed) {
this.mapWindow = window.open(null, 'mt_gis_map');
try {
this.mapWindow.location?.assign(url);
} catch (e) {
if (e.code === 18) {
// Ignore if accessing location.href is blocked by cross-domain.
} else {
throw e;
}
}
}
this.mapWindow.focus();
this.postMessage();
}
static postMessage() {
if (this.mapWindow && !this.mapWindow.closed) {
const extent = this.getMercatorMapExtent();
this.mapWindow.postMessage({
type: 'setExtent',
xmin: extent[0],
xmax: extent[2],
ymin: extent[1],
ymax: extent[3]
}, 'https://www.arcgis.com');
}
}
static init() {
$('.WazeControlPermalink').prepend(
$('<div>').css({ float: 'left', display: 'inline-block', padding: '0px 5px 0px 3px' }).append(
$('<div>', { id: 'mt-gis-button', title: 'Open the MT GIS map in a new window' })
.text('MTGIS')
.css({
float: 'left', textDecoration: 'none', color: '#000000', fontWeight: 'bold', cursor: 'pointer'
})
.click(this.onButtonClick.bind(this))
)
);
setInterval(() => {
const $btn = $('#mt-gis-button');
if ($btn.length > 0) {
$btn.css('color', (this.mapWindow && !this.mapWindow.closed) ? '#1e9d12' : '#000000');
}
}, 500);
/* Event listeners */
W.map.events.register('moveend', null, this.postMessage.bind(this));
}
static bootstrap() {
if (typeof W === 'object' && W.userscripts?.state.isReady) {
this.init();
} else {
document.addEventListener('wme-ready', this.init.bind(this), { once: true });
}
}
}
class GISMapCode {
static Extent;
static SpatialReference;
static receiveMessage(message) {
const { data } = message;
switch (data.type) {
case 'setExtent': {
const extent = new this.Extent({
xmin: data.xmin,
xmax: data.xmax,
ymin: data.ymin,
ymax: data.ymax,
spatialReference: new this.SpatialReference({ wkid: 102113 })
});
unsafeWindow.arcgisonline.map.main.map.setExtent(extent);
break;
}
default:
// Add more types as needed...
}
}
static init() {
window.addEventListener('message', this.receiveMessage.bind(this));
}
static bootstrap() {
// There may be a more elegant way to check that these modules are ready...
try {
this.Extent = unsafeWindow.require('esri/geometry/Extent');
this.SpatialReference = unsafeWindow.require('esri/SpatialReference');
this.init();
} catch {
setTimeout(this.bootstrap.bind(this), 200);
}
}
}
function bootstrap() {
if (window.location.host.toLowerCase() === 'www.arcgis.com') {
GISMapCode.bootstrap();
} else {
WMECode.bootstrap();
}
}
bootstrap();
})();