This repository has been archived by the owner on Aug 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ingress_export.js
320 lines (280 loc) · 11.9 KB
/
ingress_export.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
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
// ==UserScript==
// @id iitc-plugin-ingressportalcsvexport@zetaphor
// @name IITC Plugin: Ingress Portal CSV Export
// @category Information
// @version 0.0.5
// @namespace http://github.com/Zetaphor/IITC-Ingress-Portal-CSV-Export
// @updateURL https://raw.githubusercontent.com/Zetaphor/IITC-Ingress-Portal-CSV-Export/master/ingress_export.js
// @downloadURL https://raw.githubusercontent.com/Zetaphor/IITC-Ingress-Portal-CSV-Export/master/ingress_export.js
// @description Exports portals to a CSV list
// @include https://*ingress.com/intel*
// @include http://*ingress.com/intel*
// @match https://*ingress.com/intel*
// @match http://*ingress.com/intel*
// @match https://intel.ingress.com/*
// @match http://intel.ingress.com/*
// @grant none
// ==/UserScript==
/*global $:false */
/*global map:false */
/*global L:false */
function wrapper() {
// in case IITC is not available yet, define the base plugin object
if (typeof window.plugin !== "function") {
window.plugin = function() {};
}
// base context for plugin
window.plugin.portal_csv_export = function() {};
var self = window.plugin.portal_csv_export;
window.master_portal_list = {};
window.portal_scraper_enabled = false;
window.current_area_scraped = false;
self.portalInScreen = function portalInScreen(p) {
return map.getBounds().contains(p.getLatLng());
};
// adapted from
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/is-point-in-poly [rev. #0]
self.portalInPolygon = function portalInPolygon(polygon, portal) {
var poly = polygon.getLatLngs();
var pt = portal.getLatLng();
var c = false;
for (var i = -1, l = poly.length, j = l - 1; ++i < l; j = i) {
((poly[i].lat <= pt.lat && pt.lat < poly[j].lat) || (poly[j].lat <= pt.lat && pt.lat < poly[i].lat)) && (pt.lng < (poly[j].lng - poly[i].lng) * (pt.lat - poly[i].lat) / (poly[j].lat - poly[i].lat) + poly[i].lng) && (c = !c);
}
return c;
};
// return if the portal is within the drawtool objects.
// Polygon and circles are available, and circles are implemented
// as round polygons.
self.portalInForm = function(layer) {
if (layer instanceof L.Rectangle) {
return true;
}
if (layer instanceof L.Circle) {
return true;
}
return false;
};
self.portalInGeo = function(layer) {
if (layer instanceof L.GeodesicPolygon) {
return true;
}
if (layer instanceof L.GeodesicCircle) {
return true;
}
return false;
};
self.portalInDrawnItems = function(portal) {
var c = false;
window.plugin.drawTools.drawnItems.eachLayer(function(layer) {
if (!(self.portalInForm(layer) || self.portalInGeo(layer))) {
return false;
}
if (self.portalInPolygon(layer, portal)) {
c = true;
}
});
return c;
};
self.inBounds = function(portal) {
if (window.plugin.drawTools && window.plugin.drawTools.drawnItems.getLayers().length) {
return self.portalInDrawnItems(portal);
} else {
return self.portalInScreen(portal);
}
};
self.genStr = function genStr(title, image, lat, lng, portalGuid) {
var href = lat + "," + lng;
var str= "";
str = title;
str = str.replace(/\"/g, "\\\"");
str = str.replace(";", "_");
str = '"'+str+'"' + "," + href + "," + '"'+image+'"';
if (window.plugin.keys && (typeof window.portals[portalGuid] !== "undefined")) {
var keyCount =window.plugin.keys.keys[portalGuid] || 0;
str = str + "," + keyCount;
}
return str;
};
self.genStrFromPortal = function genStrFromPortal(portal, portalGuid) {
var lat = portal._latlng.lat,
lng = portal._latlng.lng,
title = portal.options.data.title || "untitled portal";
image = portal.options.data.image || ""
return self.genStr(title, image, lat, lng, portalGuid);
};
self.addPortalToExportList = function(portalStr, portalGuid) {
if (typeof window.master_portal_list[portalGuid] == 'undefined') {
window.master_portal_list[portalGuid] = portalStr;
self.updateTotalScrapedCount()
}
};
self.updateTotalScrapedCount = function() {
$('#totalScrapedPortals').html(Object.keys(window.master_portal_list).length);
};
self.drawRectangle = function() {
var bounds = window.map.getBounds();
var bounds = [[bounds._southWest.lat, bounds._southWest.lng], [bounds._northEast.lat, bounds._northEast.lng]];
L.rectangle(bounds, {color: "#00ff11", weight: 1, opacity: 0.9}).addTo(window.map);
};
self.managePortals = function managePortals(obj, portal, x) {
if (self.inBounds(portal)) {
var str = self.genStrFromPortal(portal, x);
obj.list.push(str);
obj.count += 1;
self.addPortalToExportList(str, x);
}
return obj;
};
self.checkPortals = function checkPortals(portals) {
var obj = {
list: [],
count: 0
};
for (var x in portals) {
if (typeof window.portals[x] !== "undefined") {
self.managePortals(obj, window.portals[x], x);
}
}
return obj;
};
self.generateCsvData = function() {
var csvData = 'Name, Latitude, Longitude, Image' + "\n";
$.each(window.master_portal_list, function(key, value) {
csvData += (value + "\n");
});
return csvData;
};
self.downloadCSV = function() {
var csvData = self.generateCsvData();
var link = document.createElement("a");
link.download = 'Portal_Export.csv';
link.href = "data:text/csv," + escape(csvData);
link.click();
}
self.showDialog = function showDialog(o) {
var csvData = self.generateCsvData();
var data = `
<form name='maxfield' action='#' method='post' target='_blank'>
<div class="row">
<div id='form_area' class="column" style="float:left;width:100%;box-sizing: border-box;padding-right: 5px;">
<textarea class='form_area'
name='portal_list_area'
rows='30'
placeholder='Zoom level must be 15 or higher for portal data to load'
style="width: 100%; white-space: nowrap;">${csvData}</textarea>
</div>
</div>
</form>
`;
var dia = window.dialog({
title: "Portal CSV Export",
html: data
}).parent();
$(".ui-dialog-buttonpane", dia).remove();
dia.css("width", "600px").css("top", ($(window).height() - dia.height()) / 2).css("left", ($(window).width() - dia.width()) / 2);
return dia;
};
self.gen = function gen() {
var dialog = self.showDialog(window.master_portal_list);
return dialog;
};
self.setZoomLevel = function() {
window.map.setZoom(15);
$('#currentZoomLevel').html('15');
self.updateZoomStatus();
};
self.updateZoomStatus = function() {
var zoomLevel = window.map.getZoom();
$('#currentZoomLevel').html(window.map.getZoom());
if (zoomLevel != 15) {
window.current_area_scraped = false;
$('#currentZoomLevel').css('color', 'red');
if (window.portal_scraper_enabled) $('#scraperStatus').html('Invalid Zoom Level').css('color', 'yellow');
}
else $('#currentZoomLevel').css('color', 'green');
};
self.updateTimer = function() {
self.updateZoomStatus();
if (window.portal_scraper_enabled) {
if (window.map.getZoom() == 15) {
if ($('#innerstatus > span.map > span').html() === 'done') {
if (!window.current_area_scraped) {
self.checkPortals(window.portals);
window.current_area_scraped = true;
$('#scraperStatus').html('Running').css('color', 'green');
self.drawRectangle();
} else {
$('#scraperStatus').html('Area Scraped').css('color', 'green');
}
} else {
current_area_scraped = false;
$('#scraperStatus').html('Waiting For Map Data').css('color', 'yellow');
}
}
}
};
self.panMap = function() {
window.map.getBounds();
window.map.panTo({lat: 40.974379, lng: -85.624982});
};
self.toggleStatus = function() {
if (window.portal_scraper_enabled) {
window.portal_scraper_enabled = false;
$('#scraperStatus').html('Stopped').css('color', 'red');
$('#startScraper').show();
$('#stopScraper').hide();
$('#csvControlsBox').hide();
$('#totalPortals').hide();
} else {
window.portal_scraper_enabled = true;
$('#scraperStatus').html('Running').css('color', 'green');
$('#startScraper').hide();
$('#stopScraper').show();
$('#csvControlsBox').show();
$('#totalPortals').show();
self.updateTotalScrapedCount();
}
};
// setup function called by IITC
self.setup = function init() {
// add controls to toolbox
var link = $("");
$("#toolbox").append(link);
var csvToolbox = `
<div id="csvToolbox" style="position: relative;">
<p style="margin: 5px 0 5px 0; text-align: center; font-weight: bold;">Portal CSV Exporter</p>
<a id="startScraper" style="position: absolute; top: 0; left: 0; margin: 0 5px 0 5px;" onclick="window.plugin.portal_csv_export.toggleStatus();" title="Start the portal data scraper">Start</a>
<a id="stopScraper" style="position: absolute; top: 0; left: 0; display: none; margin: 0 5px 0 5px;" onclick="window.plugin.portal_csv_export.toggleStatus();" title="Stop the portal data scraper">Stop</a>
<div class="zoomControlsBox" style="margin-top: 5px; padding: 5px 0 5px 5px;">
Current Zoom Level: <span id="currentZoomLevel">0</span>
<a style="margin: 0 5px 0 5px;" onclick="window.plugin.portal_csv_export.setZoomLevel();" title="Set zoom level to enable portal data download.">Set Zoom Level</a>
</div>
<p style="margin:0 0 0 5px;">Scraper Status: <span style="color: red;" id="scraperStatus">Stopped</span></p>
<p id="totalPortals" style="display: none; margin:0 0 0 5px;">Total Portals Scraped: <span id="totalScrapedPortals">0</span></p>
<div id="csvControlsBox" style="display: none; margin-top: 5px; padding: 5px 0 5px 5px; border-top: 1px solid #20A8B1;">
<a style="margin: 0 5px 0 5px;" onclick="window.plugin.portal_csv_export.gen();" title="View the CSV portal data.">View Data</a>
<a style="margin: 0 5px 0 5px;" onclick="window.plugin.portal_csv_export.downloadCSV();" title="Download the CSV portal data.">Download CSV</a>
</div>
</div>
`;
$(csvToolbox).insertAfter('#toolbox');
window.csvUpdateTimer = window.setInterval(self.updateTimer, 500);
// delete self to ensure init can't be run again
delete self.init;
};
// IITC plugin setup
if (window.iitcLoaded && typeof self.setup === "function") {
self.setup();
} else if (window.bootPlugins) {
window.bootPlugins.push(self.setup);
} else {
window.bootPlugins = [self.setup];
}
}
// inject plugin into page
var script = document.createElement("script");
script.appendChild(document.createTextNode("(" + wrapper + ")();"));
(document.body || document.head || document.documentElement)
.appendChild(script);